65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
File: CH340.py
|
|
Author: Zinc Zou
|
|
Email: zinczou@163.com
|
|
Date: 2024/10/11
|
|
Copyright: 慕乐网络科技(大连)有限公司
|
|
www.mools.net
|
|
moolsnet@126.com
|
|
Description:
|
|
"""
|
|
import serial.tools.list_ports
|
|
|
|
|
|
def list_ch340_ports():
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
ch340_ports_list = []
|
|
# print(ports)
|
|
for port in ports:
|
|
if 'CH340' in port.description or 'CH340' in port.device:
|
|
ch340_ports_list.append(port.device)
|
|
print("Found CH340 ports:", port.device)
|
|
if ch340_ports_list:
|
|
return ch340_ports_list
|
|
else:
|
|
return []
|
|
|
|
|
|
def list_USB_ports():
|
|
|
|
ports = serial.tools.list_ports.comports()
|
|
USB_ports_list = []
|
|
# print(ports)
|
|
for port in ports:
|
|
if '串行' in port.description or '串行' in port.device:
|
|
USB_ports_list.append(port.device)
|
|
print("Found USB ports:", port.device)
|
|
if USB_ports_list:
|
|
return USB_ports_list
|
|
else:
|
|
return []
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
ports = list(serial.tools.list_ports.comports())
|
|
if len(ports) == 0:
|
|
print('No port available')
|
|
else:
|
|
for port in ports:
|
|
print(port)
|
|
port = list_ch340_ports()[0] # 串口名,根据实际情况修改
|
|
baudrate = 9600 # 波特率,根据实际情况修改
|
|
pump_ser = serial.Serial(port, baudrate)
|
|
port_USB = list_USB_ports()[0] # 串口名,根据实际情况修改
|
|
baudrate = 115200 # 波特率,根据实际情况修改
|
|
if port_USB:
|
|
USB_ser = serial.Serial(port, baudrate)
|
|
# ch340_ports = list_ch340_ports()
|
|
# if ch340_ports:
|
|
# print("Found CH340 ports:", ch340_ports)
|
|
# else:
|
|
# print("No CH340 ports found.") |