简体   繁体   English

javax.comm:设备断开连接时出错

[英]javax.comm: Error when device is disconnected

I have an application that reads data from a com port using javax.comm. 我有一个使用javax.comm从com端口读取数据的应用程序。

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" 我遇到的问题是,如果我正在从中读取的设备意外断开连接,则控制台中将显示错误消息“ 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. 我已经将所有notifyOn *方法都设置为true,所以我认为我应该接收所有事件,但是我没有抓住这一事件。

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. 打印出的错误消息不是来自我的代码中的任何地方,因此它必须位于javax.comm包中的某个位置。 Can anyone tell me how to handle this error so that I can close the com port properly when it occurs? 谁能告诉我如何处理此错误,以便在发生com端口时可以正确关闭它?

Thanks! 谢谢!

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). 我们已经花了很多时间来使javax.comm正常工作,并发现在我们的案例中是COM驱动程序导致了问题(在多台Windows 7计算机上)。 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/ 如果驱动程序也是您的问题,并且您使用的是Windows 7或Vista(并且使用的是Prolific / Logilink seriel电缆),则可能是解决方法:) http://www.akosta.dk/category/技术-NOTES /

Hope you get it to work! 希望您能正常工作!

This question is nearly 2 years old, but maybe another one needs a possible solution: 这个问题已经存在了将近2年,但也许另一个问题需要一个可能的解决方案:

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. 我在java.comm中遇到了同样的问题:“ WaitCommEvent:错误5”如果COM端口断开连接,但SerialPort对象没有关闭,则此消息会多次出现。

My solution was to close the SerialPort object if reading/writing of the IO streams of the SerialPort throwed an exception. 我的解决方案是,如果对SerialPort的IO流的读/写操作引发异常,则关闭SerialPort对象。

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 ). 我使用了javax.comm api,但是为了解决该问题,我将其替换为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". 无需更改代码,但现在断开设备连接后,我收到一条IOException消息,消息为“底层输入流返回零字节”。 I handle that message and kick off a new thread. 我处理该消息并启动新线程。 In the new thread I call port.close() and port.removeEventListener(). 在新线程中,我调用port.close()和port.removeEventListener()。 These two calls must be done in a new thread or the application will hang. 这两个调用必须在新线程中完成,否则应用程序将挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM