查找有关通过USB在C#中连接所有串行设备信息 [英] Finding information about all serial devices connected through USB in C#

查看:874
本文介绍了查找有关通过USB在C#中连接所有串行设备信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目需要特定设备的检测,当它连接到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?

推荐答案

您可以使用下面的查询列出所有串口,你也看到devicemanager:

How to list all serial ports:

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芯片,主板上的端口,一个扩展卡或由一些调制解调器驱动器(即周游世界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等),您可以检查设备ID的类型(看看的设备ID的第一部分)。在那里,你也可以提取BT-MAC地址(要小心的是:设备ID看起来不一样,至少在Windows 7和Windows XP)。

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.

这篇关于查找有关通过USB在C#中连接所有串行设备信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆