diff --git a/pymodbus/framer/__init__.py b/pymodbus/framer/__init__.py index 949a60ae06..94c840e5e4 100644 --- a/pymodbus/framer/__init__.py +++ b/pymodbus/framer/__init__.py @@ -1,60 +1,17 @@ -"""Framer start.""" -# pylint: disable=missing-type-doc - - -# Unit ID, Function Code -BYTE_ORDER = ">" -FRAME_HEADER = "BB" - -# Transaction Id, Protocol ID, Length, Unit ID, Function Code -SOCKET_FRAME_HEADER = BYTE_ORDER + "HHH" + FRAME_HEADER - -# Function Code -TLS_FRAME_HEADER = BYTE_ORDER + "B" - - -class ModbusFramer: - """Base Framer class.""" - - name = "" - - def __init__(self, decoder, client=None): - """Initialize a new instance of the framer. - - :param decoder: The decoder implementation to use - """ - self.decoder = decoder - self.client = client - - def _validate_slave_id(self, slaves, single): - """Validate if the received data is valid for the client. - - :param slaves: list of slave id for which the transaction is valid - :param single: Set to true to treat this as a single context - :return: - """ - if single: - return True - if 0 in slaves or 0xFF in slaves: - # Handle Modbus TCP slave identifier (0x00 0r 0xFF) - # in asynchronous requests - return True - return self._header["uid"] in slaves # pylint: disable=no-member - - def sendPacket(self, message): - """Send packets on the bus. - - With 3.5char delay between frames - :param message: Message to be sent over the bus - :return: - """ - return self.client.send(message) - - def recvPacket(self, size): - """Receive packet from the bus. - - With specified len - :param size: Number of bytes to read - :return: - """ - return self.client.recv(size) +"""Framer""" + +__all__ = [ + "ModbusFramer", + "ModbusAsciiFramer", + "ModbusBinaryFramer", + "ModbusRtuFramer", + "ModbusSocketFramer", + "ModbusTlsFramer", +] + +from pymodbus.framer.ascii_framer import ModbusAsciiFramer +from pymodbus.framer.base import ModbusFramer +from pymodbus.framer.binary_framer import ModbusBinaryFramer +from pymodbus.framer.rtu_framer import ModbusRtuFramer +from pymodbus.framer.socket_framer import ModbusSocketFramer +from pymodbus.framer.tls_framer import ModbusTlsFramer diff --git a/pymodbus/framer/ascii_framer.py b/pymodbus/framer/ascii_framer.py index 1efaa9aeb8..ec6a6e0576 100644 --- a/pymodbus/framer/ascii_framer.py +++ b/pymodbus/framer/ascii_framer.py @@ -4,7 +4,7 @@ from binascii import a2b_hex, b2a_hex from pymodbus.exceptions import ModbusIOException -from pymodbus.framer import BYTE_ORDER, FRAME_HEADER, ModbusFramer +from pymodbus.framer.base import BYTE_ORDER, FRAME_HEADER, ModbusFramer from pymodbus.logging import Log from pymodbus.utilities import checkLRC, computeLRC diff --git a/pymodbus/framer/base.py b/pymodbus/framer/base.py new file mode 100644 index 0000000000..949a60ae06 --- /dev/null +++ b/pymodbus/framer/base.py @@ -0,0 +1,60 @@ +"""Framer start.""" +# pylint: disable=missing-type-doc + + +# Unit ID, Function Code +BYTE_ORDER = ">" +FRAME_HEADER = "BB" + +# Transaction Id, Protocol ID, Length, Unit ID, Function Code +SOCKET_FRAME_HEADER = BYTE_ORDER + "HHH" + FRAME_HEADER + +# Function Code +TLS_FRAME_HEADER = BYTE_ORDER + "B" + + +class ModbusFramer: + """Base Framer class.""" + + name = "" + + def __init__(self, decoder, client=None): + """Initialize a new instance of the framer. + + :param decoder: The decoder implementation to use + """ + self.decoder = decoder + self.client = client + + def _validate_slave_id(self, slaves, single): + """Validate if the received data is valid for the client. + + :param slaves: list of slave id for which the transaction is valid + :param single: Set to true to treat this as a single context + :return: + """ + if single: + return True + if 0 in slaves or 0xFF in slaves: + # Handle Modbus TCP slave identifier (0x00 0r 0xFF) + # in asynchronous requests + return True + return self._header["uid"] in slaves # pylint: disable=no-member + + def sendPacket(self, message): + """Send packets on the bus. + + With 3.5char delay between frames + :param message: Message to be sent over the bus + :return: + """ + return self.client.send(message) + + def recvPacket(self, size): + """Receive packet from the bus. + + With specified len + :param size: Number of bytes to read + :return: + """ + return self.client.recv(size) diff --git a/pymodbus/framer/binary_framer.py b/pymodbus/framer/binary_framer.py index 74f9101bb9..8a6606ff4f 100644 --- a/pymodbus/framer/binary_framer.py +++ b/pymodbus/framer/binary_framer.py @@ -3,7 +3,7 @@ import struct from pymodbus.exceptions import ModbusIOException -from pymodbus.framer import BYTE_ORDER, FRAME_HEADER, ModbusFramer +from pymodbus.framer.base import BYTE_ORDER, FRAME_HEADER, ModbusFramer from pymodbus.logging import Log from pymodbus.utilities import checkCRC, computeCRC diff --git a/pymodbus/framer/rtu_framer.py b/pymodbus/framer/rtu_framer.py index af26cf88f6..b24cfb3ef2 100644 --- a/pymodbus/framer/rtu_framer.py +++ b/pymodbus/framer/rtu_framer.py @@ -7,7 +7,7 @@ InvalidMessageReceivedException, ModbusIOException, ) -from pymodbus.framer import BYTE_ORDER, FRAME_HEADER, ModbusFramer +from pymodbus.framer.base import BYTE_ORDER, FRAME_HEADER, ModbusFramer from pymodbus.logging import Log from pymodbus.utilities import ModbusTransactionState, checkCRC, computeCRC diff --git a/pymodbus/framer/socket_framer.py b/pymodbus/framer/socket_framer.py index 341e5d1f15..d0569cc6bc 100644 --- a/pymodbus/framer/socket_framer.py +++ b/pymodbus/framer/socket_framer.py @@ -6,7 +6,7 @@ InvalidMessageReceivedException, ModbusIOException, ) -from pymodbus.framer import SOCKET_FRAME_HEADER, ModbusFramer +from pymodbus.framer.base import SOCKET_FRAME_HEADER, ModbusFramer from pymodbus.logging import Log diff --git a/pymodbus/framer/tls_framer.py b/pymodbus/framer/tls_framer.py index 944ed616b1..252bedb0b6 100644 --- a/pymodbus/framer/tls_framer.py +++ b/pymodbus/framer/tls_framer.py @@ -6,7 +6,7 @@ InvalidMessageReceivedException, ModbusIOException, ) -from pymodbus.framer import TLS_FRAME_HEADER, ModbusFramer +from pymodbus.framer.base import TLS_FRAME_HEADER, ModbusFramer from pymodbus.logging import Log