2

I want to get a list of the devices which are connected to a COM Port. Instead of having python -m serial.tools.list_ports with the output COM1 COM2 for example, I want to have an output like Arduino_Uno Arduino Due etc. (Like the Arduino Gui do it, for example).

I found some answers for listing the COM Ports (like Listing available com ports with Python ), but no answer for my problem.

Community
  • 1
  • 1
alabamajack
  • 642
  • 8
  • 23
  • What you are looking for is not possible. The serial interface is very simple, and does not specify any communication protocols, besides the EIA specifications on how to transmit data, and some control signals. – J. P. Petersen Mar 02 '16 at 10:08
  • Hmmm. Do you know how to do this with Windows commands (so on cmd, and then using the output in python)? – alabamajack Mar 02 '16 at 10:41

1 Answers1

0

@J.P. Petersen is right - the serial port does not by itself provide that information. But the USB specifications allows some information to be sneaked in, and serial.tools.list_ports.comports() thankfully returns that. The following code snipped is from my ArduinoBase class that works under Windows and Linux and does what you are looking for, i.e. it gives you Arduino Uno and Arduino Due descriptions as appropriate

def listPorts():
    """!
    @brief Provide a list of names of serial ports that can be opened as well as a
    a list of Arduino models.
    @return A tuple of the port list and a corresponding list of device descriptions
    """

    ports = list( serial.tools.list_ports.comports() )

    resultPorts = []
    descriptions = []
    for port in ports:
        if not port.description.startswith( "Arduino" ):
            # correct for the somewhat questionable design choice for the USB
            # description of the Arduino Uno
            if port.manufacturer is not None:
                if port.manufacturer.startswith( "Arduino" ) and \
                   port.device.endswith( port.description ):
                    port.description = "Arduino Uno"
                else:
                    continue
            else:
                continue
        if port.device:
            resultPorts.append( port.device )
            rdescriptions.append( str( port.description ) )

    return (resultPorts, descriptions)