import serial, time, json, os s = serial.Serial('COM9', 115200, timeout=0.1) print(f"[{time.strftime('%H:%M:%S')}] ✅ COM9 打开") def watch(seconds, label): print(f"\n--- [{time.strftime('%H:%M:%S')}] {label} ({seconds}s) ---") line = b'' end = time.time() + seconds cnt = 0 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('{'): d = json.loads(t) xl2 = d.get('xl2','') or '(空)' hx2 = d.get('hx2','') or '(空)' sm2_f = d.get('sm2','') or '(空)' xl1 = d.get('xl1','') or '(空)' xy1 = d.get('xy1','') or '(空)' zy2 = d.get('zy2','') or '(空)' flag = '' if xl2 != '(空)' and hx2 != '(空)': flag += ' ✅SM2' elif xl2 == '(空)' and hx2 == '(空)': flag += ' ⚠️SM2空' if xl1 != '(空)' and xy1 != '(空)': flag += ' ✅XY1' elif xl1 == '(空)' and xy1 == '(空)': flag += ' ⚠️XY1空' 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}") cnt += 1 elif t.startswith('SM7OK'): pass # 不刷屏 elif not t.startswith('DBG') and not t.startswith('FRX') and not t.startswith('FT6'): print(f" [??] {t[:100]}") else: line += bytes([b]) return cnt # 1. 先看 5 秒当前状态 watch(5, "烧录后首次状态") # 2. 发 xy1,01 print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX xy1,01") s.write(b'{"zt":"xy1","neirong":"01"}\n') s.flush() # 3. 发 sm2,01 print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,01") s.write(b'{"zt":"sm2","neirong":"01"}\n') s.flush() # 4. 等 40 秒(关键验证 — 睡眠带能不能撑过之前的 20 秒掉线点) cnt = watch(40, "双启动后连续观察 40 秒") # 5. 总结 s.close() print(f"\n=== 最终结论 ===") print(f" 40 秒内 JSON 帧数: {cnt}")