RXTXcomm 串口通信
虚拟串口和串口调试助手的使用
-
虚拟串口软件:
VSPD
,https://www.eltima.com/cn/products/vspdxp/-
VSPD会自动识别出本台计算上有几个物理串口,例如本机只有一个物理串口COM1。在右侧端口管理的分页中,添加虚拟端口。虚拟端口是成对出现的,如COM2和COM3,其编号由VSPD自动检测本地物理串口资源后,自动为虚拟串口排号。单击“Add pair”按钮为计算机添加虚拟串口组对。
为何是组对出现,COM2->COM3和COM3->COM2,串口通信是异步的,允许发送数据的同时接收数据,数据流向是双向的。组对出现我们可以通过串口调试助手分别从两端进行调试。
-
-
串口调试助手:
COMHelper
,需要可留言私信你滴邮箱,打包发你。- 开箱即用,开启串口,点击即用。
使用效果如下:
Java RXTXcomm 实现串口通信并调试
-
下载RXTXcomm
- RXTXcomm提供了 Windows x64, x86, ia64 and Linux x86, x86_64等操作系统支持。http://fizzed.com/oss/rxtx-for-java
-
根据机器配置解压缩文件夹,复制相关Jar包及DLL文件
RXTXcomm.jar
-><JAVA_HOME>\jre\lib\ext
rxtxSerial.dll
-><JAVA_HOME>\jre\bin
rxtxParallel.dll
-><JAVA_HOME>\jre\bin
-
将Jar包添加到项目lib中,IDE使用的是
IDEA
,项目构建方式为Maven
-
创建
SerialPortDataHandle
类,继承Thread
类,并实现SerialPortEventListener
接口,具体代码如下:public class SerialPortDataHandle extends Thread implements SerialPortEventListener {private static final Logger logger = LoggerFactory.getLogger(SerialPortDataHandle.class);// 通讯端口管理,控制对通信端口的访问的中心类static CommPortIdentifier portManager;// 有效连接上的端口的枚举static Enumeration<?> portList;// 串口输入流引用static InputStream inputStream;// 串口输出流引用static OutputStream outputStream;// 串口对象引用static SerialPort serialPort;// 堵塞队列:用来存放串口发送到服务端的数据private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<>();// 线程控制标识private boolean flag = true;@Overridepublic void serialEvent(SerialPortEvent event) {switch (event.getEventType()) {/** SerialPortEvent.BI:/*Break interrupt,通讯中断* SerialPortEvent.OE:/*Overrun error,溢位错误* SerialPortEvent.FE:/*Framing error,传帧错误* SerialPortEvent.PE:/*Parity error,校验错误* SerialPortEvent.CD:/*Carrier detect,载波检测* SerialPortEvent.CTS:/*Clear to send,清除发送* SerialPortEvent.DSR:/*Data set ready,数据设备就绪* SerialPortEvent.RI:/*Ring indicator,响铃指示* SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*Output buffer is empty,输出缓冲区清空*/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:// 数据接收缓冲容器byte[] readBuffer = new byte[200];try {// 存储待接收读取字节数大小int numBytes = 0;while (inputStream.available() > 0) {numBytes = inputStream.read(readBuffer);if (numBytes > 0) {msgQueue.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 收到的串口发送数据为:"+ new String(readBuffer));// 数据接收缓冲容器清空初始化readBuffer = new byte[200];}}} catch (IOException e) {logger.error("IO异常", e);}break;}}public int init() {// 通过串口通信管理类获得当前连接上的端口列表//(获取一个枚举对象,该CommPortIdentifier对象包含系统中每个端口的对象集[串口、并口])portList = CommPortIdentifier.getPortIdentifiers();while (portList.hasMoreElements()) {// 获取相应串口对象portManager = (CommPortIdentifier) portList.nextElement();/** 判断端口类型是否为串口* PORT_SERIAL = 1; 【串口】* PORT_PARALLEL = 2; 【并口】* PORT_I2C = 3; 【I2C】* PORT_RS485 = 4; 【RS485】* PORT_RAW = 5; 【RAW】*/if (portManager.getPortType() == CommPortIdentifier.PORT_SERIAL) {logger.info("串口设备名称:" + portManager.getName());// 判断模拟COM4串口存在,就打开该串口if (portManager.getName().equals("COM4")) {logger.info("测试串口设备名称:" + portManager.getName());try {if (Objects.isNull(serialPort)) {// 打开串口,设置名字为COM_4(自定义),延迟阻塞时等待3000毫秒(赋值给预设的串口引用)serialPort = (SerialPort)portManager.open("COM4", 3000);logger.info("串口设备COM4已打开");}} catch (PortInUseException e) {logger.error("串口使用异常", e);return 0;}// 在串口引用不为空时进行下述操作if (Objects.nonNull(serialPort)) {// 1. 设置串口的输入输出流引用try {inputStream = serialPort.getInputStream();outputStream = serialPort.getOutputStream();} catch (IOException e) {logger.error("串口输入输出IO异常", e);return 0;}// 2. 设置串口监听器try {serialPort.addEventListener(this);} catch (TooManyListenersException e) {logger.error("串口监听器添加异常", e);return 0;}// 设置监听器在有数据时通知生效serialPort.notifyOnDataAvailable(true);// 3. 设置串口相关读写参数try {// 比特率、数据位、停止位、校验位serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);} catch (UnsupportedCommOperationException e) {logger.error("串口设置操作异常", e);return 0;}return 1;}return 0;}}}return 0;}@Overridepublic void run() {try {logger.info("串口线程已运行");while (flag) {// 如果堵塞队列中存在数据就将其输出if (msgQueue.size() > 0) {// take() 取走BlockingQueue里排在首位的对象// 若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止logger.info(msgQueue.take());}}} catch (InterruptedException e) {logger.error("线程执行异常", e);}}public void stopGetDataBySerialPort() {this.flag = false;}public static void main(String[] args) {SerialPortDataHandle handle = new SerialPortDataHandle();int i = handle.init();if (i == 1) {// 线程启动handle.start();}} }
-
启动线程并测试(线程会占用并开启COM4串口,我们使用COM5串口进行通信测试,执行
main
方法前要从串口调试工具中关闭COM4串口,不然会出现gnu.io.PortInUseException: Unknown Application
异常)-
执行
main
方法,启动线程15:34:59.083 [main] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 串口设备名称:COM1 15:34:59.089 [main] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 串口设备名称:COM2 15:34:59.089 [main] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 串口设备名称:COM3 15:34:59.089 [main] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 串口设备名称:COM4 15:34:59.089 [main] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 测试串口设备名称:COM4 15:34:59.102 [main] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 串口设备COM4已打开 15:34:59.103 [Thread-0] INFO com.jeesite.modules.hp.serialport.SerialPortDataHandle - 串口线程已运行
-
通过串口调试工具进行数据发送,查看服务端控制台输出信息
-
查看虚拟端口状态
-