Home > Net >  com port identification by device serial number
com port identification by device serial number

Time:02-03

In our company for testing purposes, we use many serial COM port devices. These devices are transferred daily between several PC and whenever we add a new serial device, we have to manualy learn on all computers.

Currently I am working on code, for automatic COM port device detection. My question is, how to list also device ID in c next to the active COM port list?

enter image description here

With serial number, I will be able to automatically detect on what port device is on on each PC. Also for FTDI, Silicon Labs,..USB to RS232 converter I can manualy set device SN. Solution need to work on windows 7 or newer.

For example:

![enter image description here

My code:

       static void Main(string[] args)
        {

            // Get a list of serial port names.
            string[] ports = SerialPort.GetPortNames();

            Console.WriteLine("The following serial ports were found:");

            // Display each port name to the console.
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }

            Console.ReadLine();
            
        }

CodePudding user response:

You can use ManagementObjectSearcher for this. You'll need to add System.Management as a reference.

using System;
using System.Management; // need to add System.Management to your project references

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * From Win32_USBHub");

    foreach (ManagementObject queryObj in searcher.Get())
    {
      Console.WriteLine("USB device");
      Console.WriteLine("Caption: {0}", queryObj["Caption"]);
      Console.WriteLine("Description: {0}", queryObj["Description"]);
      Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
      Console.WriteLine("Name: {0}", queryObj["Name"]);
      Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
    }

CodePudding user response:

If I understood your questions correctly, the C code below should help. On my system I get this result, which seems to match your screenshots:

devId: PCI\VEN_8086&DEV_9D3D&SUBSYS_506D17AA&REV_21\3&11583659&1&B3
devId: FTDIBUS\VID_0403 PID_6001 A916RGSAA\0000
#include <iostream>
#include <string>
#include <windows.h>
#include <cfgmgr32.h>


int main()
{
    char guid_str[] = "{4D36E978-E325-11CE-BFC1-08002BE10318}";
    ULONG len{};
    auto res = CM_Get_Device_ID_List_SizeA(&len, guid_str, CM_GETIDLIST_FILTER_CLASS | CM_GETIDLIST_FILTER_PRESENT);
    if (res != CR_SUCCESS) {
        std::cerr << "error num " << res << "occured\n";
    }
    std::string devIds(len,'\0');
    res = CM_Get_Device_ID_ListA(guid_str, devIds.data(), devIds.length(), CM_GETIDLIST_FILTER_CLASS | CM_GETIDLIST_FILTER_PRESENT);

    size_t pos{ 0 };
    for (;;) {
        auto newpos = devIds.find('\0', pos);
        auto devId = devIds.substr(pos, newpos - pos);
        std::cout << "devId: " << devId << "\n";
        if (devIds.at(newpos   1) == 0) break;
        pos = newpos 1;
    }
}
  • Related