final_test.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import serial, time, json, os
  2. s = serial.Serial('COM9', 115200, timeout=0.1)
  3. print(f"[{time.strftime('%H:%M:%S')}] ✅ COM9 打开")
  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_f = d.get('sm2','') or '(空)'
  21. xl1 = d.get('xl1','') or '(空)'
  22. xy1 = d.get('xy1','') or '(空)'
  23. zy2 = d.get('zy2','') or '(空)'
  24. flag = ''
  25. if xl2 != '(空)' and hx2 != '(空)': flag += ' ✅SM2'
  26. elif xl2 == '(空)' and hx2 == '(空)': flag += ' ⚠️SM2空'
  27. if xl1 != '(空)' and xy1 != '(空)': flag += ' ✅XY1'
  28. elif xl1 == '(空)' and xy1 == '(空)': flag += ' ⚠️XY1空'
  29. print(f" #{cnt+1:<2} sm2={sm2_f:<5} xl2={xl2:<6} hx2={hx2:<6} zy2={zy2:<5} | xl1={xl1:<6} xy1={xy1:<6}{flag}")
  30. cnt += 1
  31. elif t.startswith('SM7OK'):
  32. pass # 不刷屏
  33. elif not t.startswith('DBG') and not t.startswith('FRX') and not t.startswith('FT6'):
  34. print(f" [??] {t[:100]}")
  35. else:
  36. line += bytes([b])
  37. return cnt
  38. # 1. 先看 5 秒当前状态
  39. watch(5, "烧录后首次状态")
  40. # 2. 发 xy1,01
  41. print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX xy1,01")
  42. s.write(b'{"zt":"xy1","neirong":"01"}\n')
  43. s.flush()
  44. # 3. 发 sm2,01
  45. print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,01")
  46. s.write(b'{"zt":"sm2","neirong":"01"}\n')
  47. s.flush()
  48. # 4. 等 40 秒(关键验证 — 睡眠带能不能撑过之前的 20 秒掉线点)
  49. cnt = watch(40, "双启动后连续观察 40 秒")
  50. # 5. 总结
  51. s.close()
  52. print(f"\n=== 最终结论 ===")
  53. print(f" 40 秒内 JSON 帧数: {cnt}")