sm2_test.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import serial, time, json
  2. s = serial.Serial('COM9', 115200, timeout=0.1)
  3. print(f"[{time.strftime('%H:%M:%S')}] ✅ 打开")
  4. def watch(seconds, label):
  5. print(f"\n--- [{time.strftime('%H:%M:%S')}] {label} ({seconds}s) ---")
  6. line = b''
  7. end = time.time() + seconds
  8. while time.time() < end:
  9. data = s.read(512)
  10. for b in data:
  11. if b in (13, 10):
  12. if not line: continue
  13. t = line.decode('ascii', errors='replace')
  14. line = b''
  15. if t.startswith('{'):
  16. d = json.loads(t)
  17. xl2 = d.get('xl2','') or '(空)'
  18. hx2 = d.get('hx2','') or '(空)'
  19. sm2 = d.get('sm2','') or '(空)'
  20. zy2 = d.get('zy2','') or '(空)'
  21. xl1 = d.get('xl1','') or '(空)'
  22. xy1 = d.get('xy1','') or '(空)'
  23. flag = ''
  24. if xl2 != '(空)' and hx2 != '(空)': flag = ' ✅'
  25. elif xl2 == '(空)' and hx2 == '(空)': flag = ' ⚠️ 睡眠带空'
  26. print(f" sm2={sm2:<5} xl2={xl2:<6} hx2={hx2:<6} zy2={zy2:<5} | xl1={xl1:<6} xy1={xy1:<6}{flag}")
  27. elif t.startswith('SM7OK'):
  28. pass # 不刷屏
  29. elif t.startswith('DBG') or t.startswith('FRX') or t.startswith('FT6'):
  30. pass
  31. else:
  32. print(f" [DBG] {t[:100]}")
  33. else:
  34. line += bytes([b])
  35. # 1. 先看 5 秒当前睡眠带状态
  36. watch(5, "当前睡眠带状态")
  37. # 2. 发 sm2,02(停止)
  38. print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,02")
  39. s.write(b'{"zt":"sm2","neirong":"02"}\n')
  40. s.flush()
  41. watch(6, "停止睡眠带后")
  42. # 3. 发 sm2,01(启动)— 关键测试
  43. print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,01")
  44. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  45. s.flush()
  46. watch(25, "启动睡眠带 — 看 xl2/hx2 能不能回来")
  47. # 4. 再等 20 秒稳定性测试
  48. watch(20, "稳定性 — 再等 20 秒看心跳")
  49. s.close()
  50. print(f"\n[{time.strftime('%H:%M:%S')}] === 完成 ===")