#!/usr/bin/env python3 # -*- coding: utf-8 -*- """针对性探测:list_device(串口 open 标志)+ list_thread(线程状态)""" import re import sys import time import serial port = sys.argv[1] if len(sys.argv) > 1 else "COM3" ANSI = re.compile(r"\x1b\[[0-9;]*[A-Za-z]") s = serial.Serial(port=port, baudrate=115200, bytesize=8, parity="N", stopbits=1, timeout=0.05) def read_idle(idle=0.8, max_wait=6.0): got, t0, last = [], time.time(), time.time() while True: n = s.in_waiting if n: got.append(s.read(n)) last = time.time() else: time.sleep(0.03) if time.time() - last >= idle or time.time() - t0 >= max_wait: break return b"".join(got).decode("utf-8", errors="replace") def cmd(c, idle=0.8, max_wait=6.0): s.write((c + "\r\n").encode()) return read_idle(idle, max_wait) def show(title, text, pattern=None): print("\n########## %s ##########" % title) for line in ANSI.sub("", text).splitlines(): line = line.strip() if not line: continue if pattern and not re.search(pattern, line): continue print(line) out = cmd("list_device") show("list_device", out, r"uart|adc|pwm|i2c|spi|Device|device|类型|name") out = cmd("list_thread") show("list_thread", out, r"thread|---|Hmi|hmi|dtu|work|md_m|Power|adc|sysmon|timer|tidle|tshell|main|clean|heat|Ctr") s.close()