自動檢測串口的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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章