Python 串口 comports 搜索可用串口

177 阅读1分钟
  •        Python : 3.8.13
  •          OS : Windows 21H1
  •       Conda : 4.12.0
  •     PyCharm : 2022.1 (Community Edition)

端口信息

available-port-1.png

代码

import serial.tools.list_ports

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

print("可用串口数量", len(port_list))

if len(port_list):
    for port in port_list:
        print("port.device:", port.device)
        print("port.description:", port.description)
        print("port.name:", port.name)
else:
    print("无可用串口")

运行

D:\Develop\Anaconda3\envs\pyqtenv\python.exe C:/Users/admin/Desktop/SerialDebuggingAssistant/demo.py
可用串口数量 1
port.device: COM3
port.description: Silicon Labs CP210x USB to UART Bridge (COM3)
port.name: COM3

Process finished with exit code 0

学习推荐


Python具有开源、跨平台、解释型和交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。