From 8de66a2cf046537cabe390f4dd0d57c3b127005c Mon Sep 17 00:00:00 2001 From: Logan Gunthorpe Date: Wed, 16 Aug 2023 12:05:16 -0600 Subject: [PATCH] Handle partial message in ReadDeviceInformationResponse (#1738) --- pymodbus/mei_message.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pymodbus/mei_message.py b/pymodbus/mei_message.py index 5a1a6b1db..59bbf319a 100644 --- a/pymodbus/mei_message.py +++ b/pymodbus/mei_message.py @@ -125,11 +125,14 @@ def calculateRtuFrameSize(cls, buffer): size = 8 # skip the header information count = int(buffer[7]) - while count > 0: - _, object_length = struct.unpack(">BB", buffer[size : size + 2]) - size += object_length + 2 - count -= 1 - return size + 2 + try: + while count > 0: + _, object_length = struct.unpack(">BB", buffer[size : size + 2]) + size += object_length + 2 + count -= 1 + return size + 2 + except struct.error as exc: + raise IndexError from exc def __init__(self, read_code=None, information=None, **kwargs): """Initialize a new instance.