How to detect disconnection of Serial Port connection with javax.comm?

2.5k Views Asked by At

When a connection of SerialPorts (RS232) is interrupted/disconnected, how could I detect it and report it to the user?

As can happen in any moment, I suppose I should use a separate thread.

My connection type is: send msg - recieve msg

2

There are 2 best solutions below

2
On BEST ANSWER

RS232 does not detect the "state" of an physical connection. So I guess it is not possible to directly get an event in case of connect/disconnect. Probably the only way is to send something and detect if the answer is missing (or hearbeat or whatever...).

0
On

How much control do you have over the hardware or the remote device? You might be able to do something with the flow control lines: (i.e. if you are not using RTS/CTS flow control, but you can make your target device assert/deassert CTS, you might detect a change in CTS when the device is disconnected.) But be warned; this might work fine on one piece of hardware and not work on another due to hardware differences. In the general case, I agree with @MrD above that the most reliable/portable solution is to implement some heartbeat messaging (you send a message, wait for a response, like a TCP/IP PING), and generate a 'disconnect' event if you don't get a response within some timeout.