views:

919

answers:

3

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?

A: 

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.

sipwiz
+3  A: 

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

Sesh
A: 

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.

dbasnett