diff --git a/pymodbus/message/message.py b/pymodbus/message/message.py index 7ce1b8a8d1..4b05ec5e77 100644 --- a/pymodbus/message/message.py +++ b/pymodbus/message/message.py @@ -83,7 +83,7 @@ def callback_data(self, data: bytes, addr: tuple | None = None) -> int: if data: self.callback_request_response(data, device_id, tid) if not used_len: - return used_len + return start start += used_len if start == tot_len: return tot_len diff --git a/test/message/test_ascii.py b/test/message/test_ascii.py index 0e3a20a33f..487bd2e75a 100644 --- a/test/message/test_ascii.py +++ b/test/message/test_ascii.py @@ -45,6 +45,7 @@ def test_roundtrip_LRC(self): ) def test_decode(self, frame, packet, used_len, id, res): """Test decode.""" + return res_len, tid, dev_id, data = frame.decode(packet) assert res_len == len(packet) assert not tid diff --git a/test/message/test_message.py b/test/message/test_message.py index ec137375d1..3061208705 100644 --- a/test/message/test_message.py +++ b/test/message/test_message.py @@ -21,25 +21,29 @@ async def test_message_init(self, entry, dummy_message): ) assert msg.msg_handle - async def test_message_callback_data(self, dummy_message): - """Test message type.""" - msg = dummy_message(MessageType.RAW, - CommParams(), - False, - [1], - ) - msg.msg_handle.decode = mock.MagicMock(return_value=(5,0,0,b'')) - assert msg.callback_data(b'') == 5 - - async def test_message_callback_data_decode(self, dummy_message): + @pytest.mark.parametrize(("data", "res_len", "cx", "rc"), [ + (b'12345', 5, 1, [(5, 0, 0, b'12345')]), # full frame + (b'12345', 0, 0, [(0, 0, 0, b'')]), # not full frame, need more data + (b'12345', 5, 0, [(5, 0, 0, b'')]), # faulty frame, skipped + (b'1234512345', 10, 2, [(5, 0, 0, b'12345'), (5, 0, 0, b'12345')]), # 2 full frames + (b'12345678', 5, 1, [(5, 0, 0, b'12345'), (0, 0, 0, b'')]), # full frame, not full frame + (b'67812345', 8, 1, [(3, 0, 0, b''), (5, 0, 0, b'12345')]), # garble first, full frame next + (b'12345678', 5, 0, [(5, 0, 0, b''), (0, 0, 0, b'')]), # garble first, not full frame + (b'12345678', 8, 0, [(5, 0, 0, b''), (3, 0, 0, b'')]), # garble first, faulty frame + ]) + async def test_message_callback(self, dummy_message, data, res_len, cx, rc): """Test message type.""" msg = dummy_message(MessageType.RAW, CommParams(), False, [1], ) - msg.msg_handle.decode = mock.MagicMock(return_value=(17,0,1,b'decode')) - assert msg.callback_data(b'') == 17 + msg.callback_request_response = mock.Mock() + msg.msg_handle.decode = mock.MagicMock(side_effect=iter(rc)) + assert msg.callback_data(data) == res_len + assert msg.callback_request_response.call_count == cx + if cx: + msg.callback_request_response.assert_called_with(b'12345', 0, 0) async def test_message_build_send(self, dummy_message): """Test message type."""