【问题标题】:Pyserial get the name of the device behind a COM PortPyserial 获取 COM 端口后面的设备名称
【发布时间】:2016-03-01 13:36:35
【问题描述】:

我想获取连接到 COM 端口的设备列表。例如,我不想让python -m serial.tools.list_ports 输出COM1 COM2,而是输出Arduino_Uno Arduino Due 等。(例如,Arduino Gui 就是这样做的)。

我找到了一些列出 COM 端口的答案(如 Listing available com ports with Python ),但我的问题没有答案。

【问题讨论】:

  • 您正在寻找的东西是不可能的。串行接口非常简单,除了EIA关于如何传输数据的规范和一些控制信号之外,没有指定任何通信协议。
  • 嗯。你知道如何使用 Windows 命令来做到这一点(比如 cmd,然后使用 python 中的输出)?

标签: python serial-port pyserial


【解决方案1】:

@J.P.彼得森是对的 - 串行端口本身并不提供该信息。但是 USB 规范允许一些信息被偷偷进入,并且 serial.tools.list_ports.comports() 谢天谢地返回。以下代码片段来自我的 ArduinoBase 类,该类在 Windows 和 Linux 下运行,可以满足您的需求,即它为您提供 Arduino Uno 和 Arduino Due 适当的描述

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)

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2023-04-11
    • 2017-11-02
    • 2012-08-23
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多