post_flash2.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import serial, time, zlib, json
  2. def calc_crc32(data):
  3. crc = 0xFFFFFFFF
  4. for b in data:
  5. crc ^= b
  6. for _ in range(8):
  7. if crc & 1: crc = (crc >> 1) ^ 0xEDB88320
  8. else: crc >>= 1
  9. return (crc ^ 0xFFFFFFFF) & 0xFFFFFFFF
  10. s = serial.Serial('COM9', 115200, timeout=0.3)
  11. print(f"[{time.strftime('%H:%M:%S')}] COM9 open")
  12. s.reset_input_buffer()
  13. # 先让板子重启 sm2
  14. s.write(b'{"zt":"sm2","neirong":"00"}\n')
  15. time.sleep(0.5)
  16. s.reset_input_buffer()
  17. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  18. print(f"[{time.strftime('%H:%M:%S')}] sm2 restarted")
  19. # 抓 60 秒
  20. print("\n=== 抓 60 秒(请躺好不动,让模块计算疲劳/压力)===\n")
  21. end = time.time() + 60
  22. buf = b''
  23. all_sm7ok = []
  24. last_json = None
  25. while time.time() < end:
  26. data = s.read(2048)
  27. if data:
  28. buf += data
  29. while b'\n' in buf:
  30. line, buf = buf.split(b'\n', 1)
  31. try: t = line.decode('ascii', errors='replace').strip()
  32. except: continue
  33. if t.startswith('SM7OK'):
  34. all_sm7ok.append(t)
  35. # 快速解析 command
  36. if ':' in t:
  37. p = t.split(':')[1].strip().split()
  38. if len(p) >= 13:
  39. cmd = f"{p[11]}{p[12]}"
  40. detail = ""
  41. if cmd == "020B" and len(p) >= 20:
  42. detail = f"sleep={p[14]} fatigue={p[15]} breath={p[16]} s1={p[17]} s2={p[18]} bed={p[19]} ★★★"
  43. elif cmd == "0207":
  44. detail = f"fatigue={p[14]}"
  45. elif cmd == "0209" and len(p) >= 17:
  46. detail = f"s1={p[14]} s2={p[15]} bed={p[16]}"
  47. elif cmd == "0202":
  48. detail = f"HR={p[14]} RR={p[15]} bed={p[16]}"
  49. print(f" [{cmd}] {detail}")
  50. elif t.startswith('{') and 'pl2' in t:
  51. last_json = t
  52. s.close()
  53. # 最终统计
  54. print(f"\n=== 统计 ===")
  55. counts = {}
  56. for t in all_sm7ok:
  57. if ':' in t:
  58. p = t.split(':')[1].strip().split()
  59. if len(p) >= 13:
  60. cmd = f"0x{p[11].upper()}{p[12].upper()}"
  61. counts[cmd] = counts.get(cmd, 0) + 1
  62. for k, v in sorted(counts.items()):
  63. print(f" {k}: {v} 帧")
  64. # 所有 0x020B 帧完整拆解
  65. print(f"\n=== ★ 所有 0x020B 组数据上报帧 ===")
  66. for t in all_sm7ok:
  67. if ': 02 0B' in t or ':' in t and '02 0B' in t.split(':')[1][:40]:
  68. p = t.split(':')[1].strip().split()
  69. if len(p) >= 20:
  70. print(f" 帧长={len(p)}: {' '.join(p)}")
  71. print(f" sleep={p[14]} fatigue={p[15]} breath_hold={p[16]} stress1={p[17]} stress2={p[18]} bed_state={p[19]}")
  72. crc_in = bytes([int(x,16) for x in p[1:-5]])
  73. calc_c = calc_crc32(crc_in)
  74. recv_c = (int(p[-5],16)<<24)|(int(p[-4],16)<<16)|(int(p[-3],16)<<8)|int(p[-2],16)
  75. print(f" CRC32: calc=0x{calc_c:08X} recv=0x{recv_c:08X} {'✅' if calc_c==recv_c else '❌'}")
  76. # 所有 0x0207 疲劳查询回复
  77. print(f"\n=== 所有 0x0207 疲劳查询回复 ===")
  78. prev = None
  79. for t in all_sm7ok:
  80. if ': 02 07' in t:
  81. p = t.split(':')[1].strip().split()
  82. if len(p) >= 15:
  83. crc_in = bytes([int(x,16) for x in p[1:-5]])
  84. calc_c = calc_crc32(crc_in)
  85. recv_c = (int(p[-5],16)<<24)|(int(p[-4],16)<<16)|(int(p[-3],16)<<8)|int(p[-2],16)
  86. is_same = "重复" if prev and t == prev else "新值"
  87. print(f" Err={p[13]} fatigue={p[14]} CRC={'✅' if calc_c==recv_c else '❌'} {is_same}")
  88. prev = t
  89. # 所有 0x0209 压力查询回复
  90. print(f"\n=== 所有 0x0209 压力查询回复 ===")
  91. for t in all_sm7ok:
  92. if ': 02 09' in t:
  93. p = t.split(':')[1].strip().split()
  94. if len(p) >= 17:
  95. print(f" Err={p[13]} stress1={p[14]} stress2={p[15]} bed={p[16]}")
  96. # Enable 回复
  97. print(f"\n=== Enable 开关回复 ===")
  98. for t in all_sm7ok:
  99. if ': 02 01' in t or ': 02 0A' in t:
  100. p = t.split(':')[1].strip().split()
  101. if len(p) >= 15 and p[6] == "02":
  102. print(f" Cmd=0x{p[11].upper()}{p[12].upper()} Err={p[13]}")
  103. # JSON
  104. print(f"\n=== 最终 JSON ===")
  105. if last_json:
  106. j = json.loads(last_json)
  107. print(f" pl2(疲劳)={j['pl2']} qx2(压力)={j['qx2']} sm2={j['sm2']}")
  108. print(f" xl2(心率)={j['xl2']} hx2(呼吸)={j['hx2']} bq2(憋气)={j['bq2']} zy2(在床)={j['zy2']} shuimian={j.get('shuimian','?')}")
  109. print("\nDone")