anat0lius
anat0lius

Reputation: 2265

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

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

Upvotes: 1

Views: 2713

Answers (2)

JVMATL
JVMATL

Reputation: 2122

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.

Upvotes: 1

MrD
MrD

Reputation: 1275

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...).

Upvotes: 2

Related Questions