需要帮助列出Python中COMM端口上的设备

问题描述 投票:0回答:1

我想自动读取连接到计算机的所有设备,以便我可以与所有键盘输入进行通信,而不能与其他设备进行通信。我正在使用带有PySerial模块的Python。

我最近得到的是通过CMD:

python -m serial.tools.list_ports -v

COM1
    desc: Communications Port (COM1)
    hwid: ACPI\PNP0501\2
COM3
    desc: Keyspan USB Serial Port (COM3)
    hwid: KEYSPAN\*USA19HMAP\00_00
COM4
    desc: Keyspan USB Serial Port (COM4)
    hwid: KEYSPAN\*USA19HMAP\01_00
3 ports found


我真的很想使用python脚本来获取此信息,例如


print(serial.tools.comports)

但所有返回的都是

[<serial.tools.list_ports_common.ListPortInfo object at 0x00000000044022C8>, 
<serial.tools.list_ports_common.ListPortInfo object at 0x00000000043F8288>, 
<serial.tools.list_ports_common.ListPortInfo object at 0x00000000043F8388>]

而且我不确定这是什么意思。如果您有任何建议,我想听听他们的意见!

python cmd serial-port pyserial
1个回答
0
投票

您的结果显示serial.tools.comports是一个列表,因此您可以使用for循环对其进行格式化或对每个元素分别运行某些功能。

for item in serial.tools.comports:
    print( item )

使用dir(item),您可以看到此对象中可用的所有方法和属性。

使用help(item),您应该看到源代码中的文档。

如果您知道方法,则可以使用它们

for item in serial.tools.comports:
    print("Device:", item.device)
    print("Description:", item.desctiption)
© www.soinside.com 2019 - 2024. All rights reserved.