首页 > 解决方案 > pyserial 使用 serial.tools.list_ports.grep() 找不到设备

问题描述

我有一个 USB 设备(基于 STM32F4 微控制器),在 USB 描述符中设置了 VID、PID 和产品描述。

VID = 0x0483
PID = 0x5740
Product Description = "ACME thing-a-me-bob"

在 macOS 上,使用serial.tools.list_ports.grep()可以很好地定位我的设备。例如p = grep("thing-a-me-bob")或 `p = grep("ACME")

但是在 Windows (Win 10) 上,grep 找不到我的设备。如果我使用grep("")它,它会列出我的设备(以及所有其他设备),但它只显示:

COM3 - USB Serial Device (COM3)

该设备仅使用 Win 10 附带的标准 Microsoft CDC 驱动程序。然而,我认为简单/标准认为产品描述字符串将从 USB 设备读取并使用(就像标准 macOS 驱动程序一样)。

有没有办法serial.tools.list_ports.grep()使用标准的 Windows 驱动程序?

注意:grep("0483:5740")确实找到了我的设备,但这是一组合理常用的 VID:PID 值,由 ST Micro 提供。

标签: pythonwindowspyserial

解决方案


不知道如何grep()工作,但你可以像这样连接。

if sys.platform == 'win32':
    ports = list(list_ports.comports())
    for port in ports:
        # query each port and connect if description is found
        if 'ACME thing-a-me-bob' in port.description:
            s = serial.Serial(port.device)

推荐阅读