Question

The following code returns only three serial ports (com3, com4 and com5). The firmware that I would like to access is located on a USB plug multiplier. How can I access the serial ports of this mulitplier and how can I identify the specific USB containing the firmware that I want to send information to?

using System;
using System.IO.Ports;

namespace SerialPortExample
{
    class SerialPortExample
    {
        public static void Main()
        {
            string[] ports = SerialPort.GetPortNames();
            Console.WriteLine("The following serial ports were found:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }
            Console.ReadLine();
        }
    }
}

Many thanks in advance!

Was it helpful?

Solution

This is a pretty big usability problem and caused by USB drivers taking a shortcut and emulating a serial port to make it easy to interface with them. Serial ports are very primitive devices, which makes their api very easy to use. But lacks any kind of support for plug-and-play, there's no way to get a decent notification for them. The driver just picks an arbitrary port number and it is up to the user to figure out which one it might be. Trial and error stuff. This didn't use to be a problem, serial ports had a connector mounted on the machine's back panel that was clearly labeled with the COM port name.

You can possibly get some mileage out of WMI, it lets you enumerate serial port devices with the Win32_SerialPort query. What you get is fairly unpredictable, it completely depends on the driver to supply the data. Best way to experiment with that is with the WMI Code Creator utility, it can also auto-generate the C# code you need. Unfortunately I can't find the download location anymore, this appears to have been removed in the past couple of weeks. Hope you can find an alternative.

OTHER TIPS

The code below does a good job finding the specific ports:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Windows.Forms;
namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass x = new MyClass();
            var com = x.GetCOMs();
            foreach (string port in com) 
            {
                Console.WriteLine(port);
            }
            Console.ReadLine();
        }

    }

    class MyClass
    {
        public List<string> GetCOMs()
        {
            List<string> coms = new List<string>();
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");

                foreach (ManagementObject obj in searcher.Get())
                {
                    object captionObj = obj["Caption"];
                    if (captionObj != null)
                    {
                        string caption = captionObj.ToString();
                        if (caption.Contains("(COM"))
                        {
                            coms.Add(caption);
                        }
                    }
                }

                m_ParseCOMs(ref coms);
            }
            catch (ManagementException ex)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
                return coms;
            }

            return coms;
        }

        private void m_ParseCOMs(ref List<string> comPorts)
        {
            string[] temp;
            List<string> temp2 = new List<string>();
            int index = 0;
            foreach (string s in comPorts)
            {
                string temp3 = "";
                temp = s.Split(' ');
                temp3 += temp[temp.Length - 1] + " - ";
                for (int i = 0; i < temp.Length - 1; i++)
                {
                    temp3 += temp[i] + " ";
                }
                temp2.Insert(index, temp3);
                index++;
            }
            comPorts = temp2;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top