Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 549
sebo Avatar asked Jul 12 '12 19:07

sebo


People also ask

How do I know which serial port is connected?

Verify your console cable is connected to your router, using the console port on your router. Verify your router is connected to your laptop. Navigate to Applications > Utilities > Terminal. You should see a device with “serial” or “Serial” in the name and possibly the model number of the USB serial adapter being used.


2 Answers

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.

like image 67
AlexS Avatar answered Sep 28 '22 08:09

AlexS


I think i see what you are trying to do, look at this code made using WMICodeCreator ( link to WMICodeCreator http://www.microsoft.com/en-us/download/details.aspx?id=8572 ) from this article http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto

//Below is code pasted from WMICodeCreator try {     ManagementObjectSearcher searcher =         new ManagementObjectSearcher("root\\WMI",         "SELECT * FROM MSSerial_PortName");      foreach (ManagementObject queryObj in searcher.Get())     {         Console.WriteLine("-----------------------------------");         Console.WriteLine("MSSerial_PortName instance");         Console.WriteLine("-----------------------------------");         Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);          Console.WriteLine("-----------------------------------");         Console.WriteLine("MSSerial_PortName instance");         Console.WriteLine("-----------------------------------");         Console.WriteLine("PortName: {0}", queryObj["PortName"]);          //If the serial port's instance name contains USB          //it must be a USB to serial device         if (queryObj["InstanceName"].ToString().Contains("USB"))         {             Console.WriteLine(queryObj["PortName"] + "              is a USB to SERIAL adapter/converter");         }     } } catch (ManagementException e) {     MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); }  
like image 31
Jay Avatar answered Sep 28 '22 07:09

Jay