Skip to content

Commit

Permalink
RTU frame problem, when received split. (#2452)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Nov 11, 2024
1 parent 527e5d0 commit 31dd581
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pymodbus/framer/rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
for used_len in range(data_len):
if data_len - used_len < self.MIN_SIZE:
Log.debug("Short frame: {} wait for more data", data, ":hex")
return used_len, 0, 0, self.EMPTY
return 0, 0, 0, self.EMPTY
dev_id = int(data[used_len])
if not (pdu_class := self.decoder.lookupPduClass(data[used_len:])):
continue
if not (size := pdu_class.calculateRtuFrameSize(data[used_len:])):
size = data_len +1
if data_len < used_len +size:
Log.debug("Frame - not ready")
return used_len, dev_id, 0, self.EMPTY
return 0, dev_id, 0, self.EMPTY
for test_len in range(data_len, used_len + size - 1, -1):
start_crc = test_len -2
crc = data[start_crc : start_crc + 2]
Expand Down
8 changes: 4 additions & 4 deletions test/framer/test_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,19 @@ async def test_decode_type(self, entry, test_framer, data, dev_id, tr_id, expect
(9, b'\x83\x02'),
]),
(FramerType.RTU, b'\x00\x83\x02\x91\x21', [ # bad crc
(1, b''),
(0, b''),
(0, b''),
]),
(FramerType.RTU, b'\x00\x83\x02\xf0\x91\x31', [ # dummy char in stream, bad crc
(1, b''),
(0, b''),
(0, b''),
]),
(FramerType.RTU, b'\x00\x83\x02\x91\x21\x00\x83\x02\x91\x31', [ # bad crc + good CRC
(1, b''),
(0, b''),
(0, b''),
]),
(FramerType.RTU, b'\x00\x83\x02\xf0\x91\x31\x00\x83\x02\x91\x31', [ # dummy char in stream, bad crc + good CRC
(1, b''),
(0, b''),
(0, b''),
]),
]
Expand Down
18 changes: 18 additions & 0 deletions test/framer/test_multidrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,21 @@ def test_getFrameStart(self, framer):
used_len, pdu = framer.processIncomingFrame(framer_2ok)
assert pdu
assert used_len == len(framer_2ok)

def test_rtu_split_frame(self, framer):
"""Test test_rtu_split_frame."""
msg1 = (b'\x01\x08\x00\x00\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb'
b'\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99')
msg2 = (b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99'
b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99'
b'\xaa\xbb\xcc\xdd\xee\xff\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99'
b'\xaa\xbb\xcc\xdd\xee\xff\xe7\x65')
used_len, pdu = framer.processIncomingFrame(msg1+msg2)
assert pdu
assert used_len == len(msg1 + msg2)
used_len, pdu = framer.processIncomingFrame(msg1)
assert not pdu
assert not used_len
used_len, pdu = framer.processIncomingFrame(msg1+msg2)
assert pdu
assert used_len == len(msg1 + msg2)
2 changes: 1 addition & 1 deletion test/sub_client/test_client_faulty_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_1917_frame(self):
framer = FramerRTU(DecodePDU(False))
used_len, pdu = framer.processIncomingFrame(recv)
assert not pdu
assert used_len
assert not used_len

def test_faulty_frame1(self, framer):
"""Test ok frame."""
Expand Down

0 comments on commit 31dd581

Please sign in to comment.