Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

.net - Finding information about all serial devices connected through USB in C#

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"); 
                ...

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();

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

See this detailed description of the Win32_PnPEntity-class. You should have everything you need for identifying your device.

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.

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).

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).

Regarding why some devices are not listed with Win32_SerialPort:

I suspect it depends on the driver implementation, since I have some usb-devices that get their ports listed and some that don't.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...