views:

247

answers:

1

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!

+1  A: 

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.