简体   繁体   English

如何发现设备是否连接到特定的串行(COM)端口?

[英]How can I discover if a device is connected to a specific serial (COM) port?

How can I discover, using C#'s SerialPort class, whether a device is connected to a specific serial (COM) port? 如何使用C#的SerialPort类发现设备是否连接到特定的串行(COM)端口?

Note: that class's Open method opens the port even if there is no device connected to the port. 注意:即使没有设备连接到该端口,该类的Open方法也会打开该端口。

1.WMI: SELECT * FROM Win32_SerialPort : 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: 2.如果设备将响应发送回: System.IO.Ports.SerialPort.GetPortNames()并发送基本命令:

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();
    }
}

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. 在某些情况下,连接设备时,将引发DSR( SerialPort.DsrHolding )甚至CTS( SerialPort.CtsHolding )。

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. 但是在某些情况下,您可能只连接了Tx / Rx,并且唯一的告诉方法是尝试与设备进行通信。

You need to look at the documentation for your device and its cable. 您需要查看设备及其电缆的文档。

There's no general solution that works for any device. 没有适用于任何设备的通用解决方案。

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. 例如,对于GSM调制解调器,您打开端口并在命令处发送,然后收到ok作为响应。

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. 创建串行端口对象并打开端口,现在在连接设备时,操作系统应发送CDChanged事件。
  2. You ping the serial port, and if you receive a response back, assume it is connected. 您对串行端口执行ping操作,如果收到响应,则假定已连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM