From a8cc5166017ee0330842d3457b2ac7e8e75c5996 Mon Sep 17 00:00:00 2001 From: serversdown Date: Sun, 25 Oct 2026 17:14:11 -0400 Subject: [PATCH] fix(minimateplus): SUB 0x15's serial is a C string -- cut at the first NUL BE9441 (MiniMate bench, 2026-10-25) answers b"BE9441\x00?": six characters, the terminator, a leftover byte. rstrip(b"\x00") kept the "?", so the serial read "BE9441\x00?" and the serial-named path refused every request as the wrong unit (409). Seven-character serials fill the field to the terminator, which is why no earlier unit showed it. The legacy /device/info output is cleaned too. No codec, store or DB change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- minimateplus/client.py | 7 +++++-- tests/test_minimate_serial_decode.py | 30 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/test_minimate_serial_decode.py diff --git a/minimateplus/client.py b/minimateplus/client.py index 599ae95..e5002bf 100644 --- a/minimateplus/client.py +++ b/minimateplus/client.py @@ -1305,12 +1305,15 @@ def _decode_serial_number(data: bytes) -> DeviceInfo: # Actual serial payload starts at data[11]. actual = data[11:] if len(data) > 11 else data + # The serial is a C string: cut at the first NUL. A short serial leaves a + # stray byte after its terminator (BE9441 sends b"BE9441\x00?"), so + # stripping trailing NULs alone is not enough. if len(actual) < 9: # Short payload — gracefully degrade - serial = actual.rstrip(b"\x00").decode("ascii", errors="replace") + serial = actual.split(b"\x00", 1)[0].decode("ascii", errors="replace") return DeviceInfo(serial=serial, firmware_minor=0) - serial = actual[:8].rstrip(b"\x00").decode("ascii", errors="replace") + serial = actual[:8].split(b"\x00", 1)[0].decode("ascii", errors="replace") trail_0 = actual[8] if len(actual) > 8 else None fw_minor = actual[9] if len(actual) > 9 else 0 diff --git a/tests/test_minimate_serial_decode.py b/tests/test_minimate_serial_decode.py new file mode 100644 index 0000000..ac7c01d --- /dev/null +++ b/tests/test_minimate_serial_decode.py @@ -0,0 +1,30 @@ +"""SUB 0x15's serial is a C string in an 8-byte field: cut at the first NUL. + +BE9441 (bench, 2026-10-25) answers `BE9441\\x00?` -- six characters, the +terminator, then a leftover byte. Stripping only trailing NULs kept the `?`, so +the serial read as "BE9441\\x00?" and every serial-named request was refused as +the wrong unit. Seven-character serials (BE11529, BE18189) fill the field up to +the terminator, which is why this never showed before. +""" +from __future__ import annotations + +from minimateplus.client import _decode_serial_number + +_HEADER = bytes(11) # data[0:11]: the section header + + +def _reply(field8: bytes, trail: int = 0x00, fw_minor: int = 0x11) -> bytes: + return _HEADER + field8 + bytes([trail, fw_minor]) + + +def test_a_six_character_serial_stops_at_its_terminator(): + assert _decode_serial_number(_reply(b"BE9441\x00?")).serial == "BE9441" + + +def test_a_seven_character_serial_is_unchanged(): + info = _decode_serial_number(_reply(b"BE18189\x00", trail=0x2A, fw_minor=0x11)) + assert (info.serial, info.firmware_minor) == ("BE18189", 0x11) + + +def test_a_short_payload_also_stops_at_the_terminator(): + assert _decode_serial_number(_HEADER + b"BE9441\x00?").serial == "BE9441"