wait_020b.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import serial, time, json
  2. s = serial.Serial('COM8', 115200, timeout=0.3)
  3. time.sleep(0.1)
  4. # 关再开
  5. s.write(b'{"zt":"sm2","neirong":"00"}\n')
  6. time.sleep(1)
  7. s.reset_input_buffer()
  8. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  9. print(f"[{time.strftime('%H:%M:%S')}] sm2 started — 躺好别动!")
  10. time.sleep(3)
  11. print(f"\n=== 等 3 分钟 (无查询轮询) ===\n")
  12. end = time.time() + 180
  13. buf = b''
  14. count_0b = 0
  15. hr_rr_count = 0
  16. prev_fat_stress = None
  17. while time.time() < end:
  18. data = s.read(2048)
  19. if data:
  20. buf += data
  21. while b'\n' in buf:
  22. line, buf = buf.split(b'\n', 1)
  23. try: t = line.decode('ascii', errors='replace').strip()
  24. except: continue
  25. if t.startswith('SM7OK') and ':' in t:
  26. p = t.split(':')[1].strip().split()
  27. if len(p) < 13: continue
  28. cmd = f"{p[11]}{p[12]}"
  29. ts = time.strftime('%H:%M:%S')
  30. if cmd == "020B" and len(p) >= 20:
  31. sleep_v = int(p[14], 16)
  32. fat_v = int(p[15], 16)
  33. brh_v = int(p[16], 16)
  34. s1_v = int(p[17], 16)
  35. s2_v = int(p[18], 16)
  36. bed_v = int(p[19], 16)
  37. count_0b += 1
  38. now = (fat_v, s1_v)
  39. if now != prev_fat_stress:
  40. marker = "★★★" if (fat_v > 0 or s1_v > 0) else " "
  41. print(f" [{ts}] {marker} 0x020B #{count_0b}: sleep={sleep_v} fatigue={fat_v} stress={s1_v} bed={bed_v}", flush=True)
  42. prev_fat_stress = now
  43. elif cmd == "0202" and len(p) >= 17:
  44. hr = int(p[14], 16)
  45. rr = int(p[15], 16)
  46. st = int(p[16], 16)
  47. if 30 <= hr <= 220 and 5 <= rr <= 60:
  48. hr_rr_count += 1
  49. if hr_rr_count % 5 == 0:
  50. print(f" [{ts}] HR={hr} RR={rr} bed={st} (0x020B已收{count_0b}帧)", end='\r', flush=True)
  51. s.close()
  52. print(f"\n\n=== 3 分钟结束 ===")
  53. print(f" 0x020B 主动上报: {count_0b} 帧")