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 committed Jul 9, 2020
1 parent 5048a1a commit 747867d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 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._buffer[frame_size - 2:frame_size]
Expand Down Expand Up @@ -137,13 +136,15 @@ def isFrameReady(self):
:returns: True if ready, False otherwise
"""
if len(self._buffer) > self._hsize:
if not self._header:
self.populateHeader()

return self._header and len(self._buffer) >= self._header['len']
else:
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

def populateHeader(self, data=None):
"""
Expand All @@ -163,6 +164,7 @@ def populateHeader(self, data=None):
size = pdu_class.calculateRtuFrameSize(data)
self._header['len'] = size
self._header['crc'] = data[size - 2:size]
return size

def addToFrame(self, message):
"""
Expand Down

0 comments on commit 747867d

Please sign in to comment.