import serial, time, json s = serial.Serial('COM8', 115200, timeout=0.3) time.sleep(0.1) # 关再开 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 started — 躺好别动!") time.sleep(3) print(f"\n=== 等 3 分钟 (无查询轮询) ===\n") end = time.time() + 180 buf = b'' count_0b = 0 hr_rr_count = 0 prev_fat_stress = 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]}" ts = time.strftime('%H:%M:%S') if cmd == "020B" and len(p) >= 20: sleep_v = int(p[14], 16) fat_v = int(p[15], 16) brh_v = int(p[16], 16) s1_v = int(p[17], 16) s2_v = int(p[18], 16) bed_v = int(p[19], 16) count_0b += 1 now = (fat_v, s1_v) if now != prev_fat_stress: marker = "★★★" if (fat_v > 0 or s1_v > 0) else " " print(f" [{ts}] {marker} 0x020B #{count_0b}: sleep={sleep_v} fatigue={fat_v} stress={s1_v} bed={bed_v}", flush=True) prev_fat_stress = now elif cmd == "0202" and len(p) >= 17: hr = int(p[14], 16) rr = int(p[15], 16) st = int(p[16], 16) if 30 <= hr <= 220 and 5 <= rr <= 60: hr_rr_count += 1 if hr_rr_count % 5 == 0: print(f" [{ts}] HR={hr} RR={rr} bed={st} (0x020B已收{count_0b}帧)", end='\r', flush=True) s.close() print(f"\n\n=== 3 分钟结束 ===") print(f" 0x020B 主动上报: {count_0b} 帧")