Skip to content

Occasionally run into struct error because getting 40 bytes instead of 45 bytes #12

Open
@stephenhibbert

Description

@stephenhibbert

99% of the time this is working, but maybe once a day I see the following error. Simple workaround is to catch and handle the error but wanted to pass it on in case others have see this.

ERROR
struct.error: unpack_from requires a buffer of at least 45 bytes for unpacking 8 bytes at offset 37 (actual buffer size is 40)

SOURCE CODE

while True:
        try:
            read_bytes = i2c_handle.read(PERSON_SENSOR_RESULT_BYTE_COUNT)
        except OSError as error:
            print(error)
            continue
        offset = 0
        (pad1, pad2, payload_bytes) = struct.unpack_from(
            PERSON_SENSOR_I2C_HEADER_FORMAT, read_bytes, offset)
        offset = offset + PERSON_SENSOR_I2C_HEADER_BYTE_COUNT

        (num_faces) = struct.unpack_from("B", read_bytes, offset)
        num_faces = int(num_faces[0])
        offset = offset + 1

        faces = []
        for i in range(num_faces):
            (box_confidence, box_left, box_top, box_right, box_bottom, id_confidence, id,
             is_facing) = struct.unpack_from(PERSON_SENSOR_FACE_FORMAT, read_bytes, offset)
            offset = offset + PERSON_SENSOR_FACE_BYTE_COUNT
            face = {
                "box_confidence": box_confidence,
                "box_left": box_left,
                "box_top": box_top,
                "box_right": box_right,
                "box_bottom": box_bottom,
                "id_confidence": id_confidence,
                "id": id,
                "is_facing": is_facing,
            }
            faces.append(face)
        checksum = struct.unpack_from("H", read_bytes, offset)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions