| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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
- 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 = d.get('sm2','') or '(空)'
- xl1 = d.get('xl1','') or '(空)'
- xy1 = d.get('xy1','') or '(空)'
- flag = ''
- if xl2 != '(空)' and hx2 != '(空)': flag = ' ✅'
- elif xl2 == '(空)' and hx2 == '(空)': flag = ' ⚠️ 睡眠带空'
- print(f" #{cnt+1:<2} sm2={sm2:<5} xl2={xl2:<6} hx2={hx2:<6} | xl1={xl1:<6} xy1={xy1:<6}{flag}")
- cnt += 1
- elif t.startswith('SM7OK'):
- # 打印原始帧类型
- print(f" [SM7OK] {t}")
- 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. 当前状态
- 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()
- time.sleep(3)
- # 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. 等 30 秒,观察睡眠带能撑多久
- cnt = watch(30, "sm2,01 后观察 30 秒")
- # 5. 再发一次 sm2,01(重开)
- print(f"\n[{time.strftime('%H:%M:%S')}] >>> 再发一次 sm2,01")
- s.write(b'{"zt":"sm2","neirong":"01"}\n')
- s.flush()
- cnt = watch(20, "重发后再看 20 秒")
- s.close()
- print(f"\n=== 完成 ===")
|