6

I want my C# application to automatically select the correct COM port by way of a string equality check against a 'GetInfo'-type request to the Arduino board.

I know the Arduino IDE can get info from the board;

Tools > GetBoardInfo

enter image description here

But I don't know how to get it using a sketch or if it is even possible for that matter.

Is this something that can be read from the board, or is it a USB-thing. If it's a USB thing, I'd have to get the info from GetUSBDevices.DeviceID or whatever, then get the serial port used by the USB Device, which would probably work, but I'd rather do it all via a serialPort.

n00dles
  • 163
  • 1
  • 1
  • 5

2 Answers2

5

Looking at the source code of the Arduino IDE on github, it looks like they call an executable (listComPorts.exe). So I would guess you can't get that info through serial.

Here's a C# app using WMI that can get port, vid, and pid:

namespace PortTest
{
    class Program
    {
        // Helper function to handle regex search
        static string regex(string pattern, string text)
        {
            Regex re = new Regex(pattern);
            Match m = re.Match(text);
            if (m.Success)
            {
                return m.Value;
            }
            else
            {
                return null;
            }
        }

        static void Main(string[] args)
        {
            // Use WMI to get info
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"");

            // Search all serial ports
            foreach (ManagementObject queryObj in searcher.Get())
            {
                // Parse the data
                if (null != queryObj["Name"])
                {
                    Console.WriteLine("Port = " + regex(@"(\(COM\d+\))", queryObj["Name"].ToString()));
                }
                //PNPDeviceID = USB\VID_1A86&PID_7523\5&1A63D808&0&2
                if (null != queryObj["PNPDeviceID"])
                {
                    Console.WriteLine("VID = " + regex("VID_([0-9a-fA-F]+)", queryObj["PNPDeviceID"].ToString()));
                    Console.WriteLine("PID = " + regex("PID_([0-9a-fA-F]+)", queryObj["PNPDeviceID"].ToString()));
                }
            }
            Console.WriteLine("Done");
            int c = Console.Read();
        }
    }
}

From there, it looks like it searches an online database for more info. See: getBoardWithMatchingVidPidFromCloud() function.

001
  • 963
  • 6
  • 11
1

Please look here https://todbot.com/blog/2012/03/02/listcomports-windows-command-line-tool-for-usb-to-serial/ The article is right about what you are seeking. Thanks to @Johnny Mopp, searching "listComPorts" keyword helped me.

Quote from that article:

How it works

The C and the VBS versions both utilize the WMI infrastructure that’s been around since Windows 2000 to query the machine about its configured PnP devices. The WMI is a huge data structure of just about any information in Windows. Except, it seems, good information about COM ports. While there is a “Win32_SerialPort” table in WMI, that only contains information about hardware serial ports, not USB-to-serial adapters. Instead, these two tools look at the “Win32_PnPEntity” table. While this table does list USB-to-serial adapters, it does not contain a proper mapping of the adapter’s USB or PnP ID to COM port. Instead, these tools do a string search on the “Caption” field for the string “(COMn)” where “n” is a number. It’s an incredible hack but seems to work.

Faig
  • 121
  • 6