【问题标题】:Rxtxcomm error on disconnecting断开连接时出现 Rxtxcomm 错误
【发布时间】:2011-07-05 04:25:05
【问题描述】:

我正在使用 COM 端口。 我成功打开了 COM 端口并完成了所有工作。关闭 COM 端口时发生崩溃。 代码是

public void close()
    throws GPSException
  {
    if (serial_port_ != null)
      serial_port_.close();
  }

错误

#
> # An unexpected error has been detected by Java Runtime Environment:
> #
> #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x04049e69, pid=5692,
> tid=4100
> #
> # Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode, sharing
> windows-x86)
> # Problematic frame:
> # C  [rxtxSerial.dll+0x9e69]
> #
> # If you would like to submit a bug report, please visit:
> #   http://java.sun.com/webapps/bugreport/crash.jsp
> # The crash happened outside the Java Virtual Machine in native code.
> # See problematic frame for where to report the bug.

【问题讨论】:

  • 您好,错误已解决..感谢大家
  • 我有同样的错误。你是怎么解决的?谢谢!
  • @Deepu,你是如何解决这个错误的?
  • Deepu:能否请您自己发布问题的答案,然后接受该答案?此外,如果他们解决了您的问题,您需要接受之前问题的答案。

标签: java serial-port fatal-error rxtx


【解决方案1】:

我设法找到了一个似乎可以解决问题的解决方案 - 似乎输入和输出流在串行端口之前没有完全关闭,因为它们在自己的线程中运行并且正在被赶上在他们自己的数据中。这可以通过添加同步互斥锁来解决,该互斥锁可确保在关闭串行端口之前正确清理这些线程。

在我的 SerialConnection 类中,我添加了布尔字段 stopRead 和 stopWrite,分别指示 InputStream 和 OutputStream 线程何时应该停止侦听。我还添加了互斥体 stopReadMutex 和 stopWriteMutex 以在主线程和读/写线程之间同步这些值。循环的每次迭代,读取器和写入器都会检查 stopRead 和 stopWrite 布尔值并中断循环。

当我的断开函数被调用时,我使用互斥体来改变 stopRead 和 stopWrite 值,然后再调用各个线程和 serialPort 的关闭函数,如下所示:

public void disconnect(){
    try {
        synchronized(stopReadMutex)
        {stopRead = true;}
        synchronized(stopWriteMutex)
        {stopWrite = true;}

        portOut.close();
        portIn.close();
        serialPort.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

此外,如果有人想仔细查看,这里是相关源代码的链接。 http://pastebin.com/C4Fy8mLZ

【讨论】:

    【解决方案2】:

    错误是由于线程冲突造成的。避免这种情况的最好方法是使所有方法synchronized:

    public synchronized void disconnect() {
      serialPort.close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多