简体   繁体   English

给定COM端口,如何找到USB串行适配器的序列号?

[英]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).我想从 Windows COM 端口名称 ( COM11 ) 获取提供该 COM 端口的 USB 设备的序列号(如果这很重要,我目前正在使用 FTDI 适配器)。

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.我可以使用SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, NULL, DIGCF_PRESENT)枚举系统上的端口,然后我可以使用SetupDiEnumDeviceInfoSetupDiGetDeviceRegistryProperty从硬件 ID 中获取 vid/pid。

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).目标是使用序列号对串行适配器进行编程,这些序列号标识了它们在我们已有的模拟器程序中的用途(因此用户不必弄清楚哪个适配器是哪个 COM 端口)。

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] .您可以尝试使用SetupDiGetDeviceInstanceId来获取 USB 设备的设备实例 ID,该 ID 应为USB\\VID_xxxx&PID_xxxx\\[ID]形式的字符串。 If your USB device is not composite, then [ID] would be the serial number.如果您的 USB 设备不是复合设备,则[ID]将是序列号。 This is how libusbp obtains the serial number of a USB device.这就是libusbp获取 USB 设备序列号的方式。

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.如果您的设备是复合设备,则串行端口将是一个子设备,您必须上一层才能找到代表实际 USB 设备的父设备,它应该具有您关心的设备实例 ID。

Before writing any code, I'd recommend checking the Device Instance ID in your Device Manager, where it is known as "Device Instance Path".在编写任何代码之前,我建议您检查设备管理器中的设备实例 ID,它被称为“设备实例路径”。

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:为了让您更好地从包含其他信息的字符串中提取序列号,Microsoft 在此处记录了设备实例 ID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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