import subprocess, time, re, json # 用 adb 抓实时 logcat + 发命令 ADB = 'D:\\BaiduNetdiskDownload\\HBuilderX.4.24.2024072208\\HBuilderX\\plugins\\launcher-tools\\tools\\adbs\\adb.exe' # 先清空旧 logcat subprocess.run([ADB, 'logcat', '-c'], capture_output=True) # 在 PC 端也启动血氧(COM9) import serial try: s = serial.Serial('COM9', 115200, timeout=0.2) print(f"[{time.strftime('%H:%M:%S')}] ✅ PC COM9 打开") s.write(b'{"zt":"xy1","neirong":"01"}\n') s.flush() print(f"[{time.strftime('%H:%M:%S')}] TX xy1,01") s.write(b'{"zt":"sm2","neirong":"01"}\n') s.flush() print(f"[{time.strftime('%H:%M:%S')}] TX sm2,01") s.close() except Exception as e: print(f"COM9 打开失败: {e}") print(f"\n[{time.strftime('%H:%M:%S')}] 等 3 秒让血氧启动...") time.sleep(3) # 启动 adb logcat 子进程抓 UsbSerial + BoardJson print(f"\n[{time.strftime('%H:%M:%S')}] 开始抓平板 logcat(15 秒)...") proc = subprocess.Popen( [ADB, 'logcat', '-v', 'time'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) start = time.time() json_re = re.compile(r'ASCII=(\{[^}]+\})') results = [] while time.time() - start < 15: line = proc.stdout.readline() if not line: continue if 'UsbSerial' in line or 'BoardJson' in line: # 提取 JSON m = json_re.search(line) if m: try: d = json.loads(m.group(1)) xl1 = d.get('xl1', '') or '(空)' xy1 = d.get('xy1', '') or '(空)' sm2 = d.get('sm2', '') or '(空)' xl2 = d.get('xl2', '') or '(空)' hx2 = d.get('hx2', '') or '(空)' flag = '' if xl1 != '(空)' and xy1 != '(空)': flag += ' ✅血氧' elif xl1 == '(空)' and xy1 == '(空)': flag += ' ⚠️血氧空' if xl2 != '(空)' and hx2 != '(空)': flag += ' ✅睡眠' elif xl2 == '(空)' and hx2 == '(空)': flag += ' ⚠️睡眠空' results.append((xl1, xy1, sm2, xl2, hx2)) print(f" [{time.strftime('%H:%M:%S')}] 平板收到 JSON: xl1={xl1} xy1={xy1} | xl2={xl2} hx2={hx2}{flag}") except: pass elif 'SM7OK' in line: pass # 不刷屏 proc.terminate() proc.wait() print(f"\n=== 平板验证结论 ===") print(f" 平板收到 JSON 帧数: {len(results)}") if results: last = results[-1] xl1_ok = last[0] != '(空)' and last[1] != '(空)' xl2_ok = last[3] != '(空)' and last[4] != '(空)' print(f" 血氧数据: {'✅ 正常' if xl1_ok else '❌ 空'}") print(f" 睡眠数据: {'✅ 正常' if xl2_ok else '❌ 空'}")