import serial, time, zlib, json def calc_crc32(data): crc = 0xFFFFFFFF for b in data: crc ^= b for _ in range(8): if crc & 1: crc = (crc >> 1) ^ 0xEDB88320 else: crc >>= 1 return (crc ^ 0xFFFFFFFF) & 0xFFFFFFFF s = serial.Serial('COM9', 115200, timeout=0.3) print(f"[{time.strftime('%H:%M:%S')}] COM9 open") s.reset_input_buffer() # 先让板子重启 sm2 s.write(b'{"zt":"sm2","neirong":"00"}\n') time.sleep(0.5) s.reset_input_buffer() s.write(b'{"zt":"sm2","neirong":"01"}\n') print(f"[{time.strftime('%H:%M:%S')}] sm2 restarted") # 抓 60 秒 print("\n=== 抓 60 秒(请躺好不动,让模块计算疲劳/压力)===\n") end = time.time() + 60 buf = b'' all_sm7ok = [] 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'): all_sm7ok.append(t) # 快速解析 command if ':' in t: p = t.split(':')[1].strip().split() if len(p) >= 13: cmd = f"{p[11]}{p[12]}" detail = "" if cmd == "020B" and len(p) >= 20: detail = f"sleep={p[14]} fatigue={p[15]} breath={p[16]} s1={p[17]} s2={p[18]} bed={p[19]} ★★★" elif cmd == "0207": detail = f"fatigue={p[14]}" elif cmd == "0209" and len(p) >= 17: detail = f"s1={p[14]} s2={p[15]} bed={p[16]}" elif cmd == "0202": detail = f"HR={p[14]} RR={p[15]} bed={p[16]}" print(f" [{cmd}] {detail}") elif t.startswith('{') and 'pl2' in t: last_json = t s.close() # 最终统计 print(f"\n=== 统计 ===") counts = {} for t in all_sm7ok: if ':' in t: p = t.split(':')[1].strip().split() if len(p) >= 13: cmd = f"0x{p[11].upper()}{p[12].upper()}" counts[cmd] = counts.get(cmd, 0) + 1 for k, v in sorted(counts.items()): print(f" {k}: {v} 帧") # 所有 0x020B 帧完整拆解 print(f"\n=== ★ 所有 0x020B 组数据上报帧 ===") for t in all_sm7ok: if ': 02 0B' in t or ':' in t and '02 0B' in t.split(':')[1][:40]: p = t.split(':')[1].strip().split() if len(p) >= 20: print(f" 帧长={len(p)}: {' '.join(p)}") print(f" sleep={p[14]} fatigue={p[15]} breath_hold={p[16]} stress1={p[17]} stress2={p[18]} bed_state={p[19]}") crc_in = bytes([int(x,16) for x in p[1:-5]]) calc_c = calc_crc32(crc_in) recv_c = (int(p[-5],16)<<24)|(int(p[-4],16)<<16)|(int(p[-3],16)<<8)|int(p[-2],16) print(f" CRC32: calc=0x{calc_c:08X} recv=0x{recv_c:08X} {'✅' if calc_c==recv_c else '❌'}") # 所有 0x0207 疲劳查询回复 print(f"\n=== 所有 0x0207 疲劳查询回复 ===") prev = None for t in all_sm7ok: if ': 02 07' in t: p = t.split(':')[1].strip().split() if len(p) >= 15: crc_in = bytes([int(x,16) for x in p[1:-5]]) calc_c = calc_crc32(crc_in) recv_c = (int(p[-5],16)<<24)|(int(p[-4],16)<<16)|(int(p[-3],16)<<8)|int(p[-2],16) is_same = "重复" if prev and t == prev else "新值" print(f" Err={p[13]} fatigue={p[14]} CRC={'✅' if calc_c==recv_c else '❌'} {is_same}") prev = t # 所有 0x0209 压力查询回复 print(f"\n=== 所有 0x0209 压力查询回复 ===") for t in all_sm7ok: if ': 02 09' in t: p = t.split(':')[1].strip().split() if len(p) >= 17: print(f" Err={p[13]} stress1={p[14]} stress2={p[15]} bed={p[16]}") # Enable 回复 print(f"\n=== Enable 开关回复 ===") for t in all_sm7ok: if ': 02 01' in t or ': 02 0A' in t: p = t.split(':')[1].strip().split() if len(p) >= 15 and p[6] == "02": print(f" Cmd=0x{p[11].upper()}{p[12].upper()} Err={p[13]}") # JSON print(f"\n=== 最终 JSON ===") if last_json: j = json.loads(last_json) print(f" pl2(疲劳)={j['pl2']} qx2(压力)={j['qx2']} sm2={j['sm2']}") print(f" xl2(心率)={j['xl2']} hx2(呼吸)={j['hx2']} bq2(憋气)={j['bq2']} zy2(在床)={j['zy2']} shuimian={j.get('shuimian','?')}") print("\nDone")