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
  • 5
  • 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 04 '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 05 '17 at 01:15
  • 2
    I'm voting to close this question as off-topic because it's about a C# program running on PC. –  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

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
  • 953
  • 6
  • 11
  • IMHO Since Arduino IDE 1.6.8 listComPorts is no longer part of the arduino IDE – jantje Jun 05 '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. – 001 Jun 05 '17 at 15:42
  • edit: I misunderstood your comment. I think this is the code https://github.com/arduino/Arduino/blob/master/arduino-core/src/processing/app/Platform.java#L231 they are using resolveDeviceAttachedToNative – jantje Jun 06 '17 at 18:31
  • I have looked into this many times and it looks like it is this https://github.com/arduino/libserialport but then again it doesn't – jantje Jun 06 '17 at 18:55
  • Thanks Johnny; I was actually looking into using WMI to get the info, so it's nice to see it laid out just how I need it :)))) This will work on any version of windows, right? – n00dles Jun 07 '17 at 11:40
  • @jantje If that's what they are using it uses lower level calls than WMI to get the data. Since Java's serial port handling is not very good, it would make sense to use that. – 001 Jun 08 '17 at 17:48
  • 1
    @n00dles It should. WMI has been around since Win2K. – 001 Jun 08 '17 at 17:49
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