




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java 串口通讯实例(转)2010-01-28 12:33java串口通讯最近在搞C的串口通讯,C里面没有线程的概念,所以C对串口的读写只能在一个进程里面,这样如果串口的缓存有问题,就会导致报告丢失(正好是我们遇到的),我们来看看支持线程的java是如何来解决这个问题的。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。 本 类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java (用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。SerialBeanSerialBean是本类库与其他应用程序的接口。该类库中定义了SerialBean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:public SerialBean(int PortID) 本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此类推。 public int Initialize() 本函数初始化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1。初始化的结果是该串口被SerialBean独占性使用,其参数被设置为9600, N, 8, 1。如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。 public String ReadPort(int Length) 本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。 public void WritePort(String Msg) 本函数向串口发送一个字符串。参数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 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 startss a * thread which consistently monitors the serial port. Any signal capturred * 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 incomin * 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 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(); =ReadSerialReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。public ReadSerial(SerialBuffer SB, InputStreamPort) 本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓冲区,参数Port指定从串口所接收的数据流。 public void run() 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) =SerialExampleSerialExample是本类库所提供的一个例程。它所实现的功能是打开串口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(); =编译与调试本 类库中使用了Java Communication API (m)。这是一个Java扩展类库,并不包括在标准的Java SDK当中。如果你尚未安装这个扩展类库的话,你应该从Sun公司的Java站点下载这个类库并将其安装在你的系统上。在所下载的包里面包括一个安装说 明,如果你没有正确安装这个类库及其运行环境的话,运行这
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司聚会赞助活动方案
- 公司现场宣传活动方案
- 公司营地团建活动方案
- 公司清远漂流活动方案
- 公司春茗策划方案
- 公司椅子清仓活动方案
- 公司新生产线策划方案
- 公司新春工会活动方案
- 公司组织云年会活动方案
- 公司端午感恩策划方案
- 优2023年医用X射线诊断与介入放射学 辐射安全考核试题库含答案
- 《桥小脑角占位》
- 甘肃省苹果产业发展现状、问题及对策苹果产业的现状及对策
- 培训MSDS专业知识课件
- 夜空中最亮的星二部合唱简谱
- 广东省佛山市南海区2021-2022学年六年级下学期数学学科核心素养水平抽样调研试卷
- YC/T 246-2008烟草及烟草制品烟碱的测定气相色谱法
- 钢结构施工检查记录表格
- 桥梁施工质量控制要点(PPT)
- 一二年级看图说话写话:过河 教学课件
- 售后服务管理制度与工作流程
评论
0/150
提交评论