Java串口操作RXTX

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Java串口操作RXTXRXTX是个提供串口和并口通信的开源Java类库。该项目提供了Windows,Linux,MacosX,Solaris操作系统下的兼容javax.comm串口通讯包API的实现。一、使用准备1.Windows①将rxtxParallel.dll、rxtxSerial.dll拷贝到%JAVA_HOME%安装目录bin目录下②将RXTXcomm.jar拷贝到%JAVA_HOME%\jre\lib\ext\RXTXcomm.jar2.Linux①复制librxtxSerial.so,librxtxParallel.so到%JAVA_HOME%/lib/$(ARCH)/②复制RXTXcomm.jar到%JAVA_HOME%/ext③定义驱动类后将javax.comm.properties放在应用程式的根目录下二、操作类注:串口操作类一定要继承SerialPortEventListenerpublicclassSerialPortUtilityNewimplementsSerialPortEventListener{//检测系统中可用的通讯端口类privateCommPortIdentifierportId;//Enumeration为枚举型类,在util中privateEnumerationCommPortIdentifierportList;//输入输出流privateInputStreaminputStream;privateOutputStreamoutputStream;//RS-232的串行口privateSerialPortserialPort;publicstaticStringtest=;//保存串口返回信息privatestaticSerialPortUtilityNewuniqueInstance;//单例创建//初始化串口publicvoidinit(){//获取系统中所有的通讯端口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(newSerialPortUtilityNew());//设置可监听serialPort.notifyOnDataAvailable(true);/*设置串口通讯参数*///波特率,数据位,停止位和校验方式//波特率2400,偶校验serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);test=;outputStream=serialPort.getOutputStream();}catch(PortInUseExceptione){e.printStackTrace();}catch(TooManyListenersExceptione){e.printStackTrace();}catch(UnsupportedCommOperationExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}}}/***实现接口SerialPortEventListener中的方法读取从串口中接收的数据*/publicvoidserialEvent(SerialPortEventevent){switch(event.getEventType()){caseSerialPortEvent.BI:caseSerialPortEvent.OE:caseSerialPortEvent.FE:caseSerialPortEvent.PE:caseSerialPortEvent.CD:caseSerialPortEvent.CTS:caseSerialPortEvent.DSR:caseSerialPortEvent.RI:caseSerialPortEvent.OUTPUT_BUFFER_EMPTY:break;caseSerialPortEvent.DATA_AVAILABLE://获取到串口返回信息readComm();break;default:break;}}//读取串口返回信息publicvoidreadComm(){byte[]readBuffer=newbyte[1024];try{inputStream=uniqueInstance.serialPort.getInputStream();//从线路上读取数据流intlen=0;while((len=inputStream.read(readBuffer))!=-1){System.out.println(实时反馈:+newString(readBuffer,0,len).trim()+newDate());test+=newString(readBuffer,0,len).trim();break;}}catch(IOExceptione){e.printStackTrace();}}/***关闭串口*/publicstaticvoidcloseSerialPort(){uniqueInstance.serialPort.close();}//向串口发送信息方法privatevoidsendMsg(){Stringinformation=AT\r;//要发送的内容try{outputStream.write(information.getBytes());inputStream=serialPort.getInputStream();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功