This simple program includes the following files:
IMU. java (Main Program)
ReadBuffer. java (read a message from the buffer)
ReadSerial. java (reads serial port data and puts it into the buffer zone)
SerialBuffer. java (buffer)
WriteSerial. java (continuously send asterisks to the serial port *)
Test procedure:
SendCom. java (send a data file to the serial port)
SEND. TXT (data file for testing)
A simple protocol is used in this communication program. Different messages are separated by an asterisk. The problem in this program is that the ReadSerial process and the WriteSerial process cannot be started at the same time. The error message is that the serial port cannot be opened twice at the same time because the same serial port cannot be opened twice at the same time.
Test procedure:
SendCom. java (send a data file to the serial port)
SEND. TXT (data file for testing)
A simple protocol is used in this communication program. Different messages are separated by an asterisk. The problem in this program is that the ReadSerial process and the WriteSerial process cannot be started at the same time, and the error message is that the serial port cannot be opened, because the same serial port cannot be opened twice at the same time (the FileReader and the FileWriter are declared in ReadSerial ). In this way, full-duplex communication cannot be achieved. I don't know if there is any hero who can talk about the solution.
/*
*
* IMU. java 1.0
* Main Program for Serial Communication
*
* Created: March 27,200 1
*
* 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,200 1
*
* 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,200 1
*
* 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,200 1
*
* 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)
{
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;
Policyall ();
Return CurrentMsg;
}
Public s