tablet_check.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import subprocess, time, re, json
  2. # 用 adb 抓实时 logcat + 发命令
  3. ADB = 'D:\\BaiduNetdiskDownload\\HBuilderX.4.24.2024072208\\HBuilderX\\plugins\\launcher-tools\\tools\\adbs\\adb.exe'
  4. # 先清空旧 logcat
  5. subprocess.run([ADB, 'logcat', '-c'], capture_output=True)
  6. # 在 PC 端也启动血氧(COM9)
  7. import serial
  8. try:
  9. s = serial.Serial('COM9', 115200, timeout=0.2)
  10. print(f"[{time.strftime('%H:%M:%S')}] ✅ PC COM9 打开")
  11. s.write(b'{"zt":"xy1","neirong":"01"}\n')
  12. s.flush()
  13. print(f"[{time.strftime('%H:%M:%S')}] TX xy1,01")
  14. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  15. s.flush()
  16. print(f"[{time.strftime('%H:%M:%S')}] TX sm2,01")
  17. s.close()
  18. except Exception as e:
  19. print(f"COM9 打开失败: {e}")
  20. print(f"\n[{time.strftime('%H:%M:%S')}] 等 3 秒让血氧启动...")
  21. time.sleep(3)
  22. # 启动 adb logcat 子进程抓 UsbSerial + BoardJson
  23. print(f"\n[{time.strftime('%H:%M:%S')}] 开始抓平板 logcat(15 秒)...")
  24. proc = subprocess.Popen(
  25. [ADB, 'logcat', '-v', 'time'],
  26. stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
  27. )
  28. start = time.time()
  29. json_re = re.compile(r'ASCII=(\{[^}]+\})')
  30. results = []
  31. while time.time() - start < 15:
  32. line = proc.stdout.readline()
  33. if not line: continue
  34. if 'UsbSerial' in line or 'BoardJson' in line:
  35. # 提取 JSON
  36. m = json_re.search(line)
  37. if m:
  38. try:
  39. d = json.loads(m.group(1))
  40. xl1 = d.get('xl1', '') or '(空)'
  41. xy1 = d.get('xy1', '') or '(空)'
  42. sm2 = d.get('sm2', '') or '(空)'
  43. xl2 = d.get('xl2', '') or '(空)'
  44. hx2 = d.get('hx2', '') or '(空)'
  45. flag = ''
  46. if xl1 != '(空)' and xy1 != '(空)': flag += ' ✅血氧'
  47. elif xl1 == '(空)' and xy1 == '(空)': flag += ' ⚠️血氧空'
  48. if xl2 != '(空)' and hx2 != '(空)': flag += ' ✅睡眠'
  49. elif xl2 == '(空)' and hx2 == '(空)': flag += ' ⚠️睡眠空'
  50. results.append((xl1, xy1, sm2, xl2, hx2))
  51. print(f" [{time.strftime('%H:%M:%S')}] 平板收到 JSON: xl1={xl1} xy1={xy1} | xl2={xl2} hx2={hx2}{flag}")
  52. except:
  53. pass
  54. elif 'SM7OK' in line:
  55. pass # 不刷屏
  56. proc.terminate()
  57. proc.wait()
  58. print(f"\n=== 平板验证结论 ===")
  59. print(f" 平板收到 JSON 帧数: {len(results)}")
  60. if results:
  61. last = results[-1]
  62. xl1_ok = last[0] != '(空)' and last[1] != '(空)'
  63. xl2_ok = last[3] != '(空)' and last[4] != '(空)'
  64. print(f" 血氧数据: {'✅ 正常' if xl1_ok else '❌ 空'}")
  65. print(f" 睡眠数据: {'✅ 正常' if xl2_ok else '❌ 空'}")