Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ def populateHeader(self, data=None): # pylint: disable=invalid-name
`self._buffer` is not yet long enough.
"""
data = data if data is not None else self._buffer
self._header["uid"] = int(data[0])
func_code = int(data[1])
pdu_class = self.decoder.lookupPduClass(func_code)
size = pdu_class.calculateRtuFrameSize(data)
self._header["len"] = size
self._header['uid'] = int(data[0])
size = self.get_expected_response_length(data)
self._header['len'] = size

if len(data) < size:
# crc yet not available
Expand Down Expand Up @@ -330,5 +328,15 @@ def getRawFrame(self): # pylint: disable=invalid-name
_logger.debug(txt)
return self._buffer

def get_expected_response_length(self, data):
"""Get the expected response length.

:param data: Message data read so far
:raises IndexError: If not enough data to read byte count
:return: Total frame size
"""
func_code = int(data[1])
pdu_class = self.decoder.lookupPduClass(func_code)
return pdu_class.calculateRtuFrameSize(data)

# __END__
10 changes: 8 additions & 2 deletions pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _recv(self, expected_response_length, full): # NOSONAR
if isinstance(self.client.framer, ModbusSocketFramer):
min_size = 8
elif isinstance(self.client.framer, ModbusRtuFramer):
min_size = 2
min_size = 4
elif isinstance(self.client.framer, ModbusAsciiFramer):
min_size = 5
elif isinstance(self.client.framer, ModbusBinaryFramer):
Expand All @@ -362,7 +362,7 @@ def _recv(self, expected_response_length, full): # NOSONAR
if isinstance(self.client.framer, ModbusSocketFramer):
func_code = int(read_min[-1])
elif isinstance(self.client.framer, ModbusRtuFramer):
func_code = int(read_min[-1])
func_code = int(read_min[1])
elif isinstance(self.client.framer, ModbusAsciiFramer):
func_code = int(read_min[3:5], 16)
elif isinstance(self.client.framer, ModbusBinaryFramer):
Expand All @@ -378,6 +378,12 @@ def _recv(self, expected_response_length, full): # NOSONAR
)
length = struct.unpack(">H", read_min[4:6])[0] - 1
expected_response_length = h_size + length
elif expected_response_length is None and isinstance(self.client.framer, ModbusRtuFramer):
try:
expected_response_length = self.client.framer.get_expected_response_length(read_min)
except IndexError:
# Could not determine response length with available bytes
pass
if expected_response_length is not None:
expected_response_length -= min_size
total = expected_response_length + min_size
Expand Down