Java 串口通讯 RS232(基础、范例、提高)

上传人:枫** 文档编号:509194718 上传时间:2023-07-16 格式:DOC 页数:26 大小:206.50KB
返回 下载 相关 举报
Java 串口通讯 RS232(基础、范例、提高)_第1页
第1页 / 共26页
Java 串口通讯 RS232(基础、范例、提高)_第2页
第2页 / 共26页
Java 串口通讯 RS232(基础、范例、提高)_第3页
第3页 / 共26页
Java 串口通讯 RS232(基础、范例、提高)_第4页
第4页 / 共26页
Java 串口通讯 RS232(基础、范例、提高)_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《Java 串口通讯 RS232(基础、范例、提高)》由会员分享,可在线阅读,更多相关《Java 串口通讯 RS232(基础、范例、提高)(26页珍藏版)》请在金锄头文库上搜索。

1、通过comm.jar等配置 如果用applet应用程序的话,下面这个函数是可以取到串口的,但是如果通过web应用程序(或者通过jsp调用)却取不到串口,而且也不会抛出异常,感觉很奇怪,特来请教! CommPortIdentifier.getPortIdentifiers();同时目标机器的java运行环境也需要把w32comm.dll, comm.jar xxx.proper等放到相应的目录就是用ibm的包而不用sun的comm包: ibm-javacomm-win32-x86.zip 只需要把comm驱动包这个类的装载更新一下即http:/ (与其他应用程序的接口), SerialBuffe

2、r.java(用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。1. SerialBeanSerialBean是本类库与其他应用程序的接口。该类库中定义了SerialBean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:public SerialBean(int PortID)本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示CO

3、M1,PortID = 2 表示COM2,由此类推。public int Initialize()本函数初始化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1。初始化的结果是该串口被SerialBean独占性使用,其参数被设置为9600, N, 8, 1。如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。public String ReadPort(int Length)本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。public void WritePort(String Msg)本函数向串口发送一个字符串

4、。参数Msg是需要发送的字符串。public void ClosePort()本函数停止串口检测进程并关闭串口。SerialBean的源代码如下:package serial;import java.io.*;import java.util.*;import m.*;/* This bean provides some basic functions to implement full dulplex* information exchange through the srial port.*/public class SerialBeanstatic String PortName;Com

5、mPortIdentifier 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 s

6、erial 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;tryportId = CommPortIdentifier.getPortIdentifier(PortName);tryserialPor

7、t = (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.tryin = serialPort.getInputStream();out = serialPort.getOutputStream(); catch (IOException e)return

8、InitFail;/Initialize the communication parameters to 9600, 8, 1, none.tryserialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); catch (UnsupportedCommOperationException e)return InitFail; catch (NoSuchPortException e)return InitFail;/ when successful

9、ly 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 informationreturn InitSuccess;

10、/* 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

11、 to be sent.*/public void WritePort(String Msg)int c;tryfor (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();2. SerialBufferSerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲

12、区中读取数据所需要的函数。public synchronized String GetMsg(int Length)本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。public synchronized void PutChar(int c)本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采措施实现的数据的同步。SerialBuffer的源代码如下:package serial;/* Thi

13、s class implements the buffer area to store incoming data from the serial* port.*/public class SerialBufferprivate 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.*/pub

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号