Skip to content

Commit

Permalink
frameProcessIncomingPacket.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Oct 6, 2024
1 parent 15541e7 commit 558ac74
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 77 deletions.
40 changes: 20 additions & 20 deletions pymodbus/framer/old_framer_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ def __init__(self, decoder, client=None):
self._end = b"\r\n"
self.message_handler = FramerAscii(decoder, [0])

def frameProcessIncomingPacket(self, single, callback, slave, tid=None):
def frameProcessIncomingPacket(self, single, callback, slave, _tid):
"""Process new packet pattern."""
while len(self._buffer):
used_len, data = self.message_handler.decode(self._buffer)
if not data:
if not used_len:
return
self._buffer = self._buffer[used_len :]
continue
self.dev_id = self.message_handler.incoming_dev_id
if not self._validate_slave_id(slave, single):
Log.error("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id)
self.resetFrame()
return

if (result := self.decoder.decode(data)) is None:
raise ModbusIOException("Unable to decode response")
result.slave_id = self.dev_id
result.transaction_id = self.tid
used_len, data = self.message_handler.decode(self._buffer)
if not data:
if not used_len:
return False
self._buffer = self._buffer[used_len :]
self.dev_id = 0
callback(result) # defer this
return True
self.dev_id = self.message_handler.incoming_dev_id
if not self._validate_slave_id(slave, single):
Log.error("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id)
self.resetFrame()
return False

if (result := self.decoder.decode(data)) is None:
raise ModbusIOException("Unable to decode response")
result.slave_id = self.dev_id
result.transaction_id = self.tid
self._buffer = self._buffer[used_len :]
self.dev_id = 0
callback(result) # defer this
return True
10 changes: 7 additions & 3 deletions pymodbus/framer/old_framer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ def processIncomingPacket(self, data: bytes, callback, slave, single=False, tid=
return
if not isinstance(slave, (list, tuple)):
slave = [slave]
self.frameProcessIncomingPacket(single, callback, slave, tid=tid)
while True:
if self._buffer == b'':
return
if not self.frameProcessIncomingPacket(single, callback, slave, tid):
return

def frameProcessIncomingPacket(
self, _single, _callback, _slave, tid=None
) -> None:
self, _single, _callback, _slave, _tid) -> bool:
"""Process new packet pattern."""
return True

def buildPacket(self, message: ModbusRequest | ModbusResponse) -> bytes:
"""Create a ready to send modbus packet.
Expand Down
30 changes: 14 additions & 16 deletions pymodbus/framer/old_framer_rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,21 @@ def __init__(self, decoder, client=None):
super().__init__(decoder, client)
self.message_handler: FramerRTU = FramerRTU(self.decoder, [0])

def frameProcessIncomingPacket(self, _single, callback, _slave, tid=None):
def frameProcessIncomingPacket(self, _single, callback, _slave, _tid):
"""Process new packet pattern."""
while True:
if self._buffer == b'':
break
used_len, data = self.message_handler.decode(self._buffer)
self.dev_id = self.message_handler.incoming_dev_id
if used_len:
self._buffer = self._buffer[used_len:]
if not data:
break
if (result := self.decoder.decode(data)) is None:
raise ModbusIOException("Unable to decode request")
result.slave_id = self.dev_id
result.transaction_id = 0
Log.debug("Frame advanced, resetting header!!")
callback(result) # defer or push to a thread?
used_len, data = self.message_handler.decode(self._buffer)
self.dev_id = self.message_handler.incoming_dev_id
if used_len:
self._buffer = self._buffer[used_len:]
if not data:
return False
if (result := self.decoder.decode(data)) is None:
raise ModbusIOException("Unable to decode request")
result.slave_id = self.dev_id
result.transaction_id = 0
Log.debug("Frame advanced, resetting header!!")
callback(result) # defer or push to a thread?
return True

def sendPacket(self, message: bytes) -> int:
"""Send packets on the bus with 3.5char delay between frames.
Expand Down
44 changes: 21 additions & 23 deletions pymodbus/framer/old_framer_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, decoder, client=None):
self._hsize = 0x07
self.message_handler = FramerSocket(decoder, [0])

def frameProcessIncomingPacket(self, single, callback, slave, tid=None):
def frameProcessIncomingPacket(self, single, callback, slave, tid):
"""Process new packet pattern.
This takes in a new request packet, adds it to the current
Expand All @@ -53,25 +53,23 @@ def frameProcessIncomingPacket(self, single, callback, slave, tid=None):
The processed and decoded messages are pushed to the callback
function to process and send.
"""
while True:
if self._buffer == b'':
return
used_len, data = self.message_handler.decode(self._buffer)
if not data:
return
self.dev_id = self.message_handler.incoming_dev_id
self.tid = self.message_handler.incoming_tid
if not self._validate_slave_id(slave, single):
Log.debug("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id)
self.resetFrame()
return
if (result := self.decoder.decode(data)) is None:
self.resetFrame()
raise ModbusIOException("Unable to decode request")
result.slave_id = self.dev_id
result.transaction_id = self.tid
self._buffer: bytes = self._buffer[used_len:]
if tid and tid != result.transaction_id:
self.resetFrame()
else:
callback(result) # defer or push to a thread?
used_len, data = self.message_handler.decode(self._buffer)
if not data:
return False
self.dev_id = self.message_handler.incoming_dev_id
self.tid = self.message_handler.incoming_tid
if not self._validate_slave_id(slave, single):
Log.debug("Not a valid slave id - {}, ignoring!!", self.message_handler.incoming_dev_id)
self.resetFrame()
return True
if (result := self.decoder.decode(data)) is None:
self.resetFrame()
raise ModbusIOException("Unable to decode request")
result.slave_id = self.dev_id
result.transaction_id = self.tid
self._buffer: bytes = self._buffer[used_len:]
if tid and tid != result.transaction_id:
self.resetFrame()
else:
callback(result) # defer or push to a thread?
return True
30 changes: 15 additions & 15 deletions pymodbus/framer/old_framer_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ def __init__(self, decoder, client=None):
self._hsize = 0x0
self.message_handler = FramerTLS(decoder, [0])

def frameProcessIncomingPacket(self, _single, callback, _slave, tid=None):
def frameProcessIncomingPacket(self, _single, callback, _slave, _tid):
"""Process new packet pattern."""
# no slave id for Modbus Security Application Protocol

while True:
used_len, data = self.message_handler.decode(self._buffer)
if not data:
return
self.dev_id = self.message_handler.incoming_dev_id
self.tid = self.message_handler.incoming_tid

if (result := self.decoder.decode(data)) is None:
self.resetFrame()
raise ModbusIOException("Unable to decode request")
result.slave_id = self.dev_id
result.transaction_id = self.tid
self._buffer: bytes = self._buffer[used_len:]
callback(result) # defer or push to a thread?
used_len, data = self.message_handler.decode(self._buffer)
if not data:
return False
self.dev_id = self.message_handler.incoming_dev_id
self.tid = self.message_handler.incoming_tid

if (result := self.decoder.decode(data)) is None:
self.resetFrame()
raise ModbusIOException("Unable to decode request")
result.slave_id = self.dev_id
result.transaction_id = self.tid
self._buffer: bytes = self._buffer[used_len:]
callback(result) # defer or push to a thread?
return True

0 comments on commit 558ac74

Please sign in to comment.