一个使用Java读取串口的程序
2008-01-05 19:04:37 来源:WEB开发网
这个简单的程序包括以下文件:
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)。这样是不能够实现全双工通讯的。不知道有没有做过的大侠能够讲讲处理的办法。
/*
*
* IMU.java 1.0
* Main PRogram for Serial Communication
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/
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();
}
}
/*
*
* ReadBuffer.java 1.0
* Program to Read the Serial Buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/
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);
}
}
}
/*
*
* ReadSerial.java 1.0
* Program to read characters from the serial port and put it
* to the buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/
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) {}
}
}
/*
*
* SerialBuffer.java 1.0
* Class that implements the serial buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/
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)
更多精彩
赞助商链接