A robot event log can open successfully and still show the wrong number of events. This can happen when the logger writes packed binary records and a replay tool decodes them using the layout of a native C structure. Both programs may use the same field names and unsigned types. The reader can still step through the file at a different stride because its layout inserts padding between the fields.
Consider an offline fixture for commissioning a replay tool. Its payload has one unsigned event code byte followed immediately by a four-byte unsigned tick counter in little-endian order. Every record occupies five bytes. Tick is an integer counter in this example, with no implied unit, clock domain, or control meaning. There is no header or checksum in the fixture.
Make the layout visible in both readers
Python's struct module uses the platform's native representation and alignment when the format starts with @ or omits a prefix. On the machine used for this example, @BI occupies eight bytes because the four-byte integer starts at offset four. The packed format <BI puts the integer at offset one and occupies five bytes. A NumPy structured dtype can describe the same five bytes by keeping alignment off and explicitly making the counter little-endian. Its item size is the stride between consecutive records in the buffer.
Here the sample counter byte values were chosen so the order is visible in a hex view. The first record begins with event code 07. Its four counter bytes are 04 03 02 01, so the decoded counter is hexadecimal 01020304. The second record starts immediately after those bytes, at offset five.
import struct
import numpy as np
payload = bytes.fromhex("07 04 03 02 01 09 0D 0C 0B 0A")
expected = [(7, 0x01020304), (9, 0x0A0B0C0D)]
packed = struct.Struct("<BI")
dtype = np.dtype([("code", "u1"), ("tick", "<u4")], align=False)
assert packed.size == dtype.itemsize == 5
assert dtype.fields["tick"][1] == 1
assert list(packed.iter_unpack(payload)) == expected
assert np.frombuffer(payload, dtype=dtype).tolist() == expected
larger_fixture = payload * 4
assert len(larger_fixture) == 40
assert len(list(packed.iter_unpack(larger_fixture))) == 8
native = struct.Struct("@BI")
if native.size == 8:
decoded_native = list(native.iter_unpack(larger_fixture))
assert len(decoded_native) == 5
assert decoded_native[0][1] != expected[0][1]
In the 40-byte example, both five and eight divide the payload length. The native reader can therefore accept the full buffer and yield five records, while the packed reader correctly yields eight. A check for leftover bytes helps catch truncation, but a length check by itself cannot establish the record layout. Likewise, an unsigned value obtained from the wrong offset can still fall in a plausible range.
Publish the record contract with the log
Write down each field's offset, width, signedness, and byte order. Include the overall record stride and specify any reserved bytes. Add the timestamp or tick unit, clock origin, and wrap policy as separate pieces of metadata. A consumer cannot infer whether a counter represents microseconds or controller cycles from four bytes on disk.
A format with a file header should identify the version and where the payload begins. Derive offsets and expected payload length from that version rather than adjusting them until an old capture happens to decode. If the format stores a record count, compare that count with the decoded count and actual payload length. Handle incomplete records and unsupported versions before exporting derived timing or event totals.
Keep fixed bytes in regression tests
A writer and reader configured with the same incorrect format can pass a round-trip test. Include a byte fixture from the documented format as well. This example starts from literal hex bytes and checks both the 32-bit values and the position of the next record. A real test set can also include a maximum counter, a zero event code, a truncated record, and consecutive records whose first bytes are intentionally different.
Read the same fixture with the interactive replay tool and the batch analysis job. Compare counts, the first and last complete records, and the result of parsing a file that ends mid-record. When a log is mapped into NumPy from a mutable buffer, copy the data before retaining it if later writes could change the report. The evidence to preserve during review is the format version and exact bytes that produced each decoded result.
Sources
Python library reference, struct byte order, size, and alignment
https://docs.python.org/3/library/struct.html
NumPy API reference, frombuffer
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
NumPy user guide, structured arrays and field alignment
https://numpy.org/doc/stable/user/basics.rec.html#automatic-byte-offsets-and-alignment