2. SerialBuffer
SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。
| public synchronized String GetMsg(int Length) | 本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
| public synchronized void PutChar(int c) | 本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。
在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采措施实现的数据的同步。
SerialBuffer的源代码如下:
package serial;
/** * * This class implements the buffer area to store incoming data from the serial * port. * */
public class SerialBuffer { private String Content = ""; private String CurrentMsg, TempContent; private boolean available = false; private int LengthNeeded = 1;
/** * * This function returns a string with a certain length from the incoming * messages. * * @param Length The length of the string to be returned. * */
public synchronized String GetMsg(int Length) { LengthNeeded = Length; notifyAll();
if (LengthNeeded > Content.length()) { available = false; while (available == false) { try { wait(); } catch (InterruptedException e) { } } }
CurrentMsg = Content.substring(0, LengthNeeded); TempContent = Content.substring(LengthNeeded); Content = TempContent; LengthNeeded = 1; notifyAll(); return CurrentMsg; }
/** * * This function stores a character captured from the serial port to the * buffer area. * * @param t The char value of the character to be stored. * */
public synchronized void PutChar(int c) { Character d = new Character((char) c); Content = Content.concat(d.toString()); if (LengthNeeded < Content.length()) { available = true; } notifyAll(); } }
| 3. ReadSerial
ReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。
| public ReadSerial(SerialBuffer SB, InputStream Port) | 本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓冲区,参数Port指定从串口所接收的数据流。
ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。
ReadSerial的源代码如下:
package serial;
import java.io.*;
/** * * This class reads message from the specific serial port and save * the message to the serial buffer. * */
public class ReadSerial extends Thread { private SerialBuffer ComBuffer; private InputStream ComPort;
/** * * Constructor * * @param SB The buffer to save the incoming messages. * @param Port The InputStream from the specific serial port. * */
public ReadSerial(SerialBuffer SB, InputStream Port) { ComBuffer = SB; ComPort = Port; }
public void run() { int c; try { while (true) { c = ComPort.read(); ComBuffer.PutChar(c); } } catch (IOException e) {} } } | 4. SerialExample
SerialExample是本类库所提供的一个例程。它所实现的功能是打开串口COM1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。
import serial.*; import java.io.*;
/** * * This is an example of how to use the SerialBean. It opens COM1 and reads * six messages with different length form the serial port. * */
class SerialExample { public static void main(String[] args) { //TO DO: Add your JAVA codes here
SerialBean SB = new SerialBean(1); String Msg; SB.Initialize(); for (int i = 5; i <= 10; i++) { Msg = SB.ReadPort(i); SB.WritePort("Reply: " + Msg); } SB.ClosePort(); } }
| 5. 编译与调试
本类库中使用了Java Communication API (javax.comm)。这是一个Java扩展类库,并不包括在标准的Java SDK当中。如果你尚未安装这个扩展类库的话,你应该从Sun公司的Java站点下载这个类库并将其安装在你的系统上。在所下载的包里面包括一个安装说明,如果你没有正确安装这个类库及其运行环境的话,运行这个程序的时候你会找不到串口。
正确安装Java Communication API并将上述程序编译通过以后,你可以按如下方法测试这个程序。如果你只有一台机器,你可以利用一条RS-232电缆将COM1和COM2连接起来,在COM1上运行SerialExample,在COM2上运行Windows提供的超级终端程序。如果你有两台机器的话,你可以利用一条RS-232电缆将两台机器的COM1(或者是COM2)连接起来,在一端运行例程,另外一端运行Windows提供的超级终端程序。如果有必要的 话,可以对SerialExample中所声明的串口进行相应改动。
本程序在Windows 2000 + Java SDK 1.3环境下编译通过并成功运行。
|
|