urgent_diag.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import serial, time, json
  2. s = serial.Serial('COM9', 115200, timeout=0.1)
  3. print(f"[{time.strftime('%H:%M:%S')}] ✅ COM9 打开成功")
  4. # 先疯狂看 8 秒,板子现在到底出了什么问题
  5. print(f"\n=== 第一步:抓当前现场(8 秒,只读不发)===")
  6. line = b''
  7. end = time.time() + 8
  8. json_frames = []
  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. try:
  18. d = json.loads(t)
  19. json_frames.append((time.time(), d))
  20. except:
  21. print(f" JSON? {t[:100]}")
  22. elif t.startswith('SM7OK') or t.startswith('FRX') or t.startswith('FT6'):
  23. print(f" [DBG] {t[:120]}")
  24. else:
  25. print(f" [??] {t[:120]}")
  26. else:
  27. line += bytes([b])
  28. if json_frames:
  29. print(f"\n 共抓到 {len(json_frames)} 帧 JSON,最新帧:")
  30. t, d = json_frames[-1]
  31. for k in ['xl1','xy1','sm2','pl2','zy2','xl2','hx2','bphr','sdnn','rmssd']:
  32. v = d.get(k, '')
  33. print(f" {k} = {v or '(空)'}")
  34. else:
  35. print("\n ❌ 8 秒内一帧 JSON 都没收到!板子可能卡死了!")
  36. # 第二步:发命令试试看板子还有没有反应
  37. print(f"\n=== 第二步:发命令测试(每 3 秒一条)===")
  38. cmds = [
  39. ('{"zt":"sm2","neirong":"01"}', 'sm2 启动'),
  40. ('{"zt":"xy1","neirong":"01"}', 'xy1 启动'),
  41. ]
  42. for cmd, desc in cmds:
  43. print(f"\n[{time.strftime('%H:%M:%S')}] TX → {cmd} ({desc})")
  44. s.write((cmd + '\n').encode())
  45. s.flush()
  46. time.sleep(3)
  47. # 等下一个 JSON 帧
  48. end2 = time.time() + 3
  49. got_response = False
  50. while time.time() < end2:
  51. data = s.read(256)
  52. for b in data:
  53. if b in (13, 10):
  54. if not line: continue
  55. t = line.decode('ascii', errors='replace')
  56. line = b''
  57. if t.startswith('{'):
  58. d = json.loads(t)
  59. xl1 = d.get('xl1','') or '(空)'
  60. xy1 = d.get('xy1','') or '(空)'
  61. sm2 = d.get('sm2','') or '(空)'
  62. xl2 = d.get('xl2','') or '(空)'
  63. print(f" 响应: xl1={xl1} xy1={xy1} sm2={sm2} xl2={xl2}")
  64. got_response = True
  65. else:
  66. line += bytes([b])
  67. if not got_response:
  68. print(f" ⚠️ 3 秒内板子没响应!")
  69. # 第三步:等 15 秒再看心跳有没有恢复
  70. print(f"\n=== 第三步:等 15 秒,看心跳是否持续 ===")
  71. end3 = time.time() + 15
  72. heartbeat_ok = False
  73. hb_count = 0
  74. while time.time() < end3:
  75. data = s.read(512)
  76. for b in data:
  77. if b in (13, 10):
  78. if not line: continue
  79. t = line.decode('ascii', errors='replace')
  80. line = b''
  81. if t.startswith('{'):
  82. d = json.loads(t)
  83. hb_count += 1
  84. print(f" 心跳#{hb_count} ✅ {time.strftime('%H:%M:%S')} xl1={d.get('xl1','') or '(空)'} xy1={d.get('xy1','') or '(空)'} sm2={d.get('sm2','') or '(空)'} xl2={d.get('xl2','') or '(空)'}")
  85. heartbeat_ok = True
  86. else:
  87. line += bytes([b])
  88. s.close()
  89. print(f"\n=== 最终诊断 ===")
  90. if heartbeat_ok:
  91. print(f" ✅ 板子主循环在跑!15 秒内心跳 {hb_count} 帧")
  92. else:
  93. print(f" ❌ 板子可能卡死!15 秒内没有心跳")
  94. print(f" 总共 JSON: {len(json_frames) + hb_count} 帧")