开发者

c# serialport question

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜