#!/usr/bin/env python3 """ 精确验证:每发一条命令后,盯着 JSON 输出里 sm2_ok / xy1_ok / 睡眠带 / 血氧 字段是否变化。 板子的 Host_ParseCmd 处理完后会进主循环的 if(xy1_shzh==1) / if(sm2_shzh==1) 分支, 那里会调 fengmingqi() 并改变状态变量。 """ import serial import time import json import threading import sys PORT = 'COM9' BAUD = 115200 # 解析 JSON 行的小工具 def parse_json_line(line): """把 {"xl1":"","sm2":"0",...} 转成 dict,提取关键字段""" try: d = json.loads(line) return d except: return None # 关键字段追踪 KEY_FIELDS = ['xl1', 'xy1', 'wxh1', 'pl1', 'sm2', 'pl2', 'zy2', 'xl2', 'hx2'] print(f"Opening {PORT} @ {BAUD}...", flush=True) s = serial.Serial(PORT, BAUD, timeout=0.1) print(f"OK.\n", flush=True) # 先等 6 秒抓一帧基线 JSON print("=== Phase 1: 抓基线状态(等 6 秒)===") baseline = {} start = time.time() while time.time() - start < 6: line = b'' while True: b = s.read(1) if not b: break if b == 0x0D: continue if b == 0x0A: break line += b if line and b'{' in line: ts = time.strftime('%H:%M:%S') text = line.decode('ascii', errors='replace') d = parse_json_line(text) if d: print(f"[{ts}] BASELINE: ", end='') for k in KEY_FIELDS: print(f"{k}={d.get(k,'?')} ", end='') print() baseline = d # Phase 2: 发 sm2 停止 → 观察 sm2_ok 变空 print("\n=== Phase 2: TX {sm2,02}(停止睡眠带)===") s.write(b'{"zt":"sm2","neirong":"02"}\n') s.flush() for _ in range(6): # 等 6 秒抓 1-2 帧 time.sleep(1) line = b'' while True: b = s.read(1) if not b: break if b == 0x0D: continue if b == 0x0A: break line += b if line and b'{' in line: ts = time.strftime('%H:%M:%S') text = line.decode('ascii', errors='replace') d = parse_json_line(text) if d: print(f"[{ts}] AFTER sm2=02: ", end='') for k in KEY_FIELDS: print(f"{k}={d.get(k,'?')} ", end='') print() # Phase 3: 发 sm2 启动 → 观察 sm2_ok 恢复 print("\n=== Phase 3: TX {sm2,01}(启动睡眠带)===") s.write(b'{"zt":"sm2","neirong":"01"}\n') s.flush() for _ in range(6): time.sleep(1) line = b'' while True: b = s.read(1) if not b: break if b == 0x0D: continue if b == 0x0A: break line += b if line and b'{' in line: ts = time.strftime('%H:%M:%S') text = line.decode('ascii', errors='replace') d = parse_json_line(text) if d: print(f"[{ts}] AFTER sm2=01: ", end='') for k in KEY_FIELDS: print(f"{k}={d.get(k,'?')} ", end='') print() # Phase 4: 发 xy1 启动(血氧) print("\n=== Phase 4: TX {xy1,01}(启动血氧)===") s.write(b'{"zt":"xy1","neirong":"01"}\n') s.flush() for _ in range(6): time.sleep(1) line = b'' while True: b = s.read(1) if not b: break if b == 0x0D: continue if b == 0x0A: break line += b if line and b'{' in line: ts = time.strftime('%H:%M:%S') text = line.decode('ascii', errors='replace') d = parse_json_line(text) if d: print(f"[{ts}] AFTER xy1=01: ", end='') for k in KEY_FIELDS: print(f"{k}={d.get(k,'?')} ", end='') print() s.close() print("\n=== Done ===")