Index: CN0_Python_Scripts/parser.py =================================================================== diff -u -r685d8eb11e2a49f2c8f7a270e379186415594601 -r855524c483c89b2cabf1b0fdb91227d5614b1639 --- CN0_Python_Scripts/parser.py (.../parser.py) (revision 685d8eb11e2a49f2c8f7a270e379186415594601) +++ CN0_Python_Scripts/parser.py (.../parser.py) (revision 855524c483c89b2cabf1b0fdb91227d5614b1639) @@ -1,6 +1,8 @@ import struct +from datetime import datetime +import data_analysis ######################################################################################################################## # This function parses and prints the received packet during automated polling @@ -24,10 +26,14 @@ _id, value, read_count, error = struct.unpack(' str: - # decode up to first NUL, tolerate missing terminator raw = field_bytes.split(b'\x00', 1)[0] return raw.decode('utf-8', errors='replace').strip() def take_field(max_len: int) -> bytes: - """ - Take up to max_len bytes from packet starting at i. - If fewer than max_len bytes remain, take what's left (tolerate truncated padding). - """ nonlocal i - chunk = packet[i:i + max_len] + chunk = packet[i:i+max_len] i += len(chunk) return chunk - # Read 3 fields, each up to MAX_VERSION_LENGTH, but do NOT require full padding bytes. fw_bytes = take_field(MAX_VERSION_LENGTH) hw_bytes = take_field(MAX_VERSION_LENGTH) sn_bytes = take_field(MAX_VERSION_LENGTH) @@ -84,13 +77,71 @@ hw = parse_ver_field(hw_bytes) sn = parse_ver_field(sn_bytes) - print("Versions:") - print(" FW:", fw) - print(" HW:", hw) - print(" SN:", sn) + # Return structured data + return { + "coeffs": coeffs, + "fw": fw, + "hw": hw, + "sn": sn, + } - # If there’s extra trailing data, show it (optional but useful) - if i < len(packet): - print(f"Trailing bytes ({len(packet)-i}): {packet[i:]}") ######################################################################################################################## + + +def verify_coeffs_and_versions(packet: bytes, + expected_coeffs, + expected_fw, + expected_hw, + expected_sn, + float_tol=1e-5): + parsed = parse_coeff_and_version(packet) + + coeffs_ok = True + coeff_compare = [] + for i, (exp, got) in enumerate(zip(expected_coeffs, parsed["coeffs"]), start=1): + match = abs(exp - got) <= float_tol + coeff_compare.append((i, exp, got, match)) + if not match: + coeffs_ok = False + + fw_ok = (parsed["fw"] == expected_fw) + hw_ok = (parsed["hw"] == expected_hw) + sn_ok = (parsed["sn"] == expected_sn) + + overall_ok = coeffs_ok and fw_ok and hw_ok and sn_ok + + return { + "overall_match": overall_ok, + # Coeffs already carry expected & received per row + "coeff_details": coeff_compare, + # Versions: include both expected and parsed for pretty printing + "versions": { + "fw": {"expected": expected_fw, "received": parsed["fw"], "match": fw_ok}, + "hw": {"expected": expected_hw, "received": parsed["hw"], "match": hw_ok}, + "sn": {"expected": expected_sn, "received": parsed["sn"], "match": sn_ok}, + }, + # Keep the raw parsed payload too (optional) + "parsed": parsed, + } + +######################################################################################################################## + +def print_verification_results(status): + print(f"Overall Match: {status['overall_match']}\n") + + # ---- Coeff Table ---- + print(f"{'Coeff':<8} {'Expected':>12} {'Received':>12} {'Match':>8}") + for idx, exp, got, ok in status["coeff_details"]: + print(f"{idx:<8} {exp:>12.2f} {got:>12.2f} {str(ok):>8}") + + # ---- Version Table ---- + print("\n" + f"{'Field':<12} {'Expected':>12} {'Received':>12} {'Match':>8}") + for field in ("fw", "hw", "sn"): + row = status["versions"][field] + label = field.upper() + print(f"{label:<12} {row['expected']:>12} {row['received']:>12} {str(row['match']):>8}") + + + +######################################################################################################################## \ No newline at end of file