自动检测串口的python脚本

以python3对于CP2103串口芯片的检测为例:

#!/usr/bin/python3

import sys
import os
import re
import serial.tools.list_ports

def print_serial(port):
    print("---------------[ %s ]---------------" % (port.name))
    print("Path: %s" % (port.device))
    print("Descript: %s" % (port.description))
    print("HWID: %s" % (port.hwid))
    if not None == port.manufacturer:
        print("Manufacture: %s" % (port.manufacturer))
    if not None == port.product:
        print("Product: %s" % (port.product))
    if not None == port.interface:
        print("Interface: %s" % (port.interface))
    print()

def detect_serials(vid=0x10c4, pid=0xea60):
    ports = serial.tools.list_ports.comports()
    port_cnt = 0
    port_list = []
    for port in ports:
        print_serial(port)
        if vid == port.vid and port.pid == pid:
            port_list.append(port)
            port_cnt += 1
    return (port_cnt, port_list)

def detect_serial(vid=0x10c4, pid=0xea60):
    ports = serial.tools.list_ports.comports()
    for port in ports:
        if vid == port.vid and port.pid == pid:
            return (True, port.device)
    return (False, "")

if __name__ == '__main__':
    r, dev = detect_serial()
    if r:
        print(dev)
发布了9 篇原创文章 · 获赞 0 · 访问量 2617

猜你喜欢

转载自blog.csdn.net/liyangzmx/article/details/93902965