0

I am very new to embedded programming, I am tryig to get all port names in a list. I guess this is the most basic operation.

using System.IO.Ports;    
string[] ports = SerialPort.GetPortNames();

Not sure where I am going wring with this basic operation, but the string is empty. Any leads to what i am doing wrong would be helpful

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Aashu10
  • 49
  • 10

2 Answers2

0

GetPortNames will only gather valid connected COM ports.

If you are trying to gather the COM port for a disconnected port you will need to search through the registry. Here's how I find the correct device I want based off VID and PID.

public class ComPortFinder
{
    public static List<DeviceInfo> FindConnectedDevices(uint vid, uint pid)
    {
        string pattern = string.Format("^VID_{0:X4}.PID_{1:X4}", vid, pid);
        Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);

        List<DeviceInfo> devices = new List<DeviceInfo>();

        RegistryKey rk1 = Registry.LocalMachine;
        RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");

        foreach (String s3 in rk2.GetSubKeyNames())
        {
            RegistryKey rk3 = rk2.OpenSubKey(s3);
            foreach (String s in rk3.GetSubKeyNames())
            {
                if (_rx.Match(s).Success)
                {
                    RegistryKey rk4 = rk3.OpenSubKey(s);
                    foreach (String s2 in rk4.GetSubKeyNames())
                    {
                        RegistryKey rk5 = rk4.OpenSubKey(s2);
                        RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
                        if (!string.IsNullOrEmpty((string)rk6.GetValue("PortName")))
                        {
                            DeviceInfo di = new DeviceInfo()
                            {
                                VenderId = vid,
                                ProductId = pid,
                                SerialNumber = "UNKNOWN",
                                ComPort = rk6.GetValue("PortName").ToString()
                            };
                            devices.Add(di);
                        }
                    }
                }
            }
        }
        return devices;
    }
}

public struct DeviceInfo
{
    public uint VenderId;
    public uint ProductId;
    public string SerialNumber;
    public string ComPort;
}

I do not take credit for this, as I believe this was stolen from another StackOverflow answer but hopefully it will help.

Damon Earl
  • 324
  • 2
  • 8
-1

According to Juanma's answer you can get all ports by using wmi instruments in here How to find available COM ports?

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

don't forget to add System.Management reference

Community
  • 1
  • 1
FreeMan
  • 1,417
  • 14
  • 20