| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import serial, time, json
- s = serial.Serial('COM9', 115200, timeout=0.1)
- print(f"[{time.strftime('%H:%M:%S')}] ✅ 打开")
- def watch(seconds, label):
- print(f"\n--- [{time.strftime('%H:%M:%S')}] {label} ({seconds}s) ---")
- line = b''
- end = time.time() + seconds
- 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 = d.get('sm2','') or '(空)'
- zy2 = d.get('zy2','') or '(空)'
- xl1 = d.get('xl1','') or '(空)'
- xy1 = d.get('xy1','') or '(空)'
- flag = ''
- if xl2 != '(空)' and hx2 != '(空)': flag = ' ✅'
- elif xl2 == '(空)' and hx2 == '(空)': flag = ' ⚠️ 睡眠带空'
- print(f" sm2={sm2:<5} xl2={xl2:<6} hx2={hx2:<6} zy2={zy2:<5} | xl1={xl1:<6} xy1={xy1:<6}{flag}")
- elif t.startswith('SM7OK'):
- pass # 不刷屏
- elif t.startswith('DBG') or t.startswith('FRX') or t.startswith('FT6'):
- pass
- else:
- print(f" [DBG] {t[:100]}")
- else:
- line += bytes([b])
- # 1. 先看 5 秒当前睡眠带状态
- watch(5, "当前睡眠带状态")
- # 2. 发 sm2,02(停止)
- print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,02")
- s.write(b'{"zt":"sm2","neirong":"02"}\n')
- s.flush()
- watch(6, "停止睡眠带后")
- # 3. 发 sm2,01(启动)— 关键测试
- print(f"\n[{time.strftime('%H:%M:%S')}] >>> TX sm2,01")
- s.write(b'{"zt":"sm2","neirong":"01"}\n')
- s.flush()
- watch(25, "启动睡眠带 — 看 xl2/hx2 能不能回来")
- # 4. 再等 20 秒稳定性测试
- watch(20, "稳定性 — 再等 20 秒看心跳")
- s.close()
- print(f"\n[{time.strftime('%H:%M:%S')}] === 完成 ===")
|