This simple program includes the following files:
IMu.java
(Main program)
Readbuffer.java
(Read a message from the buffer)
Readserial.java
(Read serial port data and put into buffer)
SerialBuffer.java
(Buffer)
WriteSerial.java
(Continuous towards the serial number '*') test procedure:
Sendcom.java
(Send a data file to serial port)
Send.txt
(Data file for testing) uses a simple protocol in this communication program, which is separated by an asterisk '*' between the different messages. The problem in this program is that the Readserial process and the WriteSerial process cannot start at the same time. The error message is not able to open the serial port, because the same serial port is not able to open twice (FileReader in Readserial and declare FileWrit in WriteSerial) . This is not possible to achieve full duplex communication. / ***
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. *; Class imu {public static void main (string [] args) {// to do: add your java cots 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 ** create: 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);}}} / ***}} / ***}}
1.0 * program to read it * to the serial port and put it * to the buffer ** create: 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 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 (TRUE) {C = IN. Read (); Combuffer.Putchar (c);} tryreader in = new fileReader (Comport); while (true) {c = in.read (); combffer.putchar (c);}} catch (ooException 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) {available = false; while (available == false) {Try {Wait ();} catch (interruptedExcection 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. Tnowtring ()); if (c == '*') {available = true;} NOTILALABLE ();} / *** WriteSerial.java
1.0 * program to send a character to the serial port ** 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 WriteSerial extends Thread {private SerialBuffer ComBuffer; private File ComPort; public WriteSerial (File Port) {company = port;} public void run () {int C; trywriter out = new filewriter; while (true) {out.write ('*');}} catch (ooException e) { System.out.println (E.GetMessage ());}}}} @; 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; while ((c = in.read ())! = -1) Out.write (c); in.close (); out.close ();} Catch (ioexception e) {}}}