0

I've developed a simple windows form application in c# which is used to send and receive data using serial ports. but when I click my READ button while there is no device connected, the whole application stops working an I have to close it with force! so I need a method to recognize whether a device is connected or not before I click on READ button. please help me get through it.

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            textBox2.Text = serialPort1.ReadLine();
        }
        catch (TimeoutException)
        {
            textBox2.Text = "Timeout Exception";
        }
    }
PARADOX
  • 11
  • 2
  • use try catch blocks to avoid crashing – Efe Aug 11 '17 at 18:30
  • @Efe he didnt mention exceptions - his application just gets unresponsive – Rand Random Aug 11 '17 at 18:31
  • Possible duplicate of [C# Serial Port Check if Device is Connected](https://stackoverflow.com/questions/17595992/c-sharp-serial-port-check-if-device-is-connected) – Rand Random Aug 11 '17 at 18:33
  • @Rand yes. It gets unresponsive when he tries to send data to serial port while there is no device connected and this throws exception. – Efe Aug 11 '17 at 18:37
  • Serial ports are very simple devices, they don't have a formal "connected" state. Maybe the DsrHolding property can help, but it only indicates that the device is powered-up, not that it is in a state where it will send data. Use the DataReceived event or set the ReadTimeout property. – Hans Passant Aug 11 '17 at 18:43
  • @Efe my application is already working with try catch blocks! – PARADOX Aug 11 '17 at 18:46
  • @HansPassant actually I'm noob with C#. It would be great if you send me the code you mean. – PARADOX Aug 11 '17 at 18:47
  • 1
    One way to stop being a noob is by using the sample code provided by the MSDN library topic. You already know which members you need to look at. – Hans Passant Aug 11 '17 at 18:50
  • Documentation exists for very good reasons. –  Aug 11 '17 at 18:53
  • @HansPassant where can i find these sample codes? – PARADOX Aug 11 '17 at 18:56
  • @PARADOX Please add the piece of code that reads data. If you post code in your question, you will get more chance of better solution. – Efe Aug 11 '17 at 18:58
  • @Efe i just added it to my description. – PARADOX Aug 11 '17 at 19:06
  • Based on @HansPassant comments and this answer : https://stackoverflow.com/a/4269883/7565464 . It seems there is no way to determine the connection state. But you can use a trick to know whether it is connected or not. First, connect the device and calculate the average time it takes to read data from that. By knowing this, you can set ReadTimeout to your serial port, so that you won't wait a long time to realize that device is not connected. check this on ReadTimeout: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readtimeout(v=vs.110).aspx – Efe Aug 11 '17 at 19:27

1 Answers1

-1

In this case I found my way out of it! I just Changed ReadLine to ReadExisting in try block and application didn't stop working even with no device connected! But still no way to find whether a device is connected!

PARADOX
  • 11
  • 2