Skip to content

Commit

Permalink
remove addToFrame.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Jul 2, 2023
1 parent 95d415f commit 4eb73c8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 53 deletions.
12 changes: 1 addition & 11 deletions pymodbus/framer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ def resetFrame(self):
"tid": 0,
}

def addToFrame(self, message):
"""Add the next message to the frame buffer.
This should be used before the decoding while loop to add the received
data to the buffer handle.
:param message: The most recent packet
"""
self._buffer += message

def populateResult(self, result):
"""Populate the modbus result header.
Expand Down Expand Up @@ -140,10 +130,10 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
:raises ModbusIOException:
"""
Log.debug("Processing: {}", data, ":hex")
self._buffer += data
if not isinstance(slave, (list, tuple)):
slave = [slave]
single = kwargs.pop("single", False)
self.addToFrame(data)
self.frameProcessIncomingPacket(single, callback, slave, **kwargs)

def frameProcessIncomingPacket(
Expand Down
9 changes: 1 addition & 8 deletions test/test_framers.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,9 @@ def test_rtu_populate_header(rtu_framer, data):
assert rtu_framer._header == expected # pylint: disable=protected-access


def test_add_to_frame(rtu_framer):
"""Test add to frame."""
assert rtu_framer._buffer == b"" # pylint: disable=protected-access
rtu_framer.addToFrame(b"abcd")
assert rtu_framer._buffer == b"abcd" # pylint: disable=protected-access


def test_get_frame(rtu_framer):
"""Test get frame."""
rtu_framer.addToFrame(b"\x02\x01\x01\x00Q\xcc")
rtu_framer._buffer = b"\x02\x01\x01\x00Q\xcc" # pylint: disable=protected-access
rtu_framer.populateHeader(b"\x02\x01\x01\x00Q\xcc")
assert rtu_framer.getFrame() == b"\x01\x01\x00"

Expand Down
69 changes: 35 additions & 34 deletions test/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def test_tcp_framer_transaction_ready(self):
msg = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34"
assert not self._tcp.isFrameReady()
assert not self._tcp.checkFrame()
self._tcp.addToFrame(msg)
self._tcp._buffer = msg # pylint: disable=protected-access
assert self._tcp.isFrameReady()
assert self._tcp.checkFrame()
self._tcp.advanceFrame()
Expand All @@ -298,7 +298,7 @@ def test_tcp_framer_transaction_ready(self):
def test_tcp_framer_transaction_full(self):
"""Test a full tcp frame transaction"""
msg = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34"
self._tcp.addToFrame(msg)
self._tcp._buffer = msg # pylint: disable=protected-access
assert self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == msg[7:]
Expand All @@ -308,11 +308,11 @@ def test_tcp_framer_transaction_half(self):
"""Test a half completed tcp frame transaction"""
msg1 = b"\x00\x01\x12\x34\x00"
msg2 = b"\x04\xff\x02\x12\x34"
self._tcp.addToFrame(msg1)
self._tcp._buffer = msg1 # pylint: disable=protected-access
assert not self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == b""
self._tcp.addToFrame(msg2)
self._tcp._buffer += msg2
assert self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == msg2[2:]
Expand All @@ -322,11 +322,11 @@ def test_tcp_framer_transaction_half2(self):
"""Test a half completed tcp frame transaction"""
msg1 = b"\x00\x01\x12\x34\x00\x04\xff"
msg2 = b"\x02\x12\x34"
self._tcp.addToFrame(msg1)
self._tcp._buffer = msg1 # pylint: disable=protected-access
assert not self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == b""
self._tcp.addToFrame(msg2)
self._tcp._buffer += msg2
assert self._tcp.checkFrame()
result = self._tcp.getFrame()
assert msg2 == result
Expand All @@ -336,11 +336,11 @@ def test_tcp_framer_transaction_half3(self):
"""Test a half completed tcp frame transaction"""
msg1 = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12"
msg2 = b"\x34"
self._tcp.addToFrame(msg1)
self._tcp._buffer = msg1 # pylint: disable=protected-access
assert not self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == msg1[7:]
self._tcp.addToFrame(msg2)
self._tcp._buffer += msg2
assert self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == msg1[7:] + msg2
Expand All @@ -350,12 +350,12 @@ def test_tcp_framer_transaction_short(self):
"""Test that we can get back on track after an invalid message"""
msg1 = b"\x99\x99\x99\x99\x00\x01\x00\x01"
msg2 = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34"
self._tcp.addToFrame(msg1)
self._tcp._buffer = msg1 # pylint: disable=protected-access
assert not self._tcp.checkFrame()
result = self._tcp.getFrame()
assert result == b""
self._tcp.advanceFrame()
self._tcp.addToFrame(msg2)
self._tcp._buffer += msg2
assert len(self._tcp._buffer) == 10 # pylint: disable=protected-access
assert self._tcp.checkFrame()
result = self._tcp.getFrame()
Expand All @@ -369,7 +369,7 @@ def test_tcp_framer_populate(self):
expected.protocol_id = 0x1234
expected.slave_id = 0xFF
msg = b"\x00\x01\x12\x34\x00\x04\xff\x02\x12\x34"
self._tcp.addToFrame(msg)
self._tcp._buffer = msg # pylint: disable=protected-access
assert self._tcp.checkFrame()
actual = ModbusRequest()
self._tcp.populateResult(actual)
Expand Down Expand Up @@ -399,7 +399,7 @@ def test_framer_tls_framer_transaction_ready(self):
msg = b"\x01\x12\x34\x00\x08"
assert not self._tls.isFrameReady()
assert not self._tls.checkFrame()
self._tls.addToFrame(msg)
self._tls._buffer = msg # pylint: disable=protected-access
assert self._tls.isFrameReady()
assert self._tls.checkFrame()
self._tls.advanceFrame()
Expand All @@ -410,7 +410,7 @@ def test_framer_tls_framer_transaction_ready(self):
def test_framer_tls_framer_transaction_full(self):
"""Test a full tls frame transaction"""
msg = b"\x01\x12\x34\x00\x08"
self._tls.addToFrame(msg)
self._tls._buffer = msg # pylint: disable=protected-access
assert self._tls.checkFrame()
result = self._tls.getFrame()
assert result == msg[0:]
Expand All @@ -420,11 +420,11 @@ def test_framer_tls_framer_transaction_half(self):
"""Test a half completed tls frame transaction"""
msg1 = b""
msg2 = b"\x01\x12\x34\x00\x08"
self._tls.addToFrame(msg1)
self._tls._buffer = msg1 # pylint: disable=protected-access
assert not self._tls.checkFrame()
result = self._tls.getFrame()
assert result == b""
self._tls.addToFrame(msg2)
self._tls._buffer += msg2
assert self._tls.checkFrame()
result = self._tls.getFrame()
assert result == msg2[0:]
Expand All @@ -434,12 +434,12 @@ def test_framer_tls_framer_transaction_short(self):
"""Test that we can get back on track after an invalid message"""
msg1 = b""
msg2 = b"\x01\x12\x34\x00\x08"
self._tls.addToFrame(msg1)
self._tls._buffer = msg1 # pylint: disable=protected-access
assert not self._tls.checkFrame()
result = self._tls.getFrame()
assert result == b""
self._tls.advanceFrame()
self._tls.addToFrame(msg2)
self._tls._buffer = msg2 # pylint: disable=protected-access
assert len(self._tls._buffer) == 5 # pylint: disable=protected-access
assert self._tls.checkFrame()
result = self._tls.getFrame()
Expand Down Expand Up @@ -513,7 +513,7 @@ def test_framer_tls_framer_populate(self):
"""Test a tls frame packet build"""
ModbusRequest()
msg = b"\x01\x12\x34\x00\x08"
self._tls.addToFrame(msg)
self._tls._buffer = msg # pylint: disable=protected-access
assert self._tls.checkFrame()
actual = ModbusRequest()
self._tls.populateResult(actual)
Expand All @@ -538,19 +538,19 @@ def test_rtu_framer_transaction_ready(self):
assert not self._rtu.isFrameReady()

msg_parts = [b"\x00\x01\x00", b"\x00\x00\x01\xfc\x1b"]
self._rtu.addToFrame(msg_parts[0])
self._rtu._buffer = msg_parts[0] # pylint: disable=protected-access
assert not self._rtu.isFrameReady()
assert not self._rtu.checkFrame()

self._rtu.addToFrame(msg_parts[1])
self._rtu._buffer += msg_parts[1]
assert self._rtu.isFrameReady()
assert self._rtu.checkFrame()

def test_rtu_framer_transaction_full(self):
"""Test a full rtu frame transaction"""
msg = b"\x00\x01\x00\x00\x00\x01\xfc\x1b"
stripped_msg = msg[1:-2]
self._rtu.addToFrame(msg)
self._rtu._buffer = msg # pylint: disable=protected-access
assert self._rtu.checkFrame()
result = self._rtu.getFrame()
assert stripped_msg == result
Expand All @@ -560,9 +560,9 @@ def test_rtu_framer_transaction_half(self):
"""Test a half completed rtu frame transaction"""
msg_parts = [b"\x00\x01\x00", b"\x00\x00\x01\xfc\x1b"]
stripped_msg = b"".join(msg_parts)[1:-2]
self._rtu.addToFrame(msg_parts[0])
self._rtu._buffer = msg_parts[0] # pylint: disable=protected-access
assert not self._rtu.checkFrame()
self._rtu.addToFrame(msg_parts[1])
self._rtu._buffer += msg_parts[1]
assert self._rtu.isFrameReady()
assert self._rtu.checkFrame()
result = self._rtu.getFrame()
Expand All @@ -573,7 +573,7 @@ def test_rtu_framer_populate(self):
"""Test a rtu frame packet build"""
request = ModbusRequest()
msg = b"\x00\x01\x00\x00\x00\x01\xfc\x1b"
self._rtu.addToFrame(msg)
self._rtu._buffer = msg # pylint: disable=protected-access
self._rtu.populateHeader()
self._rtu.populateResult(request)

Expand All @@ -598,7 +598,7 @@ def test_rtu_framer_packet(self):
def test_rtu_decode_exception(self):
"""Test that the RTU framer can decode errors"""
message = b"\x00\x90\x02\x9c\x01"
self._rtu.addToFrame(message)
self._rtu._buffer = message # pylint: disable=protected-access
result = self._rtu.checkFrame()
assert result

Expand Down Expand Up @@ -639,7 +639,7 @@ def test_rtu_process_incoming_packets(self):
def mock_callback():
"""Mock callback."""

self._rtu.addToFrame = mock.MagicMock()
self._rtu._buffer = mock.MagicMock() # pylint: disable=protected-access
self._rtu._process = mock.MagicMock() # pylint: disable=protected-access
self._rtu.isFrameReady = mock.MagicMock(return_value=False)
self._rtu._buffer = mock_data # pylint: disable=protected-access
Expand All @@ -654,7 +654,7 @@ def test_ascii_framer_transaction_ready(self):
msg = b":F7031389000A60\r\n"
assert not self._ascii.isFrameReady()
assert not self._ascii.checkFrame()
self._ascii.addToFrame(msg)
self._ascii._buffer = msg # pylint: disable=protected-access
assert self._ascii.isFrameReady()
assert self._ascii.checkFrame()
self._ascii.advanceFrame()
Expand All @@ -666,7 +666,7 @@ def test_ascii_framer_transaction_full(self):
"""Test a full ascii frame transaction"""
msg = b"sss:F7031389000A60\r\n"
pack = a2b_hex(msg[6:-4])
self._ascii.addToFrame(msg)
self._ascii._buffer = msg # pylint: disable=protected-access
assert self._ascii.checkFrame()
result = self._ascii.getFrame()
assert pack == result
Expand All @@ -677,11 +677,11 @@ def test_ascii_framer_transaction_half(self):
msg1 = b"sss:F7031389"
msg2 = b"000A60\r\n"
pack = a2b_hex(msg1[6:] + msg2[:-4])
self._ascii.addToFrame(msg1)
self._ascii._buffer = msg1 # pylint: disable=protected-access
assert not self._ascii.checkFrame()
result = self._ascii.getFrame()
assert not result
self._ascii.addToFrame(msg2)
self._ascii._buffer += msg2
assert self._ascii.checkFrame()
result = self._ascii.getFrame()
assert pack == result
Expand Down Expand Up @@ -727,7 +727,7 @@ def test_binary_framer_transaction_ready(self):
msg = TEST_MESSAGE
assert not self._binary.isFrameReady()
assert not self._binary.checkFrame()
self._binary.addToFrame(msg)
self._binary._buffer = msg # pylint: disable=protected-access
assert self._binary.isFrameReady()
assert self._binary.checkFrame()
self._binary.advanceFrame()
Expand All @@ -739,7 +739,7 @@ def test_binary_framer_transaction_full(self):
"""Test a full binary frame transaction"""
msg = TEST_MESSAGE
pack = msg[2:-3]
self._binary.addToFrame(msg)
self._binary._buffer = msg # pylint: disable=protected-access
assert self._binary.checkFrame()
result = self._binary.getFrame()
assert pack == result
Expand All @@ -750,11 +750,12 @@ def test_binary_framer_transaction_half(self):
msg1 = b"\x7b\x01\x03\x00"
msg2 = b"\x00\x00\x05\x85\xC9\x7d"
pack = msg1[2:] + msg2[:-3]
self._binary.addToFrame(msg1)
self._binary._buffer = msg1 # pylint: disable=protected-access
assert not self._binary.checkFrame()
result = self._binary.getFrame()
assert not result
self._binary.addToFrame(msg2)
self._binary._buffer += msg2

assert self._binary.checkFrame()
result = self._binary.getFrame()
assert pack == result
Expand Down

0 comments on commit 4eb73c8

Please sign in to comment.