Files
ContecMonitor/fiveparaminte-main/scratch/dissect_45.py
T

57 lines
1.6 KiB
Python
Raw Normal View History

import struct
def read_u16_le(data: bytes, offset: int) -> int:
if offset + 2 > len(data):
return None
return struct.unpack_from('<H', data, offset)[0]
with open("/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main/scratch/raw_contec_stream.bin", "rb") as f:
stream = f.read()
# Extract 45-byte packets (subtype 22)
packets = []
consumed = 0
while consumed < len(stream) - 4:
b0 = stream[consumed]
b1 = stream[consumed + 1]
b2 = stream[consumed + 2]
b3 = stream[consumed + 3]
if (b2 == 0x04 or b2 == 0x01) and b3 == 0x46:
pkt_len = b0 | (b1 << 8)
if pkt_len == 45 and consumed + pkt_len <= len(stream):
packets.append(stream[consumed:consumed + pkt_len])
consumed += pkt_len
else:
if pkt_len < 5 or pkt_len > 8192:
consumed += 1
else:
consumed += pkt_len
else:
consumed += 1
print(f"Found {len(packets)} 45-byte packets:")
seen_timestamps = set()
for p in packets:
# Decode timestamp
year = read_u16_le(p, 8)
month = p[10]
day = p[11]
hour = p[12]
minute = p[13]
second = p[14]
ts_str = f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}"
if ts_str in seen_timestamps:
continue
seen_timestamps.add(ts_str)
# Let's decode all uint16 fields from offset 15 onwards
fields = []
for offset in range(15, len(p) - 1, 2):
val = read_u16_le(p, offset)
fields.append(f"off{offset}:{val}")
print(f"Timestamp: {ts_str} | Payload: {p[15:].hex()}")
print(" Decoded uint16s: " + ", ".join(fields))