查询串口
from serial.tools.list_ports import comports(from serial.tools.list_ports_windows import comports)
print(comports())
[<serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6B0A8C0>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6C76BF0>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6B0B370>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6B0A680>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6B0A5C0>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6B0A620>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBDA80>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBDAB0>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBD990>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBDB10>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBD930>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBD960>, <serial.tools.list_ports_common.ListPortInfo object at 0x0000014FD6CBDC60>]
参数类型
<class 'serial.tools.list_ports_common.ListPortInfo'>
列表下存放字符串类型
def find_port():
ports = []
for c in comports():
# 列表下存放字符串类型串口
ports.append(str(c))
print(ports)
return ports
查询结果
COM57 - Silicon Labs Dual CP210x USB to UART Bridge: Enhanced COM Port (COM57)
COM1 - 通信端口 (COM1)
COM58 - Silicon Labs Dual CP210x USB to UART Bridge: Standard COM Port (COM58)
COM42 - SimTech HS-USB Diagnostics 9011 (COM42)
COM76 - SimTech HS-USB NMEA 9011 (COM76)
COM45 - Silicon Labs Dual CP210x USB to UART Bridge: Standard COM Port (COM45)
COM38 - Silicon Labs Dual CP210x USB to UART Bridge: Enhanced COM Port (COM38)
COM43 - SimTech HS-USB AT Port 9011 (COM43)
COM14 - SimTech HS-USB Modem 9011 #2
- 标准 33600 bps 调制解调器
- 标准 33600 bps 调制解调器 #2
- 标准 33600 bps 调制解调器 #3
- 标准 33600 bps 调制解调器 #4
查找匹配到的第一个uart口
def uart(ports):
for p in ports:
match = re.match(r'(COM\d+).*Enhanced.*', p)
if match:
return match.group(1)
同理,查找at口和modem口
def at(ports):
for p in ports:
match = re.match(r'(COM\d+).*AT.*', p)
if match:
return match.group(1)
def modem(ports):
for p in ports:
match = re.match(r'(COM\d+).*Modem.*', p)
if match:
return match.group(1)
结果
if __name__ == '__main__':
u = uart(find_port())
a = at(find_port())
m = modem(find_port())
print('串口:', u)
print('at口:', a)
print('modem口:', m)
标签:USB,Port,查找,串口,COM,ports,match
From: https://www.cnblogs.com/xxcnblog/p/18529532