diff --git a/.coveragerc b/.coveragerc index 894602930..dbdb75230 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,3 +1,4 @@ [run] omit = - pymodbus/repl/* \ No newline at end of file + pymodbus/repl/* + pymodbus/internal/* \ No newline at end of file diff --git a/pymodbus/framer/ascii_framer.py b/pymodbus/framer/ascii_framer.py index f98bdca7b..a88f6499f 100644 --- a/pymodbus/framer/ascii_framer.py +++ b/pymodbus/framer/ascii_framer.py @@ -1,16 +1,10 @@ import struct -import socket from binascii import b2a_hex, a2b_hex from pymodbus.exceptions import ModbusIOException from pymodbus.utilities import checkLRC, computeLRC from pymodbus.framer import ModbusFramer, FRAME_HEADER, BYTE_ORDER -# Python 2 compatibility. -try: - TimeoutError -except NameError: - TimeoutError = socket.timeout ASCII_FRAME_HEADER = BYTE_ORDER + FRAME_HEADER diff --git a/test/test_framers.py b/test/test_framers.py index 4423bb9c3..c11c72f37 100644 --- a/test/test_framers.py +++ b/test/test_framers.py @@ -12,18 +12,22 @@ else: # Python 2 from mock import Mock + @pytest.fixture def rtu_framer(): return ModbusRtuFramer(ClientDecoder()) + @pytest.fixture def ascii_framer(): return ModbusAsciiFramer(ClientDecoder()) + @pytest.fixture def binary_framer(): return ModbusBinaryFramer(ClientDecoder()) + @pytest.mark.parametrize("framer", [ModbusRtuFramer, ModbusAsciiFramer, ModbusBinaryFramer, @@ -116,9 +120,12 @@ def test_populate_result(rtu_framer): assert result.unit_id == 255 -def test_process_incoming_packet(rtu_framer): +@pytest.mark.parametrize('framer', [ascii_framer, rtu_framer, binary_framer]) +def test_process_incoming_packet(framer): def cb(res): return res + # data = b'' + # framer.processIncomingPacket(data, cb, unit=1, single=False) def test_build_packet(rtu_framer): @@ -160,4 +167,22 @@ def cb(res): def test_get_raw_frame(rtu_framer): rtu_framer._buffer = b'\x00\x01\x00\x01\x00\n\xec\x1c' - assert rtu_framer.getRawFrame() == rtu_framer._buffer \ No newline at end of file + assert rtu_framer.getRawFrame() == rtu_framer._buffer + + +def test_validate_unit_id(rtu_framer): + rtu_framer.populateHeader( b'\x00\x01\x00\x01\x00\n\xec\x1c') + assert rtu_framer._validate_unit_id([0], False) + assert rtu_framer._validate_unit_id([1], True) + + +@pytest.mark.parametrize('data', [b':010100010001FC\r\n', + b'']) +def test_decode_ascii_data(ascii_framer, data): + data = ascii_framer.decode_data(data) + assert isinstance(data, dict) + if data: + assert data.get("unit") == 1 + assert data.get("fcode") == 1 + else: + assert not data \ No newline at end of file