tablet_verify.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import serial, time, json, subprocess, re
  2. ADB = 'D:\\BaiduNetdiskDownload\\HBuilderX.4.24.2024072208\\HBuilderX\\plugins\\launcher-tools\\tools\\adbs\\adb.exe'
  3. print(f"[{time.strftime('%H:%M:%S')}] === 步骤 1: COM9 发命令 ===")
  4. s = serial.Serial('COM9', 115200, timeout=0.1)
  5. print(f" ✅ COM9 打开")
  6. # 先发 sm2,01(确保睡眠带稳定)
  7. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  8. s.flush()
  9. print(f" >>> sm2,01")
  10. # 发 xy1,01
  11. s.write(b'{"zt":"xy1","neirong":"01"}\n')
  12. s.flush()
  13. print(f" >>> xy1,01")
  14. # 等 12 秒,观察血氧能不能上来
  15. print(f"\n[{time.strftime('%H:%M:%S')}] === 步骤 2: COM9 等 12 秒看血氧 ===")
  16. line = b''
  17. end = time.time() + 12
  18. xy1_up = False
  19. while time.time() < end:
  20. data = s.read(512)
  21. for b in data:
  22. if b in (13, 10):
  23. if not line: continue
  24. t = line.decode('ascii', errors='replace')
  25. line = b''
  26. if t.startswith('{'):
  27. d = json.loads(t)
  28. xl1 = d.get('xl1', '') or '(空)'
  29. xy1 = d.get('xy1', '') or '(空)'
  30. xl2 = d.get('xl2', '') or '(空)'
  31. hx2 = d.get('hx2', '') or '(空)'
  32. flag = ''
  33. if xl1 != '(空)' and xy1 != '(空)':
  34. flag = ' ✅血氧上来了!'
  35. xy1_up = True
  36. elif xl1 == '(空)' and xy1 == '(空)':
  37. flag = ' ⚠️血氧空'
  38. print(f" PC COM9: xl1={xl1:<6} xy1={xy1:<6} xl2={xl2:<6} hx2={hx2:<6}{flag}")
  39. else:
  40. line += bytes([b])
  41. s.close()
  42. print(f"\n 血氧有没有上来: {'✅ YES' if xy1_up else '❌ NO'}")
  43. # 现在连平板看它能不能收到
  44. print(f"\n[{time.strftime('%H:%M:%S')}] === 步骤 3: 连平板抓 logcat(10 秒)===")
  45. # 先 kill 所有 adb 进程干净
  46. subprocess.run(['taskkill', '/F', '/IM', 'adb.exe'], capture_output=True, shell=True)
  47. time.sleep(2)
  48. # 启动 adb server + connect
  49. r = subprocess.run([ADB, 'start-server'], capture_output=True, text=True, timeout=10)
  50. time.sleep(3)
  51. # 清 logcat
  52. subprocess.run([ADB, 'logcat', '-c'], capture_output=True, timeout=5)
  53. # 抓 logcat
  54. proc = subprocess.Popen([ADB, 'logcat', '-v', 'time'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
  55. json_re = re.compile(r'ASCII=(\{.+\})')
  56. results = []
  57. start = time.time()
  58. while time.time() - start < 10:
  59. line_out = proc.stdout.readline()
  60. if not line_out: continue
  61. if 'UsbSerial' in line_out or 'BoardJson' in line_out:
  62. m = json_re.search(line_out)
  63. if m:
  64. try:
  65. d = json.loads(m.group(1))
  66. xl1 = d.get('xl1', '') or '(空)'
  67. xy1 = d.get('xy1', '') or '(空)'
  68. sm2 = d.get('sm2', '') or '(空)'
  69. xl2 = d.get('xl2', '') or '(空)'
  70. hx2 = d.get('hx2', '') or '(空)'
  71. flag = ''
  72. if xl1 != '(空)' and xy1 != '(空)': flag += ' ✅血氧'
  73. elif xl1 == '(空)' and xy1 == '(空)': flag += ' ⚠️血氧空'
  74. if xl2 != '(空)' and hx2 != '(空)': flag += ' ✅睡眠'
  75. elif xl2 == '(空)' and hx2 == '(空)': flag += ' ⚠️睡眠空'
  76. results.append((xl1, xy1, sm2, xl2, hx2))
  77. print(f" 平板收到: xl1={xl1:<6} xy1={xy1:<6} sm2={sm2:<5} xl2={xl2:<6} hx2={hx2:<6}{flag}")
  78. except:
  79. pass
  80. proc.terminate()
  81. proc.wait(timeout=3)
  82. print(f"\n=== 🎯 最终验证 ===")
  83. print(f" PC COM9 血氧: {'✅ 正常' if xy1_up else '❌ 空'}")
  84. print(f" 平板收到 JSON: {len(results)} 帧")
  85. if results:
  86. last = results[-1]
  87. xl1_ok = last[0] != '(空)' and last[1] != '(空)'
  88. xl2_ok = last[3] != '(空)' and last[4] != '(空)'
  89. print(f" 平板血氧: {'✅ 正常' if xl1_ok else '❌ 空'} ({last[0]}, {last[1]})")
  90. print(f" 平板睡眠: {'✅ 正常' if xl2_ok else '❌ 空'} ({last[3]}, {last[4]})")
  91. else:
  92. print(" ⚠️ 平板没收到任何 JSON")