Skip to content

Commit

Permalink
rtu_framer: fix processing of incomplete frames (#466)
Browse files Browse the repository at this point in the history
* rtu_framer: fix processing of incomplete frames

* rtu_framer: fix test case
  • Loading branch information
stv0g authored and dhoomakethu committed Nov 13, 2019
1 parent 632d300 commit 635d8ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ def isFrameReady(self):
:returns: True if ready, False otherwise
"""
return len(self._buffer) > self._hsize
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

def populateHeader(self, data=None):
"""
Expand Down
10 changes: 8 additions & 2 deletions test/test_framers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ def test_reset_framer(rtu_framer, data):
assert rtu_framer._buffer == b''


@pytest.mark.parametrize("data", [(b'', False), (b'abcd', True)])
@pytest.mark.parametrize("data", [
(b'', False),
(b'\x11\x03\x06', False),
(b'\x11\x03\x06\xAE\x41\x56\x52\x43\x40\x49', False),
(b'\x11\x03\x06\xAE\x41\x56\x52\x43\x40\x49\xAD', True),
(b'\x11\x03\x06\xAE\x41\x56\x52\x43\x40\x49\xAD\xAB\xCD', True)
])
def test_is_frame_ready(rtu_framer, data):
data, expected = data
rtu_framer._buffer = data
Expand Down Expand Up @@ -185,4 +191,4 @@ def test_decode_ascii_data(ascii_framer, data):
assert data.get("unit") == 1
assert data.get("fcode") == 1
else:
assert not data
assert not data

0 comments on commit 635d8ab

Please sign in to comment.