开发者

c# serialport question

开发者 https://www.devze.com 2023-01-03 13:02 出处:网络
i have a USB phone attached to my computer开发者_运维技巧 how do i detect with the serialport which COM its on?Iterate over all COM ports and try to get an ident from each one. Below is a minimal exa

i have a USB phone attached to my computer

开发者_运维技巧

how do i detect with the serialport which COM its on?


Iterate over all COM ports and try to get an ident from each one. Below is a minimal example which should be expanded upon for better error checking etc.

string[] sPorts = SerialPort.GetPortNames();
foreach(string port in sPorts)
{
   var serialPort = new SerialPort();
   serialPort.PortName =  port;
   serialPort.Open();
   serialPort.WriteLine("ATI"); // this will ask the port to issue an ident string which you can match against
   var message = Console.ReadLine(); // read the response
}

You might be able to use WMI with Win32_SerialPort and Win32_PnPEntity as well. I haven't tried it to see what information it gives back.

0

精彩评论

暂无评论...
验证码 换一张
取 消