1

My hardware device connects to PC and exposes virtual serial port. The serial port number varies depending on which physical USB port I plug the device into. Is there a way my application can know which physical USB port is my device connected to? This would really help me optimize the code. Currently my application determines the actual port number ever time device is connected by iterating through all possible ports which is a lengthy process. If I know which USB port was it connected, I already know which comm port it exposes and I can save/load these settings for each particular physical USB port (profile). I am using C++/MFC and Windows 7. I would also like this to work in XP.

zar
  • 11,361
  • 14
  • 96
  • 178
  • Your question is definitely OS dependant. From that you say, that you use MFC, I assume, that you are on Windows. But which exact version? – Kai Petzke Aug 12 '13 at 17:06
  • I am using Windows 7 but would also like solution for Win XP if it's different. And right I am using MFC. I have updated question tags to include windows. – zar Aug 12 '13 at 17:33
  • Is there something that "responds" at the other end? If so, I would just try every serial port available in the machine (obviously assuming your compilter is not also connected to a nuclear missile that will fly off if you try to connect to it on its serial port and fail more than X times). I used this technique to find the mobile phones in a system to test mobile phones (because each time I plugged in a different phone, it got a differnt number com-port, so I identified the mobile phone on its identity instead, and used a configuration file for mappign phone ID to more useful names) – Mats Petersson Aug 12 '13 at 18:48
  • @MatsPetersson My situation is slightly difference than yours, I only connect just one proprietary device so I only have to worry about different port numbers it creates for different USB ports. I think you got a little lucky with not ending up same problem if you connect same phone on two different ports! Currently my application polls for all the ports as well but it takes a lot of time. – zar Aug 12 '13 at 20:41
  • 1
    Didn't take long in my case - could do 100 ports (which I believe is the maximum) in a few tenths of a second, which isn't a problem. All phones have unique ID's (IMEI), so not a problem. – Mats Petersson Aug 12 '13 at 20:47
  • The "COM port address changing" is only expected for broken devices. A proper USB device has a serial number, to which a port number is tied. Lacking that, Windows ties port numbers to physical USB ports. – MSalters Aug 13 '13 at 07:51
  • @MatsPetersson That seems very fast in your case. I commented Read/Write operations and just opening and closing 64 ports took a second. My `Read` operations are adding more time, they are not fast at all, with them it takes a little over 3 seconds. I have taken over this code so will also look into optimizing read/write, this should do with COMMTIMEOUTS – zar Aug 13 '13 at 15:00
  • I set a fairly short timeout, because I relied on the device "when it's there, it replies quickly". If you can't rely on that, then you will have longer times. Of course, the other option is to "remember" where your last com-port was, and only "scan" if that port doesn't work in some way. – Mats Petersson Aug 13 '13 at 15:06

1 Answers1

1

The USBView sample source is available in the WDK. If you are unfamiliar with this, simply run it - this tool walks the entire USB tree on the system and prints out information and descriptor listings for each device, as well as locations on the host controllers and hubs.

Start at the RefreshTree() function in this sample, you can then follow the code to see how it enumerates the host controllers, hubs and finally devices.

The easiest way to get the source to this sample is to install the 7.1.0 WDK which is currently available here: http://www.microsoft.com/en-us/download/details.aspx?id=11800

Preston
  • 2,543
  • 1
  • 17
  • 26