使用Java读取串口的程序
2006-03-15 15:49
585 查看
下是我写的用Java读取串口的程序。这个程序里面还有一些问题,也希望有经验的网友能够给我提点意见。
这个简单的程序包括以下文件:
IMU.java (主程序)
ReadBuffer.java (从缓冲区读取一个消息)
ReadSerial.java (读取串口数据并放入缓冲区)
SerialBuffer.java (缓冲区)
WriteSerial.java (不断的往串口送星号´*´)
测试程序:
SendCom.java (将一个数据文件往串口发送)
SEND.TXT (供测试用的数据文件)
在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号´*´作为分隔。这个程序中的问题是ReadSerial进程和WriteSerial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在ReadSerial中声明了FileReader
测试程序:
SendCom.java (将一个数据文件往串口发送)
SEND.TXT (供测试用的数据文件)
在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号´*´作为分隔。这个程序中的问题是ReadSerial进程和WriteSerial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在ReadSerial中声明了FileReader和在WriteSerial中声明了FileWriter)。这样是不能够实现全双工通讯的。
------------------------------------------
import java.io.*;
class IMU
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here
File ComPort = new File("COM1");
SerialBuffer SB = new SerialBuffer();
ReadSerial r1 = new ReadSerial(SB, ComPort);
ReadBuffer r2 = new ReadBuffer(SB);
WriteSerial r3 = new WriteSerial(ComPort);
r1.start();
r2.start();
r3.start();
}
}
---------------------------------------------
import java.io.*;
public class ReadBuffer extends Thread
{
private SerialBuffer ComBuffer;
public ReadBuffer(SerialBuffer SB)
{
ComBuffer = SB;
}
public void run()
{
String Msg;
while (true)
{
Msg = ComBuffer.GetMsg();
System.out.println(Msg);
}
}
}
-----------------------------------------------
import java.io.*;
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private File ComPort;
public ReadSerial(SerialBuffer SB, File Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
FileReader in = new FileReader(ComPort);
while (true)
{
c = in.read();
ComBuffer.PutChar(c);
}
try
{
FileReader in = new FileReader(ComPort);
while (true)
{
c = in.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}
------------------------------------------------
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
public synchronized String GetMsg()
{
int SepMark;
if ((SepMark = Content.indexOf(´*´)) == -1)
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
SepMark = Content.indexOf(´*´);
}
CurrentMsg = Content.substring(0, SepMark);
TempContent = Content.substring(SepMark+1);
Content = TempContent;
notifyAll();
return CurrentMsg;
}
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (c == ´*´)
{
available = true;
}
notifyAll();
}
}
-------------------------------------------------
import java.io.*;
public class WriteSerial extends Thread
{
private SerialBuffer ComBuffer;
private File ComPort;
public WriteSerial(File Port)
{
ComPort = Port;
}
public void run()
{
int c;
try
{
FileWriter out = new FileWriter(ComPort);
while (true)
{
out.write(´*´);
}
} catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
-----------------------------------------------
import java.io.*;
public class SendCom
{
public static void main(String[] args)
{
File OutFile = new File("SEND.TXT");
File ComPort = new File("COM2");
int c;
try
{
FileReader in = new FileReader(OutFile);
FileWriter out = new FileWriter(ComPort);
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
} catch (IOException e) {}
}
}
----------------------------------------------
SEND.TXT*
This is a sample of the data file for program testing. *
It should be in the same directory as the SendCom.class file.*
When you run this sample program, connect your COM1 and COM2 with a
serial cable so that you can test this program on one machine. If
you have two machines, you can connect the two machine via a serial
cable and test it. Modified the definition of ComPort in the program
if necessary. *
Thank you for testing this program. If you have any suggestions please
kindly let me know. *
这个简单的程序包括以下文件:
IMU.java (主程序)
ReadBuffer.java (从缓冲区读取一个消息)
ReadSerial.java (读取串口数据并放入缓冲区)
SerialBuffer.java (缓冲区)
WriteSerial.java (不断的往串口送星号´*´)
测试程序:
SendCom.java (将一个数据文件往串口发送)
SEND.TXT (供测试用的数据文件)
在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号´*´作为分隔。这个程序中的问题是ReadSerial进程和WriteSerial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在ReadSerial中声明了FileReader
测试程序:
SendCom.java (将一个数据文件往串口发送)
SEND.TXT (供测试用的数据文件)
在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号´*´作为分隔。这个程序中的问题是ReadSerial进程和WriteSerial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在ReadSerial中声明了FileReader和在WriteSerial中声明了FileWriter)。这样是不能够实现全双工通讯的。
------------------------------------------
import java.io.*;
class IMU
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here
File ComPort = new File("COM1");
SerialBuffer SB = new SerialBuffer();
ReadSerial r1 = new ReadSerial(SB, ComPort);
ReadBuffer r2 = new ReadBuffer(SB);
WriteSerial r3 = new WriteSerial(ComPort);
r1.start();
r2.start();
r3.start();
}
}
---------------------------------------------
import java.io.*;
public class ReadBuffer extends Thread
{
private SerialBuffer ComBuffer;
public ReadBuffer(SerialBuffer SB)
{
ComBuffer = SB;
}
public void run()
{
String Msg;
while (true)
{
Msg = ComBuffer.GetMsg();
System.out.println(Msg);
}
}
}
-----------------------------------------------
import java.io.*;
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private File ComPort;
public ReadSerial(SerialBuffer SB, File Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
FileReader in = new FileReader(ComPort);
while (true)
{
c = in.read();
ComBuffer.PutChar(c);
}
try
{
FileReader in = new FileReader(ComPort);
while (true)
{
c = in.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}
------------------------------------------------
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
public synchronized String GetMsg()
{
int SepMark;
if ((SepMark = Content.indexOf(´*´)) == -1)
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
SepMark = Content.indexOf(´*´);
}
CurrentMsg = Content.substring(0, SepMark);
TempContent = Content.substring(SepMark+1);
Content = TempContent;
notifyAll();
return CurrentMsg;
}
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (c == ´*´)
{
available = true;
}
notifyAll();
}
}
-------------------------------------------------
import java.io.*;
public class WriteSerial extends Thread
{
private SerialBuffer ComBuffer;
private File ComPort;
public WriteSerial(File Port)
{
ComPort = Port;
}
public void run()
{
int c;
try
{
FileWriter out = new FileWriter(ComPort);
while (true)
{
out.write(´*´);
}
} catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
-----------------------------------------------
import java.io.*;
public class SendCom
{
public static void main(String[] args)
{
File OutFile = new File("SEND.TXT");
File ComPort = new File("COM2");
int c;
try
{
FileReader in = new FileReader(OutFile);
FileWriter out = new FileWriter(ComPort);
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
} catch (IOException e) {}
}
}
----------------------------------------------
SEND.TXT*
This is a sample of the data file for program testing. *
It should be in the same directory as the SendCom.class file.*
When you run this sample program, connect your COM1 and COM2 with a
serial cable so that you can test this program on one machine. If
you have two machines, you can connect the two machine via a serial
cable and test it. Modified the definition of ComPort in the program
if necessary. *
Thank you for testing this program. If you have any suggestions please
kindly let me know. *
相关文章推荐
- 如何使用Java读取串口的程序
- 一个使用Java读取串口的程序
- JAVA本地程序使用FileSystemXmlApplicationContext读取application-datasorce.xml进行数据库操作
- Java解析ELF文件:使用Java读取文件头部、节区头部表、程序头部表
- 使用java程序读取配置文件中的相关属性值-asp.net关注
- 64位机器使用java开发串口程序心得
- 使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)
- 使用JDBC和Hibernate在oracle中插入和读取Blob数据的图片的java程序
- 使用Java程序一次分段读取所有数据(如海量数据)并计数处理
- 使用Java程序读取JPEG文件
- 使用Java程序一次分段读取所有数据(如海量数据)并计数处理
- 使用java.nio读取网页的程序
- 一个使用JAVA编写的类似按键精灵的程序,支持脚本文件编写(含源代码)
- java使用poi读取ecxel文件的工具类
- java 读取串口 com 外部设备
- 使用SerialPort类设计串口通讯程序 (VS2005)[转载]
- 使用JAVA远程调用导致程序自动down掉,jdk1.5的debug模式系统漏洞
- Java中使用Runtime和Process类运行外部程序
- 使用gradle编译和运行Java程序
- python简单程序读取串口信息的方法