糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 通过JAVA与串口(RS232)通信实例

通过JAVA与串口(RS232)通信实例

时间:2021-08-26 11:41:42

相关推荐

通过JAVA与串口(RS232)通信实例

通过JAVA与串口(RS232)通信实例

博客分类:J2SE JavaVBthread 最近了解到的需求是需要需激光打刻机进行(RS232)串口通信,

这里使用的是RXTX开源包实现的。

之前并没有用java做过串口通信,而且这方面资料不是很多。

项目实际应用中可能会采用VB开发(这个我就不会了)

只不过用java尝试一下,记个笔记,希望可以对相关开发用些帮助。

下面是实现代码

Java代码 packagetest; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.io.OutputStream; importjava.util.Date; importjava.util.Enumeration; importjava.util.TooManyListenersException; mPortIdentifier; importgnu.io.PortInUseException; importgnu.io.SerialPort; importgnu.io.SerialPortEvent; importgnu.io.SerialPortEventListener; importgnu.io.UnsupportedCommOperationException; publicclassCommUtilimplementsSerialPortEventListener{ InputStreaminputStream;//从串口来的输入流 OutputStreamoutputStream;//向串口输出的流 SerialPortserialPort;//串口的引用 CommPortIdentifierportId; publicCommUtil(EnumerationportList,Stringname){ while(portList.hasMoreElements()){ CommPortIdentifiertemp=(CommPortIdentifier)portList.nextElement(); if(temp.getPortType()==CommPortIdentifier.PORT_SERIAL){//判断如果端口类型是串口 if(temp.getName().equals(name)){//判断如果端口已经启动就连接 portId=temp; } } } try{ serialPort=(SerialPort)portId.open("My"+name,2000); }catch(PortInUseExceptione){ } try{ inputStream=serialPort.getInputStream(); outputStream=serialPort.getOutputStream(); }catch(IOExceptione){ } try{ serialPort.addEventListener(this);//给当前串口天加一个监听器 }catch(TooManyListenersExceptione){ } serialPort.notifyOnDataAvailable(true);//当有数据时通知 try{ serialPort.setSerialPortParams(2400,SerialPort.DATABITS_8,//设置串口读写参数 SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); }catch(UnsupportedCommOperationExceptione){ } } 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://当有可用数据时读取数据,并且给串口返回数据 byte[]readBuffer=newbyte[20]; try{ while(inputStream.available()>0){ System.out.println(inputStream.available()); intnumBytes=inputStream.read(readBuffer); System.out.println(numBytes); } System.out.println(newString(readBuffer).trim()); }catch(IOExceptione){ e.printStackTrace(); } break; } } publicvoidsend(Stringcontent){ try{ outputStream.write(content.getBytes()); }catch(IOExceptione){ e.printStackTrace(); } } publicvoidClosePort(){ if(serialPort!=null){ serialPort.close(); } } }

测试

Java代码 packagetest; mPortIdentifier; importjava.util.Enumeration; publicclassTest{ publicstaticvoidmain(String[]args)throwsInterruptedException{ EnumerationportList=CommPortIdentifier.getPortIdentifiers();//得到当前连接上的端口 CommUtilcomm3=newCommUtil(portList,"COM3"); inti=0; while(i<5) { Thread.sleep(3000); comm3.send("hello"); i++; } comm3.ClosePort(); } } rxtx-2.1-7-bins-r2.zip(596 KB)

如果觉得《通过JAVA与串口(RS232)通信实例》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。