简体   繁体   中英

How to find serial number of USB serial adapter, given the COM port?

I want to get from a Windows COM port name ( COM11 ) to the serial number of the USB device that provides that COM port (I'm presently working with FTDI adapters, if that matters).

I can use SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, NULL, DIGCF_PRESENT) to enumerate the ports on the system, then I can use SetupDiEnumDeviceInfo and SetupDiGetDeviceRegistryProperty to get the vid/pid out of the Hardware ID.

But I can't seem to figure out a way to get hold of the serial number.

The objective is to program the serial adapters with serial numbers that identify their usage for a simulator program we've got (so the user doesn't have to figure out which adapter is which COM port).

You can try using SetupDiGetDeviceInstanceId to get the Device Instance ID of USB device, which should be a string of the form USB\\VID_xxxx&PID_xxxx\\[ID] . If your USB device is not composite, then [ID] would be the serial number. This is how libusbp obtains the serial number of a USB device.

If your device is composite, the serial port would be a child device and you would have to go up one level to find the parent that represents the actual USB device, which should have the device instance ID that you care about.

Before writing any code, I'd recommend checking the Device Instance ID in your Device Manager, where it is known as "Device Instance Path".

To make you feel somewhat better about extracting the serial number from a string that has other information, the Device Instance ID is documented by Microsoft here:

https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-instance-ids

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM