Skip to content

Commit

Permalink
better fix for rtu incomplete frames
Browse files Browse the repository at this point in the history
- avoid multiple calls to populateHeader()
- trap IndexError in calculateRtuFrameSize()
- more readable
  • Loading branch information
sherpya authored and janiversen committed May 24, 2022
1 parent a957539 commit 2e50928
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,17 @@ def isFrameReady(self):
:returns: True if ready, False otherwise
"""
if len(self._buffer) <= self._hsize:
return False

try:
# Frame is ready only if populateHeader() successfully
# populates crc field which finishes RTU frame otherwise,
# if buffer is not yet long enough, populateHeader() raises IndexError
self.populateHeader()
except IndexError:
return False

return True
size = self._header.get('len', 0)
if size == 0 and len(self._buffer) > self._hsize:
try:
# Frame is ready only if populateHeader() successfully
# populates crc field which finishes RTU frame otherwise,
# if buffer is not yet long enough, populateHeader() raises IndexError
size = self.populateHeader()
except IndexError:
return False

return len(self._buffer) >= size if size > 0 else False

def populateHeader(self, data=None): # pylint: disable=invalid-name
"""Try to set the headers `uid`, `len` and `crc`.
Expand All @@ -164,6 +163,7 @@ def populateHeader(self, data=None): # pylint: disable=invalid-name
# crc yet not available
raise IndexError
self._header["crc"] = data[size - 2 : size]
return size

def addToFrame(self, message):
"""Add the received data to the buffer handle.
Expand Down

0 comments on commit 2e50928

Please sign in to comment.