3

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.

  • I think you want the USB vendor and device IDs. That is likly how operating-systems pick drivers for different USB devices. But you may want more if you have several similar USB (for example FTDI chips) connected to the same operating-system/computer. I assume that is why you are asking about the "Arduino board information"? – st2000 Jun 4 '17 at 15:44
  • if you change the com port for a board in windows, it will persist, then you don't need to search – dandavis Jun 5 '17 at 1:15
  • 2
    I'm voting to close this question as off-topic because it's about a C# program running on PC. – user31481 Jan 24 '18 at 15:39
  • @LookAlterno I don't know, it's a question Arduino users seem to come across at some point. I'm not bothered if it gets closed, but I genuinely think it's a helpful question for Arduino users specifically, as it has good viewing numbers on here. At it's core, it's an Arduino question, even if the answer is more generalized. It's up to the com and any precedence though. – n00dles Jan 24 '18 at 18:17
4

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.

  • IMHO Since Arduino IDE 1.6.8 listComPorts is no longer part of the arduino IDE – jantje Jun 5 '17 at 15:09
  • @jantje They may be using JNI now, but I'm guessing the underlying process is the same. Platform.java has 2 native functions resolveDeviceAttachedToNative and listSerialsNative but I can't find the source for them. – Johnny Mopp Jun 5 '17 at 15:42
  • edit: I misunderstood your comment. I think this is the code github.com/arduino/Arduino/blob/master/arduino-core/src/… they are using resolveDeviceAttachedToNative – jantje Jun 6 '17 at 18:31
  • I have looked into this many times and it looks like it is this github.com/arduino/libserialport but then again it doesn't – jantje Jun 6 '17 at 18:55
  • 1
    @n00dles It should. WMI has been around since Win2K. – Johnny Mopp Jun 8 '17 at 17:49
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.