# -*- coding: utf-8 -*- # 用一条已知合法的 aa55 帧反推帧格式(校验和算法)。不需要 BLE。 frame_hex = "aa55001700010000030100071011a201024200e154" b = bytes.fromhex(frame_hex) print("total bytes:", len(b), b.hex()) def chk_sum(data): return sum(data) & 0xff def chk_comp(data): return (0x100 - sum(data)) & 0xff def chk_xor(data): r = 0 for x in data: r ^= x return r def chk_crc8(data): crc = 0 for x in data: crc ^= x for _ in range(8): crc = ((crc << 1) ^ 0x07) & 0xff if (crc & 0x80) else (crc << 1) & 0xff return crc algos = {"sum": chk_sum, "comp": chk_comp, "xor": chk_xor, "crc8": chk_crc8} header = 2 # aa55 for lf in [0, 1, 2]: payload_start = header + lf if payload_start >= len(b): continue for s in range(payload_start + 1, len(b)): payload = b[payload_start:s] chkbytes = b[s:] for an, algo in algos.items(): for scope in ["hp", "p"]: data = (b[:payload_start] + payload) if scope == "hp" else payload c = algo(data) if len(chkbytes) == 1 and c == chkbytes[0]: print(f"[1B] lf={lf} split={s} algo={an} scope={scope} -> MATCH chk={c:02x}") if len(chkbytes) == 2: if c == int.from_bytes(chkbytes, "big"): print(f"[2B] lf={lf} split={s} algo={an} scope={scope} BE -> MATCH") if c == int.from_bytes(chkbytes, "little"): print(f"[2B] lf={lf} split={s} algo={an} scope={scope} LE -> MATCH") # 额外:列出各 split 下的候选 (type/len 字节 + payload + chk) print("\n--- 候选分段 ---") for lf in [0, 1, 2]: ps = header + lf if ps >= len(b): continue if lf == 1: print(f"lf=1: type/len byte = {b[header]:02x}") if lf == 2: print(f"lf=2: len bytes BE={b[header]:02x}{b[header+1]:02x}={int.from_bytes(b[header:header+2],'big')} " f"LE={int.from_bytes(b[header:header+2],'little')}")