问题描述
- Java使用rxtx进行多串口开发问题
-
# 我的项目第一版中使用RXTX库进行串口操作(同一时间只涉及单个串口在工作),都没出现过问题。
# 现在项目需要,接入多个串口同时工作,出现了JVM Crash的问题。
现象描述:
程序使用rxtx.jar进行串口通信开发,同时进程多个串口双向通信,
假设使用到的串口名称为COM1和COM2
情况1:
com1开启,com2开启-->OK
com1关闭-->OK
com1第二次开启----->奔溃,提示见附1
情况2:
com1开启,com2开启-->OK
com2关闭-->OK
com2第二次开启--->ok
情况3:
com1开启,com2开启-->OK
com2关闭-->OK
com1第二次开启----->提示串口被占用,正常
情况4:
com1开启,com2开启-->OK
com1关闭-->OK
com2关闭-->OK
com1第二次开启----->OK
个人总结下情况:
使用多个串口,如果有串口要进行第二次开启,且这个串口在机器上扫描出来是第一个串口的话,需先将所有串口都关闭后再执行串口开启,否则就会出现错误。
附1:以下运行出错时提示的内容,具体日志在hs_err_pid9032.log中
# A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000001800071cd, pid=9032, tid=5016 # # JRE version: Java(TM) SE Runtime Environment (7.0_51-b13) (build 1.7.0_51-b13) # Java VM: Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode windows-amd64 compressed oops) # Problematic frame: # C [rxtxSerial.dll+0x71cd] # # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows # # An error report file with more information is saved as: # C:UsersJackworkspaceTestProjecths_err_pid9032.log # # If you would like to submit a bug report, please visit: # http://bugreport.sun.com/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. #
附2:简单测试代码
public static void main(String[] args) throws Exception { CommPortIdentifier com13PI = CommPortIdentifier .getPortIdentifier("COM13"); CommPortIdentifier com1PI = CommPortIdentifier .getPortIdentifier("COM1"); SerialPort com13 = (SerialPort) com13PI.open(main.class.getName(), 400); SerialPort com1 = (SerialPort) com1PI.open(main.class.getName(), 400); com13.close(); //com1.close(); com13PI = CommPortIdentifier.getPortIdentifier("COM13"); com13 = (SerialPort) com13PI.open(main.class.getName(), 400); }
现在的做法是每次有串口需要变动时,都先关闭所有串口,再开启需要用到的多个目标串口,但这样灵活性不高,有谁知道如果解决该问题的,望指点下,谢谢!
解决方案
java 使用rxtx 进行串口交流。
C++Builder中使用MSCOMM进行串口编程
解决方案二:
直接用JVM调C库,是比较容易出现JVM崩溃的问题;为了节约时间成本,最好不要这么做;
可以用ICE来实现Java和C++的相互调用,比较安全
时间: 2022-12-25