断开连接时 Rxtxcomm 错误

发布于 2024-11-18 05:56:33 字数 825 浏览 3 评论 0原文

我正在使用 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.

I am working with a COM port .
I successfully opens the COM port and do all the work. While closing the COM port a crash happens.
The code is

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

Error

#
> # 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁许谁一生繁华 2024-11-25 05:56:33

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

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

当调用断开连接函数时,我使用互斥锁来更改 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

I've managed to find a solution which appears to resolve the problem - it seems as if the Input and OutputStreams are not completely being closed before the serial port is, as these are running in their own threads and are getting caught up in their own data. This is resolved by adding synchronized mutexes which ensure these threads have properly cleaned up before the serial port is closed.

In my SerialConnection class I added boolean fields, stopRead and stopWrite, to indicate when the InputStream and OutputStream threads respectively should stop listening. I also added mutexes stopReadMutex and stopWriteMutex to synchronize these values among the main and read/write threads. Each iteration of their loops, the reader and writer check the stopRead and stopWrite booleans and break their loops.

When my disconnect function is called, I use the mutexes to change the stopRead and stopWrite values before I call the close functions of the respective threads and serialPort, as shown here:

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

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

Also, here is a link to the associated source code if anyone would like to take a closer look.
http://pastebin.com/C4Fy8mLZ

梦行七里 2024-11-25 05:56:33

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

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

The error is due to conflicting threads. The best way to avoid this, is to make all methods synchronized:

public synchronized void disconnect() {
  serialPort.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文