From 6b875e161b253123c4be5eb838aa7b2e5f112964 Mon Sep 17 00:00:00 2001 From: Brian Harrison Date: Mon, 27 Apr 2026 23:29:53 -0400 Subject: [PATCH] fix(protocol): implement auto-detection of frame mode based on probe response size for accurate chunk handling --- minimateplus/protocol.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/minimateplus/protocol.py b/minimateplus/protocol.py index f85b977..159a1a9 100644 --- a/minimateplus/protocol.py +++ b/minimateplus/protocol.py @@ -614,6 +614,34 @@ class MiniMateProtocol: [f"0x{f.page_key:04X}" for f in probe_batch], ) + # Auto-detect frame mode from probe response size. + # + # Two observed TCP modes (confirmed from 4-26-26 and 4-27-26 captures): + # + # 1-frame mode (RS-232 / fast TCP): probe returns 1 large frame (~1100 B). + # Each subsequent chunk also returns 1 large frame. The probe contributes + # ~999 bytes of ADC body data. The correct term_counter = metadata_counter + # + 0x0400, which returns ~702 bytes of tail+footer — NO extra chunk needed. + # + # 2-frame mode (TCP frame-splitting): probe returns 1 smaller frame (~554 B). + # Each subsequent chunk returns 2 smaller frames (~550 B each). The probe + # contributes only ~487 bytes. The missing ~512 bytes of body data must come + # from an extra chunk after metadata, terminating at metadata_counter+0x0800. + # The caller's extra_chunks_after_metadata value (default 1) covers this. + # + # Threshold 700 B sits comfortably between the two observed probe sizes (554 vs + # 1097 bytes) and is robust to minor variation. + _probe_is_large = ( + len(probe_batch) == 1 and len(probe_batch[0].data) >= 700 + ) + _effective_extra_chunks = 0 if _probe_is_large else extra_chunks_after_metadata + log.warning( + "5A probe data_len=%d _probe_is_large=%s effective_extra_chunks=%d", + len(probe_batch[0].data), + _probe_is_large, + _effective_extra_chunks, + ) + # ── Step 2: chunk loop ─────────────────────────────────────────────── # Counter formula: _chunk_base + (chunk_num - 1) * 0x0400 # where _chunk_base = max(key4[2:4], 0x0400). @@ -686,8 +714,8 @@ class MiniMateProtocol: # (confirmed 2026-04-23). The extra chunk data also belongs in the # file body (confirmed from TCP capture analysis 2026-04-27). log.debug("5A metadata found — fetching %d more chunk(s)", - extra_chunks_after_metadata) - for _extra_n in range(extra_chunks_after_metadata): + _effective_extra_chunks) + for _extra_n in range(_effective_extra_chunks): chunk_num += 1 counter = _chunk_base + (chunk_num - 1) * _BULK_COUNTER_STEP params = bulk_waveform_params(key4, counter)