Skip to content

Commit

Permalink
update modbusrtuframer.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Mar 14, 2023
1 parent cb3a41e commit 593114c
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=missing-type-doc
import struct
import time
from typing import List

from pymodbus.exceptions import (
InvalidMessageReceivedException,
Expand Down Expand Up @@ -191,6 +192,15 @@ def populateResult(self, result):
result.slave_id = self._header["uid"]
result.transaction_id = self._header["uid"]

def isFrameIntendedForUs(self, _slaves: List[int]):
"""Check if slave is one we want."""
# slave = self._buffer[0]
# if slaves and slave not in slaves:
# return False

# have a look at _validate_slave_id it does the same
return True

# ----------------------------------------------------------------------- #
# Public Member Functions
# ----------------------------------------------------------------------- #
Expand All @@ -217,22 +227,24 @@ def processIncomingPacket(self, data, callback, slave, **kwargs):
self.addToFrame(data)
single = kwargs.get("single", False)
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()
break
else:
Log.debug("Frame check failed, ignoring!!")
self.resetFrame()
break
else:
if not self.isFrameIntendedForUs(slave):
self.resetFrame()
Log.debug(f"Frame not intended for us, ignoring!! - [{data}]")
break
if not self.isFrameReady():
Log.debug("Frame - [{}] not ready", data)
break
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
self._process(callback)
Log.debug(f"Frame responded to!! - [{data}]")

def buildPacket(self, message):
"""Create a ready to send modbus packet.
Expand Down

0 comments on commit 593114c

Please sign in to comment.