long_capture.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import serial, time, json
  2. s = serial.Serial('COM9', 115200, timeout=0.3)
  3. # 关再开 sm2 重启睡眠带
  4. s.write(b'{"zt":"sm2","neirong":"00"}\n')
  5. time.sleep(1)
  6. s.reset_input_buffer()
  7. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  8. print(f"[{time.strftime('%H:%M:%S')}] sm2 restarted — 请躺好不动!")
  9. # 抓 120 秒
  10. print("\n=== 抓 120 秒(躺好别动!)===")
  11. end = time.time() + 120
  12. buf = b''
  13. prev_fat = -1
  14. prev_stress = -1
  15. prev_0b_fat = -1
  16. prev_0b_stress = -1
  17. last_json = None
  18. while time.time() < end:
  19. data = s.read(2048)
  20. if data:
  21. buf += data
  22. while b'\n' in buf:
  23. line, buf = buf.split(b'\n', 1)
  24. try: t = line.decode('ascii', errors='replace').strip()
  25. except: continue
  26. if t.startswith('SM7OK') and ':' in t:
  27. p = t.split(':')[1].strip().split()
  28. if len(p) < 13: continue
  29. cmd = f"{p[11]}{p[12]}"
  30. # 只在值变化时打印
  31. if cmd == "0207" and len(p) >= 15:
  32. f_val = int(p[14], 16)
  33. if f_val != prev_fat:
  34. print(f" 0x0207 疲劳查询: fatigue={f_val}", flush=True)
  35. prev_fat = f_val
  36. elif cmd == "0209" and len(p) >= 17:
  37. s_val = int(p[14], 16)
  38. if s_val != prev_stress:
  39. print(f" 0x0209 压力查询: stress={s_val} bed={p[16]}", flush=True)
  40. prev_stress = s_val
  41. elif cmd == "020B" and len(p) >= 20:
  42. f0b = int(p[15], 16)
  43. s0b = int(p[17], 16)
  44. if f0b != prev_0b_fat or s0b != prev_0b_stress:
  45. print(f" ★ 0x020B 主动上报: sleep={p[14]} fatigue={f0b} stress={s0b} bed={p[19]}", flush=True)
  46. prev_0b_fat = f0b
  47. prev_0b_stress = s0b
  48. elif t.startswith('{') and 'pl2' in t:
  49. try: last_json = json.loads(t)
  50. except: pass
  51. s.close()
  52. print(f"\n=== 120 秒结果 ===")
  53. if last_json:
  54. 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']}")
  55. print(f" 疲劳查询值变化过: {prev_fat >= 0} (最后={prev_fat})")
  56. print(f" 压力查询值变化过: {prev_stress >= 0} (最后={prev_stress})")
  57. print(f" 0x020B 主动上报出现过: {prev_0b_fat >= 0}")