javax.comm:设备断开连接时出错
我有一个使用 javax.comm 从 com 端口读取数据的应用程序。
我遇到的问题是,如果我正在读取的设备意外断开连接,我会在控制台中收到一条错误消息,显示“WaitCommEvent:错误 5”,
我环顾四周,找不到任何有关此问题的有用信息。 我已将所有 notificationOn* 方法设置为 true,因此我认为我应该接收所有事件,但我没有捕获此事件。
打印出来的错误消息不是来自我的代码中的任何位置,因此它必须位于 javax.comm 包中的某个位置。 谁能告诉我如何处理此错误,以便我可以在发生此错误时正确关闭 com 端口?
谢谢!
I have an application that reads data from a com port using javax.comm.
The problem I am having is that if the device that I'm reading from is unexpectedly disconnected I get an error in the console that says "WaitCommEvent: Error 5"
I have looked around and can't find any helpful information about this. I have set all of the notifyOn* methods to true so I think I should be receiving all of the events but I'm not catching this one.
The error message that is printed out does not come from anywhere in my code so it must be in the javax.comm package somewhere. Can anyone tell me how to handle this error so that I can close the com port properly when it occurs?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果有人对此感兴趣,我找到了解决方案。 我使用的是 javax.comm api,但为了解决问题,我将其替换为 rxtx api ( http://rxtx.qbang.org/wiki/index.php/Main_Page)。 不需要更改代码,但现在当设备断开连接时,我收到一个 IOException ,其中包含消息“底层输入流返回零字节”。 我处理该消息并启动一个新线程。 在新线程中,我调用 port.close() 和 port.removeEventListener()。 这两个调用必须在新线程中完成,否则应用程序将挂起。
IF anyone is interested in this, I found a solution. I was using the javax.comm api but to solve the problem I replaced it with rxtx api (http://rxtx.qbang.org/wiki/index.php/Main_Page). No code changes were needed but now when the device is disconnected I receive an IOException with the message "Underlying input stream returned zero bytes". I handle that message and kick off a new thread. In the new thread I call port.close() and port.removeEventListener(). These two calls must be done in a new thread or the application will hang.
我们花了很多时间让 javax.comm 正常工作,发现在我们的例子中是 COM 驱动程序导致了问题(在几台 Windows 7 计算机上)。
如果驱动程序也是您的问题,并且您使用的是 Windows 7 或可能 Vista(并且您使用的是 Prolific/Logilink 串行电缆),那么这可能是解决方案:)
http://www.akosta.dk/category/technical-notes/
希望您让它发挥作用!
We've been using a lot of time on getting javax.comm to work and found that in our case it was the COM driver which was causing the problems (on several Windows 7 computers).
If the driver is also your problem and you're using Windows 7 or possibly Vista (and you're using a Prolific/Logilink seriel cable) then this might be the solution :)
http://www.akosta.dk/category/technical-notes/
Hope you get it to work!
这个问题已经有近 2 年历史了,但也许另一个问题需要一个可能的解决方案:
我在 java.comm 上遇到了同样的问题:“WaitCommEvent:错误 5”
如果 COM 端口断开连接,则此消息会多次出现,
但 SerialPort 对象未关闭。
我的解决方案是,如果读取/写入 SerialPort 的 IO 流抛出异常,则关闭 SerialPort 对象。
This question is nearly 2 years old, but maybe another one needs a possible solution:
I had the same problem with java.comm: "WaitCommEvent: Error 5"
This message appears multiple times, if the COM port is disconnected,
but the SerialPort object is not closed.
My solution was to close the SerialPort object if reading/writing of the IO streams of the SerialPort throwed an exception.