码农不专业 2015-12-25 02:57 采纳率: 0%
浏览 4581
已结题

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:\Users\Jack\workspace\TestProject\hs_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);
 }

现在的做法是每次有串口需要变动时,都先关闭所有串口,再开启需要用到的多个目标串口,但这样灵活性不高,有谁知道如果解决该问题的,望指点下,谢谢!

  • 写回答

3条回答 默认 最新

  • ioufev 2019-05-27 22:29
    关注

    两种方法

    1.借助空闲串口规避缺陷

    • 先打开一个串口,比如COM1,程序启动时打开COM1,程序关闭后关闭COM1,期间其他串口打开关闭都不会引起崩溃了。这个COM1口是要空闲的。

    2.使用rxtx的纯Java实现库purejavacomm

    • 代码不变,引用的库改一下即可。
    评论

报告相同问题?

悬赏问题

  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?