我正在使用 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.
最佳答案
我设法找到了一个似乎可以解决问题的解决方案 - 似乎输入和输出流在串行端口之前没有完全关闭,因为它们在自己的线程中运行并陷入自己的数据中。通过添加同步互斥体可以解决此问题,确保这些线程在串行端口关闭之前已正确清理。
在我的 SerialConnection 类中,我添加了 boolean 字段 stopRead 和 stopWrite,以指示 InputStream 和 OutputStream 线程分别何时应停止监听。我还添加了互斥体 stopReadMutex 和 stopWriteMutex 以在主线程和读/写线程之间同步这些值。每次循环迭代时,读取器和写入器都会检查 stopRead 和 stopWrite boolean 值并中断循环。
当调用断开连接函数时,我使用互斥锁来更改 stopRead 和 stopWrite 值,然后再调用相应线程和串行端口的关闭函数,如下所示:
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
关于java - 断开连接时 Rxtxcomm 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577948/