简体   繁体   中英

javax.comm: Error when device is disconnected

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!

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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