Search code examples
c#c++winapiserial-numbercom-port

com port identification by device serial number


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++ or 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();
            
        }

Solution

  • I found a working code check this post Getting Serial Port Information

    Code:

    using System.Management;
    using Microsoft.Win32;
    
    using (ManagementClass i_Entity = new ManagementClass("Win32_PnPEntity"))
    {
        foreach (ManagementObject i_Inst in i_Entity.GetInstances())
        {
            Object o_Guid = i_Inst.GetPropertyValue("ClassGuid");
            if (o_Guid == null || o_Guid.ToString().ToUpper() != "{4D36E978-E325-11CE-BFC1-08002BE10318}")
                continue; // Skip all devices except device class "PORTS"
    
            String s_Caption  = i_Inst.GetPropertyValue("Caption")     .ToString();
            String s_Manufact = i_Inst.GetPropertyValue("Manufacturer").ToString();
            String s_DeviceID = i_Inst.GetPropertyValue("PnpDeviceID") .ToString();
            String s_RegPath  = "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Enum\\" + s_DeviceID + "\\Device Parameters";
            String s_PortName = Registry.GetValue(s_RegPath, "PortName", "").ToString();
    
            int s32_Pos = s_Caption.IndexOf(" (COM");
            if (s32_Pos > 0) // remove COM port from description
                s_Caption = s_Caption.Substring(0, s32_Pos);
    
            Console.WriteLine("Port Name:    " + s_PortName);
            Console.WriteLine("Description:  " + s_Caption);
            Console.WriteLine("Manufacturer: " + s_Manufact);
            Console.WriteLine("Device ID:    " + s_DeviceID);
            Console.WriteLine("-----------------------------------");
        }
        Console.ReadLine();
    }
    

    Console output:

    Port Name:    COM29
    Description:  CDC Interface (Virtual COM Port) for USB Debug
    Manufacturer: GHI Electronics, LLC
    Device ID:    USB\VID_1B9F&PID_F003&MI_01\6&3009671A&0&0001
    -----------------------------------
    Port Name:    COM28
    Description:  Teensy USB Serial
    Manufacturer: PJRC.COM, LLC.
    Device ID:    USB\VID_16C0&PID_0483\1256310
    -----------------------------------
    Port Name:    COM25
    Description:  USB-SERIAL CH340
    Manufacturer: wch.cn
    Device ID:    USB\VID_1A86&PID_7523\5&2499667D&0&3
    -----------------------------------
    Port Name:    COM26
    Description:  Prolific USB-to-Serial Comm Port
    Manufacturer: Prolific
    Device ID:    USB\VID_067B&PID_2303\5&2499667D&0&4
    -----------------------------------
    Port Name:    COM1
    Description:  Comunications Port
    Manufacturer: (Standard port types)
    Device ID:    ACPI\PNP0501\1
    -----------------------------------
    Port Name:    COM999
    Description:  EPSON TM Virtual Port Driver
    Manufacturer: EPSON
    Device ID:    ROOT\PORTS\0000
    -----------------------------------
    Port Name:    COM20
    Description:  EPSON COM Emulation USB Port
    Manufacturer: EPSON
    Device ID:    ROOT\PORTS\0001
    -----------------------------------
    Port Name:    COM8
    Description:  Standard Serial over Bluetooth link
    Manufacturer: Microsoft
    Device ID:    BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&000F\8&3ADBDF90&0&001DA568988B_C00000000
    -----------------------------------
    Port Name:    COM9
    Description:  Standard Serial over Bluetooth link
    Manufacturer: Microsoft
    Device ID:    BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\8&3ADBDF90&0&000000000000_00000002
    -----------------------------------
    Port Name:    COM30
    Description:  Arduino Uno
    Manufacturer: Arduino LLC (www.arduino.cc)
    Device ID:    USB\VID_2341&PID_0001\74132343530351F03132
    -----------------------------------