這個(gè)簡(jiǎn)單的程序包括以下文件:
IMU.java (主程序)
ReadBuffer.java (從緩沖區(qū)讀取一個(gè)消息)
ReadSerial.java (讀取串口數(shù)據(jù)并放入緩沖區(qū))
SerialBuffer.java (緩沖區(qū))
WriteSerial.java (不斷的往串口送星號(hào)'*')
測(cè)試程序:
SendCom.java (將一個(gè)數(shù)據(jù)文件往串口發(fā)送)
SEND.TXT (供測(cè)試用的數(shù)據(jù)文件)
在這個(gè)通訊程序中使用了一個(gè)簡(jiǎn)單的協(xié)議,既不同的消息之間用星號(hào)'*'作為分隔。這個(gè)程序中的問題是ReadSerial進(jìn)程和WriteSerial進(jìn)程不能夠同時(shí)啟動(dòng),出錯(cuò)信息是不能夠打開串口,因?yàn)橥瑯右粋€(gè)串口不能夠同時(shí)被打開兩次(在ReadSerial中聲明了FileReader
測(cè)試程序:
SendCom.java (將一個(gè)數(shù)據(jù)文件往串口發(fā)送)
SEND.TXT (供測(cè)試用的數(shù)據(jù)文件)
在這個(gè)通訊程序中使用了一個(gè)簡(jiǎn)單的協(xié)議,既不同的消息之間用星號(hào)'*'作為分隔。這個(gè)程序中的問題是ReadSerial進(jìn)程和WriteSerial進(jìn)程不能夠同時(shí)啟動(dòng),出錯(cuò)信息是不能夠打開串口,因?yàn)橥瑯右粋€(gè)串口不能夠同時(shí)被打開兩次(在ReadSerial中聲明了FileReader和在WriteSerial中聲明了FileWriter)。這樣是不能夠?qū)崿F(xiàn)全雙工通訊的。不知道有沒有做過的大俠能夠講講處理的辦法。
/*
*
* 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)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注