import serial, time, json s = serial.Serial('COM9', 115200, timeout=0.1) print(f"[{time.strftime('%H:%M:%S')}] ✅ COM9 打开成功") # 先疯狂看 8 秒,板子现在到底出了什么问题 print(f"\n=== 第一步:抓当前现场(8 秒,只读不发)===") line = b'' end = time.time() + 8 json_frames = [] while time.time() < end: data = s.read(512) for b in data: if b in (13, 10): if not line: continue t = line.decode('ascii', errors='replace') line = b'' if t.startswith('{'): try: d = json.loads(t) json_frames.append((time.time(), d)) except: print(f" JSON? {t[:100]}") elif t.startswith('SM7OK') or t.startswith('FRX') or t.startswith('FT6'): print(f" [DBG] {t[:120]}") else: print(f" [??] {t[:120]}") else: line += bytes([b]) if json_frames: print(f"\n 共抓到 {len(json_frames)} 帧 JSON,最新帧:") t, d = json_frames[-1] for k in ['xl1','xy1','sm2','pl2','zy2','xl2','hx2','bphr','sdnn','rmssd']: v = d.get(k, '') print(f" {k} = {v or '(空)'}") else: print("\n ❌ 8 秒内一帧 JSON 都没收到!板子可能卡死了!") # 第二步:发命令试试看板子还有没有反应 print(f"\n=== 第二步:发命令测试(每 3 秒一条)===") cmds = [ ('{"zt":"sm2","neirong":"01"}', 'sm2 启动'), ('{"zt":"xy1","neirong":"01"}', 'xy1 启动'), ] for cmd, desc in cmds: print(f"\n[{time.strftime('%H:%M:%S')}] TX → {cmd} ({desc})") s.write((cmd + '\n').encode()) s.flush() time.sleep(3) # 等下一个 JSON 帧 end2 = time.time() + 3 got_response = False while time.time() < end2: data = s.read(256) for b in data: if b in (13, 10): if not line: continue t = line.decode('ascii', errors='replace') line = b'' if t.startswith('{'): d = json.loads(t) xl1 = d.get('xl1','') or '(空)' xy1 = d.get('xy1','') or '(空)' sm2 = d.get('sm2','') or '(空)' xl2 = d.get('xl2','') or '(空)' print(f" 响应: xl1={xl1} xy1={xy1} sm2={sm2} xl2={xl2}") got_response = True else: line += bytes([b]) if not got_response: print(f" ⚠️ 3 秒内板子没响应!") # 第三步:等 15 秒再看心跳有没有恢复 print(f"\n=== 第三步:等 15 秒,看心跳是否持续 ===") end3 = time.time() + 15 heartbeat_ok = False hb_count = 0 while time.time() < end3: data = s.read(512) for b in data: if b in (13, 10): if not line: continue t = line.decode('ascii', errors='replace') line = b'' if t.startswith('{'): d = json.loads(t) hb_count += 1 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 '(空)'}") heartbeat_ok = True else: line += bytes([b]) s.close() print(f"\n=== 最终诊断 ===") if heartbeat_ok: print(f" ✅ 板子主循环在跑!15 秒内心跳 {hb_count} 帧") else: print(f" ❌ 板子可能卡死!15 秒内没有心跳") print(f" 总共 JSON: {len(json_frames) + hb_count} 帧")