Java serial I / O: handling USB serial connection / disconnection in reliable mode

I use rxtx for serial I / O processing in Java with the FTDI2232H, which provides a USB communication port. It works fine, with one exception: if I disconnect the USB cable so that the COM port disappears at runtime, it throws the exceptions left and right:

java.io.IOException: No error in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1427)
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1339)

and when I plug in the cable again, it does not recover.

Is there a way to get rxtx to work properly with connecting / disconnecting a USB communication port? (I tried to send a message to the rxtx mailing list, but for some strange reason, I cannot send messages, although I subscribed to this list. I sent an email to the list administrator and did not receive a response.)

If not, is there another input / output system?

+3
source share
2 answers

According to him, I do not know if this is possible in Java, but there is a Windows message that is broadcasting - WM_DEVICECHANGE , probably you need to intercept this message, and if it leaves the device, gracefully turn off the rxtx code. Here is an example code for checking insert / remove a CD into a CDROM drive (this is not in Java).

Edit: Added another link here that may interest you.

Hope this helps, Regards, Tom.

0
source

. WM_DEVICECHANGE ++ DLL, JNI JAva..

0

Source: https://habr.com/ru/post/1730118/


All Articles