import serial, time s = serial.Serial('COM9', 115200, timeout=0.3) print(f"[{time.strftime('%H:%M:%S')}] COM9 opened") # wait and collect print("\n=== Collecting 30 seconds of data ===\n") end = time.time() + 30 buf = b'' all_lines = [] while time.time() < end: data = s.read(1024) if data: buf += data while b'\n' in buf: line, buf = buf.split(b'\n', 1) all_lines.append(line.strip()) s.close() # 统计 counts = {} for line in all_lines: try: t = line.decode('ascii', errors='replace').strip() except: continue if t.startswith('SM7OK'): # 提取 command bytes parts = t.split(':')[1].strip().split() if ':' in t else [] if len(parts) >= 13: cmd_hex = f"{parts[11].upper()}{parts[12].upper()}" key = f"0x{cmd_hex}" else: key = "short" counts[key] = counts.get(key, 0) + 1 print(f" [{key}] {t[:120]}") print(f"\n=== Frame counts ===") for k, v in sorted(counts.items()): print(f" {k}: {v}") # 详细分析 0x0207 和 0x0209 和 0x0202 和 0x020B def analyze_frame(t): parts = t.split(':')[1].strip().split() if ':' in t else [] if len(parts) < 15: return cmd = f"{parts[11].upper()}{parts[12].upper()}" print(f"\n--- 0x{cmd} len={len(parts)} ---") print(f" RAW: {' '.join(parts)}") # 关键字节偏移(从 AA 开始算,parts[0]=EF, [1]=AA, [2]=55, ...) # Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # Field: EF AA 55 LENh LENl VER TYP SEQh SEQL CH RES CMDh CMDl D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 ... for i, p in enumerate(parts): print(f" [{i:2d}] 0x{p.upper()} = {int(p,16)}") print("\n\n=== Detailed 0x0207 frames ===") for line in all_lines: t = line.decode('ascii', errors='replace').strip() if t.startswith('SM7OK') and '02 07' in t: analyze_frame(t) print("\n\n=== Detailed 0x0209 frames ===") for line in all_lines: t = line.decode('ascii', errors='replace').strip() if t.startswith('SM7OK') and '02 09' in t: analyze_frame(t) print("\n\n=== Detailed 0x0202 frames ===") for line in all_lines[:3]: t = line.decode('ascii', errors='replace').strip() if t.startswith('SM7OK') and '02 02' in t: analyze_frame(t) print("\n\n=== Any 0x020B frames? ===") found_0b = False for line in all_lines: t = line.decode('ascii', errors='replace').strip() if t.startswith('SM7OK') and ('02 0B' in t or '0B ' in t.split(':')[1][:40] if ':' in t else False): found_0b = True analyze_frame(t) if not found_0b: print(" NO 0x020B frames found!") # 也打印 JSON 里的 pl2/qx2 值 print("\n\n=== JSON pl2/qx2 values ===") json_seen = 0 for line in all_lines: t = line.decode('ascii', errors='replace').strip() if t.startswith('{') and json_seen < 5: if 'pl2' in t or 'qx2' in t or 'ql2' in t or 'shuimian' in t or 'xinlv' in t: print(f" {t}") json_seen += 1