|
| 1 | +"""Test transport.""" |
| 2 | +import struct |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pymodbus.message.ascii import MessageAscii |
| 7 | + |
| 8 | + |
| 9 | +class TestMessageAscii: |
| 10 | + """Test message module.""" |
| 11 | + |
| 12 | + @staticmethod |
| 13 | + @pytest.fixture(name="frame") |
| 14 | + def prepare_frame(): |
| 15 | + """Return message object.""" |
| 16 | + return MessageAscii([1], False) |
| 17 | + |
| 18 | + |
| 19 | + def test_check_LRC(self): |
| 20 | + """Test check_LRC.""" |
| 21 | + data = struct.pack(">HHHH", 0x1234, 0x2345, 0x3456, 0x4567) |
| 22 | + assert MessageAscii.check_LRC(data, 0x1C) |
| 23 | + |
| 24 | + def test_check_noLRC(self): |
| 25 | + """Test check_LRC.""" |
| 26 | + data = struct.pack(">HHHH", 0x1234, 0x2345, 0x3456, 0x4567) |
| 27 | + assert not MessageAscii.check_LRC(data, 0x0C) |
| 28 | + |
| 29 | + def test_compute_LRC(self): |
| 30 | + """Test compute_LRC.""" |
| 31 | + data = struct.pack(">HHHH", 0x1234, 0x2345, 0x3456, 0x4567) |
| 32 | + assert MessageAscii.compute_LRC(data) == 0x1c |
| 33 | + |
| 34 | + def test_roundtrip_LRC(self): |
| 35 | + """Test combined compute/check LRC.""" |
| 36 | + data = struct.pack(">HHHH", 0x1234, 0x2345, 0x3456, 0x4567) |
| 37 | + assert MessageAscii.compute_LRC(data) == 0x1c |
| 38 | + assert MessageAscii.check_LRC(data, 0x1C) |
| 39 | + |
| 40 | + @pytest.mark.parametrize( |
| 41 | + ("packet", "used_len", "res_id", "res"), |
| 42 | + [ |
| 43 | + (b':010100010001FC\r\n', 17, 1, b'\x01\x00\x01\x00\x01'), |
| 44 | + (b':00010001000AF4\r\n', 17, 0, b'\x01\x00\x01\x00\x0a'), |
| 45 | + (b':01010001000AF3\r\n', 17, 1, b'\x01\x00\x01\x00\x0a'), |
| 46 | + (b':61620001000A32\r\n', 17, 97, b'\x62\x00\x01\x00\x0a'), |
| 47 | + (b':01270001000ACD\r\n', 17, 1, b'\x27\x00\x01\x00\x0a'), |
| 48 | + (b':010100', 0, 0, b''), # short frame |
| 49 | + (b':00010001000AF4', 0, 0, b''), |
| 50 | + (b'abc:00010001000AF4', 3, 0, b''), # garble before frame |
| 51 | + (b'abc00010001000AF4', 17, 0, b''), # only garble |
| 52 | + (b':01010001000A00\r\n', 17, 0, b''), |
| 53 | + ], |
| 54 | + ) |
| 55 | + def test_decode(self, frame, packet, used_len, res_id, res): |
| 56 | + """Test decode.""" |
| 57 | + res_len, tid, dev_id, data = frame.decode(packet) |
| 58 | + assert res_len == used_len |
| 59 | + assert data == res |
| 60 | + assert not tid |
| 61 | + assert dev_id == res_id |
| 62 | + |
| 63 | + @pytest.mark.parametrize( |
| 64 | + ("data", "dev_id", "res_msg"), |
| 65 | + [ |
| 66 | + (b'\x01\x05\x04\x00\x17', 1, b':010105040017DE\r\n'), |
| 67 | + (b'\x03\x07\x06\x00\x73', 2, b':0203070600737B\r\n'), |
| 68 | + (b'\x08\x00\x01', 3, b':03080001F4\r\n'), |
| 69 | + ], |
| 70 | + ) |
| 71 | + def test_encode(self, frame, data, dev_id, res_msg): |
| 72 | + """Test encode.""" |
| 73 | + msg = frame.encode(data, dev_id, 0) |
| 74 | + assert res_msg == msg |
| 75 | + assert dev_id == int(msg[1:3], 16) |
| 76 | + |
| 77 | + @pytest.mark.parametrize( |
| 78 | + ("data", "dev_id", "res_msg"), |
| 79 | + [ |
| 80 | + (b'\x01\x05\x04\x00\x17', 1, b':010105040017DF\r\n'), |
| 81 | + (b'\x03\x07\x06\x00\x73', 2, b':0203070600737D\r\n'), |
| 82 | + (b'\x08\x00\x01', 3, b':03080001F7\r\n'), |
| 83 | + ], |
| 84 | + ) |
| 85 | + def test_roundtrip(self, frame, data, dev_id, res_msg): |
| 86 | + """Test encode.""" |
| 87 | + msg = frame.encode(data, dev_id, 0) |
| 88 | + res_len, _, res_id, res_data = frame.decode(msg) |
| 89 | + assert data == res_data |
| 90 | + assert dev_id == res_id |
| 91 | + assert res_len == len(res_msg) |
0 commit comments