Java串口操作RXTX.doc_第1页
Java串口操作RXTX.doc_第2页
Java串口操作RXTX.doc_第3页
Java串口操作RXTX.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java串口操作RXTXRXTX是个提供串口和并口通信的开源Java类库。该项目提供了Windows,Linux,Mac os X,Solaris操作系统下的兼容m串口通讯包API的实现。一、 使用准备1.Windows将rxtxParallel.dll、rxtxSerial.dll拷贝到%JAVA_HOME%安装目录bin目录下将RXTXcomm.jar 拷贝到%JAVA_HOME%jrelibextRXTXcomm.jar2.Linux复制librxtxSerial.so,librxtxParallel.so到%JAVA_HOME%/lib/$(ARCH)/复制RXTXcomm.jar到%JAVA_HOME%/ext定义驱动类后将perties放在应用程式的根目录下二、 操作类注:串口操作类一定要继承SerialPortEventListenerpublic class SerialPortUtilityNew implements SerialPortEventListener / 检测系统中可用的通讯端口类private CommPortIdentifier portId;/ Enumeration 为枚举型类,在util中private Enumeration portList;/ 输入输出流private InputStream inputStream;private OutputStream outputStream;/ RS-232的串行口private SerialPort serialPort;public static String test = ;/保存串口返回信息private static SerialPortUtilityNew uniqueInstance;/单例创建/初始化串口public void init() / 获取系统中所有的通讯端口portList = CommPortIdentifier.getPortIdentifiers();/ 用循环结构找出串口while (portList.hasMoreElements() / 强制转换为通讯端口类型portId = (CommPortIdentifier) portList.nextElement();/ 判断是否为串口if (portId.getPortType() = CommPortIdentifier.PORT_SERIAL)/比较串口名称是否为“COM3”if (portId.getName().equals(COM3) System.out.println(找到COM3);/ 打开串口try / open:(应用程序名【随意命名】,阻塞时等待的毫秒数)serialPort = (SerialPort) portId.open(GSMModemTest2.0, 2000);/ 设置串口监听serialPort.addEventListener(new SerialPortUtilityNew();/ 设置可监听serialPort.notifyOnDataAvailable(true);/* 设置串口通讯参数 */ 波特率,数据位,停止位和校验方式/ 波特率2400,偶校验serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);test = ;outputStream = serialPort.getOutputStream(); catch (PortInUseException e) e.printStackTrace(); catch (TooManyListenersException e) e.printStackTrace(); catch (UnsupportedCommOperationException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();/* * 实现接口SerialPortEventListener中的方法 读取从串口中接收的数据 */public void serialEvent(SerialPortEvent event) switch (event.getEventType() case SerialPortEvent.BI:case SerialPortEvent.OE:case SerialPortEvent.FE:case SerialPortEvent.PE:case SerialPortEvent.CD:case SerialPortEvent.CTS:case SerialPortEvent.DSR:case SerialPortEvent.RI:case SerialPortEvent.OUTPUT_BUFFER_EMPTY:break;case SerialPortEvent.DATA_AVAILABLE:/获取到串口返回信息readComm();break;default:break;/读取串口返回信息public void readComm() byte readBuffer = new byte1024;try inputStream = uniqueInstance.serialPort.getInputStream();/ 从线路上读取数据流int len = 0;while (len = inputStream.read(readBuffer) != -1) System.out.println(实时反馈:+new String(readBuffer, 0, len).trim()+new Date();test += new String(readBuffer, 0, len).trim();break; catch (IOException e) e.printStackTrace();/* * 关闭串口 */public static void closeSerialPort() uniqueInstance.serialPort.close();/向串口发送信息方法private void sendMsg()String information = ATr;/要发送的内容try outputStream.writ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论