sm2_test2.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. cnt = 0
  9. while time.time() < end:
  10. data = s.read(512)
  11. for b in data:
  12. if b in (13, 10):
  13. if not line: continue
  14. t = line.decode('ascii', errors='replace')
  15. line = b''
  16. if t.startswith('{'):
  17. d = json.loads(t)
  18. xl2 = d.get('xl2','') or '(空)'
  19. hx2 = d.get('hx2','') or '(空)'
  20. sm2 = d.get('sm2','') 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" #{cnt+1:<2} sm2={sm2:<5} xl2={xl2:<6} hx2={hx2:<6} | xl1={xl1:<6} xy1={xy1:<6}{flag}")
  27. cnt += 1
  28. elif t.startswith('SM7OK'):
  29. # 打印原始帧类型
  30. print(f" [SM7OK] {t}")
  31. elif not t.startswith('DBG') and not t.startswith('FRX') and not t.startswith('FT6'):
  32. print(f" [??] {t[:100]}")
  33. else:
  34. line += bytes([b])
  35. return cnt
  36. # 1. 当前状态
  37. watch(5, "当前睡眠带状态")
  38. # 2. 发 sm2,02 清掉
  39. print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,02")
  40. s.write(b'{"zt":"sm2","neirong":"02"}\n')
  41. s.flush()
  42. time.sleep(3)
  43. # 3. 发 sm2,01 启动
  44. print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,01")
  45. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  46. s.flush()
  47. # 4. 等 30 秒,观察睡眠带能撑多久
  48. cnt = watch(30, "sm2,01 后观察 30 秒")
  49. # 5. 再发一次 sm2,01(重开)
  50. print(f"\n[{time.strftime('%H:%M:%S')}] >>> 再发一次 sm2,01")
  51. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  52. s.flush()
  53. cnt = watch(20, "重发后再看 20 秒")
  54. s.close()
  55. print(f"\n=== 完成 ===")