2

i have a USB phone attached to my computer

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

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

1 Answers1

1

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.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • mikael thank you very much for this. can you please tell me plainly how would i use this? – Alex Gordon Jun 13 '10 at 18:31
  • perhaps you might be able to assist with this as wel http://stackoverflow.com/questions/3033240/c-threading-and-getting-com-port-of-attached-phone – Alex Gordon Jun 13 '10 at 18:34
  • If you run the sample you should in theory get an ID string from the COM port which is assigned to your phone. I can't test it myself as I don't have tethering support on my current phone. – Mikael Svenson Jun 13 '10 at 19:34
  • I also wrote something on your other SO q. – Mikael Svenson Jun 13 '10 at 19:50
  • hey mikael im sorry to bother you gain but could you please incorporate this code into the other code example that you did forme – Alex Gordon Jun 13 '10 at 22:47
  • What are you doing?? You are writing to a port before you know it is yours! This can launch a nuclear missile. – Pavel Radzivilovsky Jun 15 '10 at 04:45
  • @Pavel, Like I said, it's a minimal example. And if it's already open, the .Open() will throw an UnauthorizedAccessException. And feel free to post some insight on this. No missile launched quite yet. – Mikael Svenson Jun 15 '10 at 05:45