Skip to content

Commit

Permalink
Clean framer/__init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Apr 14, 2023
1 parent 2f12b4e commit f4046b7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 65 deletions.
77 changes: 17 additions & 60 deletions pymodbus/framer/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pymodbus/framer/ascii_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 60 additions & 0 deletions pymodbus/framer/base.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion pymodbus/framer/binary_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pymodbus/framer/rtu_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pymodbus/framer/socket_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion pymodbus/framer/tls_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit f4046b7

Please sign in to comment.