一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java(用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。
1. SerialBean
SerialBean是本类库与其他应用程序的接口。该类库中定义了SerialBean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:
| public SerialBean(int PortID) |
| public int Initialize() |
| public String ReadPort(int Length) |
| public void WritePort(String Msg) |
| public void ClosePort() |
| package serial; import java.io.*; import java.util.*; import javax.comm.*; /** * * This bean provides some basic functions to implement full dulplex * information exchange through the srial port. * */ public class SerialBean { static String PortName; CommPortIdentifier portId; SerialPort serialPort; static OutputStream out; static InputStream in; SerialBuffer SB; ReadSerial RT; /** * * Constructor * * @param PortID the ID of the serial to be used. 1 for COM1, * 2 for COM2, etc. * */ public SerialBean(int PortID) { PortName = "COM" + PortID; } /** * * This function initialize the serial port for communication. It starts a * thread which consistently monitors the serial port. Any signal captured * from the serial port is stored into a buffer area. * */ public int Initialize() { int InitSuccess = 1; int InitFail = -1; try { portId = CommPortIdentifier.getPortIdentifier(PortName); try { serialPort = (SerialPort) portId.open("Serial_Communication", 2000); } catch (PortInUseException e) { return InitFail; } //Use InputStream in to read from the serial port, and OutputStream //out to write to the serial port. try { in = serialPort.getInputStream(); out = serialPort.getOutputStream(); } catch (IOException e) { return InitFail; } //Initialize the communication parameters to 9600, 8, 1, none. try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { return InitFail; } } catch (NoSuchPortException e) { return InitFail; } // when successfully open the serial port, create a new serial buffer, // then create a thread that consistently accepts incoming signals from // the serial port. Incoming signals are stored in the serial buffer. SB = new SerialBuffer(); RT = new ReadSerial(SB, in); RT.start(); // return success information return InitSuccess; } /** * * This function returns a string with a certain length from the incoming * messages. * * @param Length The length of the string to be returned. * */ public String ReadPort(int Length) { String Msg; Msg = SB.GetMsg(Length); return Msg; } /** * * This function sends a message through the serial port. * * @param Msg The string to be sent. * */ public void WritePort(String Msg) { int c; try { for (int i = 0; i < Msg.length(); i++) out.write(Msg.charAt(i)); } catch (IOException e) {} } /** * * This function closes the serial port in use. * */ public void ClosePort() { RT.stop(); serialPort.close(); } } SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。 在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采措施实现的数据的同步。 SerialBuffer的源代码如下:
3. ReadSerial ReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。
本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓冲区,参数Port指定从串口所接收的数据流。
ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。 ReadSerial的源代码如下:
4. SerialExample SerialExample是本类库所提供的一个例程。它所实现的功能是打开串口COM1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。
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环境下编译通过并成功运行。 文档说明:
相关文档 返回首页 | 关于本站 | | 友情链接 | 广告服务 | 意见建议 | 访客留言 | 本站论坛 Copyright© 2001-2006 ProgramBBS.com All Rights Reserved 版权所有©编程论坛 Email: 吉ICP备05009985号 感谢长春订餐网友情支持 |