Skip to content

Commit

Permalink
clean ProcessIncommingPacket. (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Apr 13, 2023
1 parent 33dbcfb commit f9febb9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 49 deletions.
26 changes: 13 additions & 13 deletions pymodbus/framer/ascii_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,20 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
single = kwargs.get("single", False)
self.addToFrame(data)
while self.isFrameReady():
if self.checkFrame():
if self._validate_slave_id(slave, single):
frame = self.getFrame()
if (result := self.decoder.decode(frame)) is None:
raise ModbusIOException("Unable to decode response")
self.populateResult(result)
self.advanceFrame()
callback(result) # defer this
else:
header_txt = self._header["uid"]
Log.error("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
else:
if not self.checkFrame():
break
if not self._validate_slave_id(slave, single):
header_txt = self._header["uid"]
Log.error("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
continue

frame = self.getFrame()
if (result := self.decoder.decode(frame)) is None:
raise ModbusIOException("Unable to decode response")
self.populateResult(result)
self.advanceFrame()
callback(result) # defer this

def buildPacket(self, message):
"""Create a ready to send modbus packet.
Expand Down
25 changes: 11 additions & 14 deletions pymodbus/framer/binary_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,20 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
slave = [slave]
single = kwargs.get("single", False)
while self.isFrameReady():
if self.checkFrame():
if self._validate_slave_id(slave, single):
if (result := self.decoder.decode(self.getFrame())) is None:
raise ModbusIOException("Unable to decode response")
self.populateResult(result)
self.advanceFrame()
callback(result) # defer or push to a thread?
else:
header_txt = self._header["uid"]
Log.debug("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
break

else:
if not self.checkFrame():
Log.debug("Frame check failed, ignoring!!")
self.resetFrame()
break
if not self._validate_slave_id(slave, single):
header_txt = self._header["uid"]
Log.debug("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
break
if (result := self.decoder.decode(self.getFrame())) is None:
raise ModbusIOException("Unable to decode response")
self.populateResult(result)
self.advanceFrame()
callback(result) # defer or push to a thread?

def buildPacket(self, message):
"""Create a ready to send modbus packet.
Expand Down
23 changes: 11 additions & 12 deletions pymodbus/framer/socket_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,22 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
Log.debug("Processing: {}", data, ":hex")
self.addToFrame(data)
while True:
if self.isFrameReady():
if self.checkFrame():
if self._validate_slave_id(slave, single):
self._process(callback)
else:
header_txt = self._header["uid"]
Log.debug("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
else:
Log.debug("Frame check failed, ignoring!!")
self.resetFrame()
else:
if not self.isFrameReady():
if len(self._buffer):
# Possible error ???
if self._header["len"] < 2:
self._process(callback, error=True)
break
if not self.checkFrame():
Log.debug("Frame check failed, ignoring!!")
self.resetFrame()
continue
if not self._validate_slave_id(slave, single):
header_txt = self._header["uid"]
Log.debug("Not a valid slave id - {}, ignoring!!", header_txt)
self.resetFrame()
continue
self._process(callback)

def _process(self, callback, error=False):
"""Process incoming packets irrespective error condition."""
Expand Down
20 changes: 10 additions & 10 deletions pymodbus/framer/tls_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
Log.debug("Processing: {}", data, ":hex")
self.addToFrame(data)

if self.isFrameReady():
if self.checkFrame():
if self._validate_slave_id(slave, single):
self._process(callback)
else:
Log.debug("Not in valid slave id - {}, ignoring!!", slave)
self.resetFrame()
else:
Log.debug("Frame check failed, ignoring!!")
self.resetFrame()
if not self.isFrameReady():
return
if not self.checkFrame():
Log.debug("Frame check failed, ignoring!!")
self.resetFrame()
return
if not self._validate_slave_id(slave, single):
Log.debug("Not in valid slave id - {}, ignoring!!", slave)
self.resetFrame()
self._process(callback)

def _process(self, callback, error=False):
"""Process incoming packets irrespective error condition."""
Expand Down

0 comments on commit f9febb9

Please sign in to comment.