import serial, time, json s = serial.Serial('COM9', 115200, timeout=0.3) # 关再开 sm2 重启睡眠带 s.write(b'{"zt":"sm2","neirong":"00"}\n') time.sleep(1) s.reset_input_buffer() s.write(b'{"zt":"sm2","neirong":"01"}\n') print(f"[{time.strftime('%H:%M:%S')}] sm2 restarted — 请躺好不动!") # 抓 120 秒 print("\n=== 抓 120 秒(躺好别动!)===") end = time.time() + 120 buf = b'' prev_fat = -1 prev_stress = -1 prev_0b_fat = -1 prev_0b_stress = -1 last_json = None while time.time() < end: data = s.read(2048) if data: buf += data while b'\n' in buf: line, buf = buf.split(b'\n', 1) try: t = line.decode('ascii', errors='replace').strip() except: continue if t.startswith('SM7OK') and ':' in t: p = t.split(':')[1].strip().split() if len(p) < 13: continue cmd = f"{p[11]}{p[12]}" # 只在值变化时打印 if cmd == "0207" and len(p) >= 15: f_val = int(p[14], 16) if f_val != prev_fat: print(f" 0x0207 疲劳查询: fatigue={f_val}", flush=True) prev_fat = f_val elif cmd == "0209" and len(p) >= 17: s_val = int(p[14], 16) if s_val != prev_stress: print(f" 0x0209 压力查询: stress={s_val} bed={p[16]}", flush=True) prev_stress = s_val elif cmd == "020B" and len(p) >= 20: f0b = int(p[15], 16) s0b = int(p[17], 16) if f0b != prev_0b_fat or s0b != prev_0b_stress: print(f" ★ 0x020B 主动上报: sleep={p[14]} fatigue={f0b} stress={s0b} bed={p[19]}", flush=True) prev_0b_fat = f0b prev_0b_stress = s0b elif t.startswith('{') and 'pl2' in t: try: last_json = json.loads(t) except: pass s.close() print(f"\n=== 120 秒结果 ===") if last_json: print(f" 最终 JSON: pl2={last_json['pl2']} qx2={last_json['qx2']} xl2={last_json['xl2']} hx2={last_json['hx2']} bq2={last_json['bq2']} zy2={last_json['zy2']}") print(f" 疲劳查询值变化过: {prev_fat >= 0} (最后={prev_fat})") print(f" 压力查询值变化过: {prev_stress >= 0} (最后={prev_stress})") print(f" 0x020B 主动上报出现过: {prev_0b_fat >= 0}")