问题描述
我的项目需要在连接到 USB 时检测特定设备.我可以识别此设备的唯一方法是通过它的描述/设备名称,而不是 com 端口.我发现执行正确功能的是使用 WMI 查询并检查 name 属性:
My project requires detection of a specific device when it is connected to USB. The only way I can identify this device is by its description/device name, not the com port. What I have found to perform the correct function is using a WMI query and checking the name property:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_SerialPort");
foreach (ManagementObject port in searcher.Get())
{
deviceName = (string)foundPort.GetPropertyValue("Name");
...
我最初通过连接我的手机对此进行了测试,查询返回了在 COM3 上找到的手机,如预期的那样.然后,我连接了另一个设备(一个 USB 到串行转换器,它更类似于我需要这个项目的设备)并且查询根本没有找到它.它只会找到电话.但是,此设备确实显示在设备管理器中的端口 COM4 上.更让我恼火的是,SerialPort 类找到了两个设备,但它没有提供识别设备所需的信息:
I initially tested this by connecting my phone, and the query returned the phone found on COM3 as expected. Then, I connected another device (a USB to serial converter, which more closely resembles the device I need this project for) and the query simply did not find it. It only finds the phone. This device does, however, show up on port COM4 in Device Manager. To spite me even more, the SerialPort class finds both devices, but it does not provide the information I need to identify the device:
string[] tempPorts = SerialPort.GetPortNames();
我已经阅读了关于 SO 和其他地方的大量主题,但找不到令人满意的解决方案.有人可以澄清为什么 WIN32_SerialPort 查询找不到我的其他设备吗?由于某种原因,它不被认为是win32串口吗?而且,有人可以指出解决这个问题的方向吗?
I have read numerous threads on SO and elsewhere and cannot find a satisfactory solution. Could someone please clarify why the WIN32_SerialPort query does not find my other device? Is it not considered a win32 serial port for some reason? And, could someone please point me in the direction of a solution to this problem?
推荐答案
如何列出所有串口:
有几个System-定义的设备设置类可供硬件供应商使用.正确编写的 COM-Ports
驱动程序应使用 Ports (COM & LPT ports)
-class (guid: 4d36e978-e325-11ce-bfc1-08002be10318代码>).可能这个类也被设备管理器使用.
How to list all serial ports:
There are several System-Defined Device Setup Classes available to hardware vendors. Properly written drivers for COM-Ports
should use the Ports (COM & LPT ports)
-class (guid: 4d36e978-e325-11ce-bfc1-08002be10318
). Probably this class is used by the device manager as well.
因此,您可以使用以下查询列出您在设备管理器中也看到的每个串行端口:
So you can use the following query to list every serial port you also see in the devicemanager:
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"root\CIMV2",
"SELECT * FROM Win32_PnPEntity WHERE ClassGuid="{4d36e978-e325-11ce-bfc1-08002be10318}""
);
foreach (ManagementObject queryObj in searcher.Get())
{
// do what you like with the Win32_PnpEntity
}
请参阅Win32_PnPEntity-类.您应该拥有识别设备所需的一切.
See this detailed description of the Win32_PnPEntity-class. You should have everything you need for identifying your device.
为了确定端口号,我检查了 name 属性并将其提取出来.到目前为止,这工作正常,但我不知道端口号是否保证包含在名称中.到目前为止,我还没有找到任何串口设备,它的名称中没有包含端口号.
For determining the port number I examine the name property and extract it. Until now this works fine, but I don't know if the port number is garanteed to be included in the name. I haven't found any serial port device until now, that doesn't have the port number included in the name.
上面的查询找到每一个串口设备,无论是蓝牙SPP、FTDI芯片、主板上的端口、扩展卡还是某些调制解调器驱动程序(即Globetrotter GTM66xxW)生成的虚拟串口.
The above query finds every serial port device, no matter if it is a bluetooth SPP, a FTDI-chip, a port on the mainboard, an extension card or a virtual serial port generated by some modem driver (i.e. Globetrotter GTM66xxW).
要确定连接类型(蓝牙、USB 等),您可以检查 deviceid(查看 deviceid 的第一部分).您还可以在那里提取 bt-mac 地址(请注意:至少在 Windows 7 和 Windows XP 上 deviceid 看起来不同).
To determine the type of connection (bluetooth, usb, etc.) you can examine the deviceid (have a look at the first part of the deviceid). There you can also extract the bt-mac address (be careful with that: the deviceid looks different at least on Windows 7 and Windows XP).
我怀疑这取决于驱动程序的实现,因为我有一些 USB 设备会列出其端口,而另一些则没有.
I suspect it depends on the driver implementation, since I have some usb-devices that get their ports listed and some that don't.
这篇关于在 C# 中查找有关通过 USB 连接的所有串行设备的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!