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 a993bdb
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def checkFrame(self):
2. Discard frame if UID does not match
"""
try:
self.populateHeader()
frame_size = self._header["len"]
data = self._buffer[: frame_size - 2]
crc = self._header["crc"]
Expand Down Expand Up @@ -131,16 +130,15 @@ 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
size = self._header.get('len', 0)
if size == 0 and len(self._buffer) > self._hsize:
try:
size = self.populateHeader()
except IndexError:
return False

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

return True

Expand All @@ -164,6 +162,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 a993bdb

Please sign in to comment.