LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to identify an USB device (seen as VCOM) both by its hardware location and COM port number?

Good morning,

I need to develop a program in CVI which must be able to inform the user the hardware location (physical connection) of an USB device (seen as VCOM) and its COM port assigned by Windows.

I was able to get the hardware location using SetupDiGetDeviceRegistryProperty with parameters SPDRP_LOCATION_INFORMATION and SPDRP_LOCATION_PATHS: 

For example: Port_#0004.Hub_#0004  |  PCIROOT(0)#PCI(1D00)#USBROOT(0)#USB(1)#USB(4)

The problem is that i do not know how to get its corresponding COM port number assigned to be able to communicate with that specific USB device since there are several similar devices (having equal VID and PID but without Serial Number) connected to a PC at the same time.

I will use a TestStand sequence running 2 threads to communicate to both USB devices (having their hardware locations got as mentioned before assuming the physical connection/wiring will be always fixed) but their COM ports assigned by Windows are required to communicate with them.

I need the existing relationship between COM port assignment and hardware location.

Thanks in advance!

Regards,

Alejandro

0 Kudos
Message 1 of 3
(4,256 Views)

COM ports are assigned in chronological order. So if you plug one after the other, you'll get COM3 then COM4 (assuming you already have 1&2 on your mobo). But on reboot, the order may be different, _but_ it should be stable between reboots. So, turn off, plug them in, reboot, note the order, reboot again, verify it stays the same.

Another surer way would be to simply query which instrument is at the end.

0 Kudos
Message 2 of 3
(4,199 Views)

Thanks for the reply gdargaud

Finally, i searched into the Windows Registry Editor to find the "PortName" of the device of interest connected into an USB port to get the complete path to access this registry programmatically as follows:

sprintf(comPortRegPath, "%s%s%s%s", "SYSTEM\\\\CurrentControlSet\\\\Enum\\\\USB\\\\", USB_DEV_VID_PID, devSerialNumber, "\\\\Device Parameters");

registryAccess = RegOpenKeyEx(HKEY_LOCAL_MACHINE, comPortRegPath, 0, KEY_READ, &registryKey);

usbVCOMportNameSize = sizeof(usbVCOMportName);

registryAccess = RegQueryValueEx(registryKey, "PortName", NULL, NULL, (LPBYTE)usbVCOMportName, &usbVCOMportNameSize);

RegCloseKey(registryKey);

NOTE: devSerialNumber is assigned by Windows since these particular USB devices have no serial number.

Alejandro

 

0 Kudos
Message 3 of 3
(4,187 Views)