probe_uarts.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """针对性探测:list_device(串口 open 标志)+ list_thread(线程状态)"""
  4. import re
  5. import sys
  6. import time
  7. import serial
  8. port = sys.argv[1] if len(sys.argv) > 1 else "COM3"
  9. ANSI = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
  10. s = serial.Serial(port=port, baudrate=115200, bytesize=8, parity="N",
  11. stopbits=1, timeout=0.05)
  12. def read_idle(idle=0.8, max_wait=6.0):
  13. got, t0, last = [], time.time(), time.time()
  14. while True:
  15. n = s.in_waiting
  16. if n:
  17. got.append(s.read(n))
  18. last = time.time()
  19. else:
  20. time.sleep(0.03)
  21. if time.time() - last >= idle or time.time() - t0 >= max_wait:
  22. break
  23. return b"".join(got).decode("utf-8", errors="replace")
  24. def cmd(c, idle=0.8, max_wait=6.0):
  25. s.write((c + "\r\n").encode())
  26. return read_idle(idle, max_wait)
  27. def show(title, text, pattern=None):
  28. print("\n########## %s ##########" % title)
  29. for line in ANSI.sub("", text).splitlines():
  30. line = line.strip()
  31. if not line:
  32. continue
  33. if pattern and not re.search(pattern, line):
  34. continue
  35. print(line)
  36. out = cmd("list_device")
  37. show("list_device", out, r"uart|adc|pwm|i2c|spi|Device|device|类型|name")
  38. out = cmd("list_thread")
  39. show("list_thread", out, r"thread|---|Hmi|hmi|dtu|work|md_m|Power|adc|sysmon|timer|tidle|tshell|main|clean|heat|Ctr")
  40. s.close()