saranya
saranya

Reputation:

How to properly timeout when communicating with a serial-port device?

I am using a serial port communication C# windows application. I wrote a program to get the data from the port, to manipulate it and write through the serial port.

My question is in case the unit is not powered on, the power supply is disconnected, or the unit is not responding to commands for a long time, how will I know? In my program, I have written for normal flow of execution. But in case there is no response for long time, the program keeps on waiting for a long time. I used a timer but it doesn't work properly.

Can anyone help me please?

Upvotes: 0

Views: 3385

Answers (3)

dbasnett
dbasnett

Reputation: 11773

One thing that might help is if the cable you are using is wired with DTR / DSR crossed, meaning that when your program starts you raise DTR and the other device see's DSR go high, and in turn raises it's DTR, which raises your DSR.

If you are using unknown cables and communicating with devices you don't have control over, then you will have to use .Timeout.

Upvotes: 0

Sesh
Sesh

Reputation: 6182

If you are using the "SerialPort" object in C#, then you can use the ReadTimeOut and WriteTimeOut Properties.

Upvotes: 4

sipsorcery
sipsorcery

Reputation: 30699

You've got two options:

  1. If you have a device which should be generating data you can listen on the serial port for data from and if nothing is received for a set period you can assume it is offline,

  2. Periodically send data to your device over the serial port and if you get an exception then you will know your device is offline.

Upvotes: 0

Related Questions