Home:ALL Converter>get com port number of an USB adapter

get com port number of an USB adapter

Ask Time:2013-04-23T15:46:50         Author:Ottavio Campana

Json Formatter

I am trying to write a program that uses an arduino mega and a FTDI-based USB to RS485 adapter.

I want to make the program user-friendly, thus I don't wont the user to manually check the com port number, but I want to auto-detect it. Here's a snippet of the code

    ManagementScope scope = new ManagementScope();
    SelectQuery query = new SelectQuery("SELECT * FROM Win32_SerialPort");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

    try
    {
        foreach (ManagementObject item in searcher.Get())
        {
            String description = item["Description"].ToString();
            String deviceID = item["DeviceID"].ToString();

            Console.WriteLine("Porta " + description + " deviceID " + deviceID);

            if (description.Contains("USB Serial Port"))
                return deviceID;
        }
    }
    catch (ManagementException)
    {
    }

The point that I am not able to understand is why I can find the USB Serial port of the Arduino (matching description.Contains("Arduino") ) but not the com port of the USB RS485 port.

Do you have an idea why this could happen? is the query SELECT * FROM Win32_SerialPort wrong?

Author:Ottavio Campana,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16163773/get-com-port-number-of-an-usb-adapter
Ottavio Campana :

Well, after studying several forums, I discovered that the com port associated to the USB/RS485 adapter is listed if I run the query SELECT * FROM Win32_PnPEntity . \n\nBut I really don't understand why the serial port of the arduino is shown by a query and the other port with the other query.... I mean, those are both uSB<->serial adapters!",
2013-04-23T11:10:12
Marcello Romani :

I don't have a USB<->RS485 adapter, but I suspect they don't fall into the \"serial port\" category (which seems reserved for RS232 interfaces), which could be the reason why they don't appear among the Win32_SerialPort query results.\n\nTo detect the connection of and Arduino board I look into this registry key:\n\nHKLM\\HARDWARE\\DEVICEMAP\\SERIALCOMM\n\n\nfor entries like\n\n\\Device\\VCP0\n\n\n(VCP is the prefix to look for).\n\nMaybe you can look into this registry key too, or watch its parent key, DEVICEMAP, and see what happens when you connect the FTDI RS485 adapter.\nDetecting a change in one of these registry keys contents should be straightforward at that point.\n\nHTH",
2013-04-30T11:56:01
Jaskaran Singh :

This might work for you. I used this to dynamically read port number of Arduino on a system. Here \n\n\n description.Contains(\"uino\")\n\n\nis to look for both Arduino and Genuino keyword for both varients of board.\n\n public string detectArduinoPort()\n {\n ManagementScope mScope = new ManagementScope();\n SelectQuery query = new SelectQuery(\"SELECT * FROM Win32_SerialPort\");\n ManagementObjectSearcher objectList = new ManagementObjectSearcher(mScope, query);\n\n try\n {\n foreach (ManagementObject obj in objectList.Get())\n {\n string description = obj[\"Description\"].ToString();\n string deviceId = obj[\"DeviceID\"].ToString();\n\n if (description.Contains(\"uino\"))\n {\n return deviceId;\n }\n }\n }\n catch (Exception)\n {\n\n }\n return \"\";\n }\n",
2018-06-18T13:07:58
Mikias Gebre :

as @Marcello Romani pointed out FTDI don't fall into the \"serial port\" category. Even though I am late I came across this problem recently and I fixed it using another query. Instead of searching in WIN32_SerialPort, you can query the Win32_PnPEntity class.One downside of using this class is that is slow to query. \n\n using (var searcher = new ManagementObjectSearcher\n (\"SELECT * FROM Win32_PnPEntity\"))\n {\n string[] portnames = SerialPort.GetPortNames();\n var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();\n\n foreach (ManagementBaseObject queryObj in ports)\n {\n }\n\n }\n",
2020-03-05T11:32:16
yy