Skip to content

Commit

Permalink
solucion.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Apr 13, 2023
1 parent 7342cc7 commit 5de5266
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
11 changes: 7 additions & 4 deletions pymodbus/framer/socket_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def decode_data(self, data):
}
return {}

def processIncomingPacket(self, data, callback, slave, **kwargs):
def processIncomingPacket(self, data, callback, slave, tid=None, **kwargs):
"""Process new packet pattern.
This takes in a new request packet, adds it to the current
Expand Down Expand Up @@ -176,9 +176,9 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
Log.debug("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
continue
self._process(callback)
self._process(callback, tid)

def _process(self, callback, error=False):
def _process(self, callback, tid, error=False):
"""Process incoming packets irrespective error condition."""
data = self.getRawFrame() if error else self.getFrame()
if (result := self.decoder.decode(data)) is None:
Expand All @@ -187,7 +187,10 @@ def _process(self, callback, error=False):
raise InvalidMessageReceivedException(result)
self.populateResult(result)
self.advanceFrame()
callback(result) # defer or push to a thread?
if tid and tid != result.transaction_id:
self.resetFrame()
else:
callback(result) # defer or push to a thread?

def resetFrame(self):
"""Reset the entire message frame.
Expand Down
5 changes: 4 additions & 1 deletion pymodbus/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ def execute(self, request): # pylint: disable=too-complex
tid=request.transaction_id,
)
self.client.framer.processIncomingPacket(
response, addTransaction, request.slave_id
response,
addTransaction,
request.slave_id,
tid=request.transaction_id,
)
if not (response := self.getTransaction(request.transaction_id)):
if len(self.transactions):
Expand Down
26 changes: 0 additions & 26 deletions test/test_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,6 @@ def test_udp_client_recv(self):
def test_udp_client_recv_duplicate(self):
"""Test the udp client receive method"""
test_msg = b"\x00\x01\x00\x00\x00\x05\x01\x04\x02\x00\x03"

# test normal receive
if False:
client = ModbusUdpClient("127.0.0.1")
client.socket = mockSocket(copy_send=False)
client.socket.mock_prepare_receive(test_msg)
reply_ok = client.read_input_registers(0x820, 1, 1)
assert not reply_ok.isError()
reply_none = client.read_input_registers(0x820, 1, 1)
assert reply_none.isError()
client.close()

# test duplicate receive
client = ModbusUdpClient("127.0.0.1")
client.socket = mockSocket(copy_send=False)
client.socket.mock_prepare_receive(test_msg)
Expand All @@ -105,19 +92,6 @@ def test_udp_client_recv_duplicate(self):
assert reply_none.isError()
client.close()

# test duplicate receive with garbage
client = ModbusUdpClient("127.0.0.1")
client.socket = mockSocket(copy_send=False)
client.socket.mock_prepare_receive(test_msg)
client.socket.mock_prepare_receive(test_msg + b"\xf6\x3e")
reply_ok = client.read_input_registers(0x820, 1, 1)
assert not reply_ok.isError()
reply_none = client.read_input_registers(0x820, 1, 1)
assert reply_none.isError()
reply_none = client.read_input_registers(0x820, 1, 1)
assert reply_none.isError()
client.close()

def test_udp_client_repr(self):
"""Test udp client representation."""
client = ModbusUdpClient("127.0.0.1")
Expand Down

0 comments on commit 5de5266

Please sign in to comment.