Skip to content

Commit f7fe305

Browse files
sherpyajaniversen
andauthored
better fix for rtu incomplete frames (#511)
* better fix for rtu incomplete frames - avoid multiple calls to populateHeader() - trap IndexError in calculateRtuFrameSize() - more readable Co-authored-by: jan Iversen <jancasacondor@gmail.com>
1 parent a957539 commit f7fe305

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

examples/contrib/message_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def report(self, message): # pylint: disable=no-self-use
105105
" %-12s => %s" % (k_item, v_item) # pylint: disable=consider-using-f-string
106106
)
107107

108-
elif isinstance(v_dict, collections.Iterable): # pylint: disable=no-member
108+
elif isinstance(v_dict, collections.Iterable):
109109
print("%-15s =" % k_dict) # pylint: disable=consider-using-f-string
110110
value = str([int(x) for x in v_dict])
111111
for line in textwrap.wrap(value, 60):

pymodbus/framer/rtu_framer.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,17 @@ def isFrameReady(self):
131131
132132
:returns: True if ready, False otherwise
133133
"""
134-
if len(self._buffer) <= self._hsize:
135-
return False
136-
137-
try:
138-
# Frame is ready only if populateHeader() successfully
139-
# populates crc field which finishes RTU frame otherwise,
140-
# if buffer is not yet long enough, populateHeader() raises IndexError
141-
self.populateHeader()
142-
except IndexError:
143-
return False
144-
145-
return True
134+
size = self._header.get('len', 0)
135+
if not size and len(self._buffer) > self._hsize:
136+
try:
137+
# Frame is ready only if populateHeader() successfully
138+
# populates crc field which finishes RTU frame otherwise,
139+
# if buffer is not yet long enough, populateHeader() raises IndexError
140+
size = self.populateHeader()
141+
except IndexError:
142+
return False
143+
144+
return len(self._buffer) >= size if size > 0 else False
146145

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

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

0 commit comments

Comments
 (0)