5 Replies - 1782 Views - Last Post: 02 September 2015 - 06:47 AM Rate Topic: -----

#1 kayanialex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-September 15

Virtual Com Port not correct based on VID/PID

Posted 02 September 2015 - 12:23 AM

noise from other site post said:

2
down vote
favorite

I am developing a usb cdc custom application, for getting the com port name based on the vid and pid. I am using below code but it is not returning correct com port name.
Following code outputs (Com4) but device Detected on Com6 and Com4 does not exist.Please Help

public  static List<string> ComPortNames(String VID, String PID)
{
  String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
  Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
  List<string> comports = new List<string>();

  RegistryKey rk1 = Registry.LocalMachine;
  RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");

  foreach (String s3 in rk2.GetSubKeyNames())
  {
    RegistryKey rk3 = rk2.OpenSubKey(s3);
    foreach (String s in rk3.GetSubKeyNames())
    {
      if (_rx.Match(s).Success)
      {
        RegistryKey rk4 = rk3.OpenSubKey(s);
        foreach (String s2 in rk4.GetSubKeyNames())
        {
          RegistryKey rk5 = rk4.OpenSubKey(s2);
          string location = (string)rk5.GetValue("LocationInformation");
          if (location != string.Empty)
          {
            string port = location.Substring(location.IndexOf('#') + 1, 4).TrimStart('0');
            if (port != string.Empty) comports.Add(String.Format("COM{0:####}", port));
          }
          //RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
          //comports.Add((string)rk6.GetValue("PortName"));
        }
      }
    }
  }
  return comports;
}

private void Get_Port_Name_Click(object sender, EventArgs e)
{ 
  var list = ComPortNames("0000", "0400");
}


Cross posted to:
http://stackoverflow...ased-on-vid-pid

This post has been edited by tlhIn`toq: 02 September 2015 - 06:05 AM
Reason for edit:: Cleaned up post. Added [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Virtual Com Port not correct based on VID/PID

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Virtual Com Port not correct based on VID/PID

Posted 02 September 2015 - 02:35 AM

could you add some print statements to the code to display debugging information

are you using Visual Studio ? if so what does GetPortNames() display?
https://msdn.microso...#code-snippet-2

an alternative I use is a loop where I attampt to open each COM port in turn - if open works it exists and is not in use
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Virtual Com Port not correct based on VID/PID

Posted 02 September 2015 - 05:58 AM

I'm just curious how many sites you spammed with this.
Frankly it show a great deal of laziness that you couldn't even be bothered with cleaning up the post to remove the 'noise' you copied from the other site(s)

Quote

2
down vote
favorite

And you couldn't be bothered with actually putting your code in code tags as shown all over this site including the text box you you pasted this in to.

Anyway... You're not really reading the com ports, you're reading registry values. Maybe there is a disconnect there. It doesn't matter if there is no COM4 if you have a registry entry that says there is. Have you tried using RegEdit to look at the values your code is reading? Maybe your code is doing its job just fine but you have registry keys you weren't expecting.

Also you say this is a virtual com port. Is it Bluetooth? Because the Microsoft Bluetooth stack has/had a problem where it failed to null-terminate names. So instead of
COM4\0 like it should be
COM6 NO termination so you get random characters on the back end of the name. such as
COM6
COM7
COM8♥



Beyond that advice all I can suggest is to learn basic debugging techniques.

This looks like a very straight-forward debugging situation.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Virtual Com Port not correct based on VID/PID

Posted 02 September 2015 - 06:13 AM

View Posthorace, on 02 September 2015 - 03:35 AM, said:

an alternative I use is a loop where I attampt to open each COM port in turn - if open works it exists and is not in use

The OP is not just looking for a valid port - but is looking for WHICH port has that specific device on it.

Personally I agree with you... Screw the registry, when possible. Loop through the ports and try to actually connect to the device.

But there could be circumstances where that doesn't work. Maybe he's trying to monitor the port like a watchdog. In that case the port would normally NOT be available because the regular program is using it. But this watchdog keeps polling it trying to connect. If it can connect that would me the real control program crashed, and his watchdog program could then re-start the proper program. That's just one reason I can envision going this route.

One also has to realize that with tighter security on the registry this may not be an option if the user is on a newer OS and doesn't have admin rights.
Was This Post Helpful? 0
  • +
  • -

#5 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Virtual Com Port not correct based on VID/PID

Posted 02 September 2015 - 06:39 AM

View PosttlhIn`toq, on 02 September 2015 - 02:13 PM, said:

The OP is not just looking for a valid port - but is looking for WHICH port has that specific device on it.

Personally I agree with you... Screw the registry, when possible. Loop through the ports and try to actually connect to the device.

I have never attempted to access the registry - if I am looking for a specific device I would open each COM port in turn and probe it to what happens, e.g. a modem would reply to <CR> with "AT" and a microcontroler board with something like "PIC24 Mechatronics board V1.1"

This post has been edited by horace: 02 September 2015 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Virtual Com Port not correct based on VID/PID

Posted 02 September 2015 - 06:47 AM

Yes - If it isn't already in use. But you can't take that approach universally. I already gave one example where that won't work: Where you are watchdogging a device that is supposed to be in use.
I can think of a couple devices that don't reply at all: You have to send the correct code to activate them. So unless you already know what kind of device is supposed to be on that port then you won't actually 'detect' it.
I can think of a couple devices (not the best designed) that will activate when the port is opened. Just opened and a connect is made. Its poor design but its out there. So just probing it is going to activate it.

Sometimes you have to be very very careful, assume the worst case and do no harm.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1