1

How can I discover, using C#'s SerialPort class, whether a device is connected to a specific serial (COM) port?

Note: that class's Open method opens the port even if there is no device connected to the port.

Milind Thakkar
  • 980
  • 2
  • 13
  • 20
Saran
  • 835
  • 3
  • 11
  • 31
  • The DsrHolding property will be true when a device is connected to the port and powered-up. Of course you'll have no idea *what* particular device until you start talking to it. – Hans Passant Oct 10 '12 at 07:30
  • "The DsrHolding property will be true when a device is connected" - may or may not be, depending on the device and the cable. – Joe Oct 10 '12 at 07:44

4 Answers4

3

1.WMI: SELECT * FROM Win32_SerialPort:

ManagementObjectSearcher manObjSearch = new ManagementObjectSearcher("Select * from Win32_SerialPort");
ManagementObjectCollection manObjReturn = manObjSearch.Get();

foreach (ManagementObject manObj in manObjReturn)
{
    //int s = manObj.Properties.Count;
    //foreach (PropertyData d in manObj.Properties)
    //{
    //    Console.WriteLine(d.Name);
    //}
    Console.WriteLine(manObj["DeviceID"].ToString());
    Console.WriteLine(manObj["Name"].ToString());
    Console.WriteLine(manObj["Caption"].ToString());
}

2. If device send response back: System.IO.Ports.SerialPort.GetPortNames() and sending basic command:

foreach (string portname in SerialPort.GetPortNames())
{
    var sp = new SerialPort(portname, 4800, Parity.Odd, 8, StopBits.One);
    try
    {
        sp.Open();
        sp.Write("Your known command to device");
        Thread.Sleep(500);
        string received = sp.ReadLine();

        if (received == "expected response")
        {
            Console.WriteLine("device connected to: " + portname);
            break;
        }
    }
    catch (Exception)
    {
        Console.WriteLine("device NOT connected to: " + portname);
    }
    finally
    {
        sp.Close();
    }
}
Ria
  • 10,237
  • 3
  • 33
  • 60
  • What if the device is connected and will not send any response back. For example Pole display connected to COM port may not send any data back. – Saran Oct 10 '12 at 06:57
2

The answer depends on the device and the cable.

In some cases, DSR (SerialPort.DsrHolding) or even CTS (SerialPort.CtsHolding) will be raised when the device is connected.

But in some cases you may only have Tx / Rx connected, and the only way to tell is to attempt to communicate with the device.

You need to look at the documentation for your device and its cable.

There's no general solution that works for any device.

Joe
  • 122,218
  • 32
  • 205
  • 338
0

You can do it by opening serial port and sending most basic command your device support and check the response. For example for GSM modem you open port and sent at command and receive ok in response.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Adil
  • 146,340
  • 25
  • 209
  • 204
0

Couple of things you can try

  1. Create Serial port object and open a port, now when a device is connected, OS should send CDChanged event.
  2. You ping the serial port, and if you receive a response back, assume it is connected.
Milind Thakkar
  • 980
  • 2
  • 13
  • 20