Skip to content

Commit 17ea99e

Browse files
christiansandbergjaniversen
authored andcommitted
Catch IndexError
1 parent c653813 commit 17ea99e

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

pymodbus/framer/rtu_framer.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import struct
44
import time
55

6-
from pymodbus.exceptions import ModbusIOException, NotImplementedException
6+
from pymodbus.exceptions import ModbusIOException
77
from pymodbus.exceptions import InvalidMessageReceivedException
88
from pymodbus.utilities import checkCRC, computeCRC
99
from pymodbus.utilities import hexlify_packets, ModbusTransactionState
@@ -328,14 +328,16 @@ def getRawFrame(self): # pylint: disable=invalid-name
328328
_logger.debug(txt)
329329
return self._buffer
330330

331-
def get_expected_response_length(self, data=None):
332-
"""Get the expected response length if available in data, else None."""
333-
data = data if data is not None else self._buffer
331+
def get_expected_response_length(self, data):
332+
"""Get the expected response length if available in data.
333+
334+
:param data: Message data read so far
335+
:raises IndexError: If not enough data to read byte count
336+
:raises NotImplementedException: If not supported by function code
337+
:return: Total frame size
338+
"""
334339
func_code = int(data[1])
335340
pdu_class = self.decoder.lookupPduClass(func_code)
336-
try:
337-
return pdu_class.calculateRtuFrameSize(data)
338-
except NotImplementedException:
339-
return None
341+
return pdu_class.calculateRtuFrameSize(data)
340342

341343
# __END__

pymodbus/transaction.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def _recv(self, expected_response_length, full): # NOSONAR
343343
if isinstance(self.client.framer, ModbusSocketFramer):
344344
min_size = 8
345345
elif isinstance(self.client.framer, ModbusRtuFramer):
346-
min_size = 3
346+
min_size = 4
347347
elif isinstance(self.client.framer, ModbusAsciiFramer):
348348
min_size = 5
349349
elif isinstance(self.client.framer, ModbusBinaryFramer):
@@ -379,7 +379,11 @@ def _recv(self, expected_response_length, full): # NOSONAR
379379
length = struct.unpack(">H", read_min[4:6])[0] - 1
380380
expected_response_length = h_size + length
381381
elif expected_response_length is None and isinstance(self.client.framer, ModbusRtuFramer):
382-
expected_response_length = self.client.framer.get_expected_response_length(read_min)
382+
try:
383+
expected_response_length = self.client.framer.get_expected_response_length(read_min)
384+
except IndexError:
385+
# Could not determine response length with available bytes
386+
pass
383387
if expected_response_length is not None:
384388
expected_response_length -= min_size
385389
total = expected_response_length + min_size

0 commit comments

Comments
 (0)