venv added, updated

This commit is contained in:
Norbert
2024-09-13 09:46:28 +02:00
parent 577596d9f3
commit 82af8c809a
4812 changed files with 640223 additions and 2 deletions

View File

@@ -0,0 +1,22 @@
"""Pymodbus: Modbus Protocol Implementation.
Released under the BSD license
"""
__all__ = [
"ExceptionResponse",
"FramerType",
"ModbusException",
"pymodbus_apply_logging_config",
"__version__",
"__version_full__",
]
from pymodbus.exceptions import ModbusException
from pymodbus.framer import FramerType
from pymodbus.logging import pymodbus_apply_logging_config
from pymodbus.pdu import ExceptionResponse
__version__ = "3.7.2"
__version_full__ = f"[pymodbus, version {__version__}]"

View File

@@ -0,0 +1,19 @@
"""Client."""
__all__ = [
"AsyncModbusSerialClient",
"AsyncModbusTcpClient",
"AsyncModbusTlsClient",
"AsyncModbusUdpClient",
"ModbusBaseClient",
"ModbusSerialClient",
"ModbusTcpClient",
"ModbusTlsClient",
"ModbusUdpClient",
]
from pymodbus.client.base import ModbusBaseClient
from pymodbus.client.serial import AsyncModbusSerialClient, ModbusSerialClient
from pymodbus.client.tcp import AsyncModbusTcpClient, ModbusTcpClient
from pymodbus.client.tls import AsyncModbusTlsClient, ModbusTlsClient
from pymodbus.client.udp import AsyncModbusUdpClient, ModbusUdpClient

View File

@@ -0,0 +1,303 @@
"""Base for all clients."""
from __future__ import annotations
import asyncio
import socket
from abc import abstractmethod
from collections.abc import Awaitable, Callable
from typing import cast
from pymodbus.client.mixin import ModbusClientMixin
from pymodbus.client.modbusclientprotocol import ModbusClientProtocol
from pymodbus.exceptions import ConnectionException, ModbusIOException
from pymodbus.factory import ClientDecoder
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerType, ModbusFramer
from pymodbus.logging import Log
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.transaction import SyncModbusTransactionManager
from pymodbus.transport import CommParams
from pymodbus.utilities import ModbusTransactionState
class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]]):
"""**ModbusBaseClient**.
:mod:`ModbusBaseClient` is normally not referenced outside :mod:`pymodbus`.
"""
def __init__(
self,
framer: FramerType,
retries: int,
on_connect_callback: Callable[[bool], None] | None,
comm_params: CommParams | None = None,
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
if comm_params:
self.comm_params = comm_params
self.retries = retries
self.ctx = ModbusClientProtocol(
framer,
self.comm_params,
on_connect_callback,
)
# Common variables.
self.use_udp = False
self.state = ModbusTransactionState.IDLE
self.last_frame_end: float | None = 0
self.silent_interval: float = 0
self._lock = asyncio.Lock()
@property
def connected(self) -> bool:
"""Return state of connection."""
return self.ctx.is_active()
async def connect(self) -> bool:
"""Call transport connect."""
self.ctx.reset_delay()
Log.debug(
"Connecting to {}:{}.",
self.ctx.comm_params.host,
self.ctx.comm_params.port,
)
return await self.ctx.connect()
def register(self, custom_response_class: ModbusResponse) -> None:
"""Register a custom response class with the decoder (call **sync**).
:param custom_response_class: (optional) Modbus response class.
:raises MessageRegisterException: Check exception text.
Use register() to add non-standard responses (like e.g. a login prompt) and
have them interpreted automatically.
"""
self.ctx.framer.decoder.register(custom_response_class)
def close(self, reconnect: bool = False) -> None:
"""Close connection."""
if reconnect:
self.ctx.connection_lost(asyncio.TimeoutError("Server not responding"))
else:
self.ctx.close()
def idle_time(self) -> float:
"""Time before initiating next transaction (call **sync**).
Applications can call message functions without checking idle_time(),
this is done automatically.
"""
if self.last_frame_end is None or self.silent_interval is None:
return 0
return self.last_frame_end + self.silent_interval
def execute(self, request: ModbusRequest):
"""Execute request and get response (call **sync/async**).
:param request: The request to process
:returns: The result of the request execution
:raises ConnectionException: Check exception text.
"""
if not self.ctx.transport:
raise ConnectionException(f"Not connected[{self!s}]")
return self.async_execute(request)
async def async_execute(self, request) -> ModbusResponse:
"""Execute requests asynchronously."""
request.transaction_id = self.ctx.transaction.getNextTID()
packet = self.ctx.framer.buildPacket(request)
count = 0
while count <= self.retries:
async with self._lock:
req = self.build_response(request)
self.ctx.framer.resetFrame()
self.ctx.send(packet)
if not request.slave_id:
resp = None
break
try:
resp = await asyncio.wait_for(
req, timeout=self.ctx.comm_params.timeout_connect
)
break
except asyncio.exceptions.TimeoutError:
count += 1
if count > self.retries:
self.close(reconnect=True)
raise ModbusIOException(
f"ERROR: No response received after {self.retries} retries"
)
return resp # type: ignore[return-value]
def build_response(self, request: ModbusRequest):
"""Return a deferred response for the current request."""
my_future: asyncio.Future = asyncio.Future()
request.fut = my_future
if not self.ctx.transport:
if not my_future.done():
my_future.set_exception(ConnectionException("Client is not connected"))
else:
self.ctx.transaction.addTransaction(request)
return my_future
async def __aenter__(self):
"""Implement the client with enter block.
:returns: The current instance of the client
:raises ConnectionException:
"""
await self.connect()
return self
async def __aexit__(self, klass, value, traceback):
"""Implement the client with aexit block."""
self.close()
def __str__(self):
"""Build a string representation of the connection.
:returns: The string representation
"""
return (
f"{self.__class__.__name__} {self.ctx.comm_params.host}:{self.ctx.comm_params.port}"
)
class ModbusBaseSyncClient(ModbusClientMixin[ModbusResponse]):
"""**ModbusBaseClient**.
:mod:`ModbusBaseClient` is normally not referenced outside :mod:`pymodbus`.
"""
def __init__(
self,
framer: FramerType,
retries: int,
comm_params: CommParams | None = None,
) -> None:
"""Initialize a client instance."""
ModbusClientMixin.__init__(self) # type: ignore[arg-type]
if comm_params:
self.comm_params = comm_params
self.retries = retries
self.slaves: list[int] = []
# Common variables.
self.framer: ModbusFramer = FRAMER_NAME_TO_CLASS.get(
framer, cast(type[ModbusFramer], framer)
)(ClientDecoder(), self)
self.transaction = SyncModbusTransactionManager(
self,
self.retries,
)
self.reconnect_delay_current = self.comm_params.reconnect_delay or 0
self.use_udp = False
self.state = ModbusTransactionState.IDLE
self.last_frame_end: float | None = 0
self.silent_interval: float = 0
self.transport = None
# ----------------------------------------------------------------------- #
# Client external interface
# ----------------------------------------------------------------------- #
def register(self, custom_response_class: ModbusResponse) -> None:
"""Register a custom response class with the decoder (call **sync**).
:param custom_response_class: (optional) Modbus response class.
:raises MessageRegisterException: Check exception text.
Use register() to add non-standard responses (like e.g. a login prompt) and
have them interpreted automatically.
"""
self.framer.decoder.register(custom_response_class)
def idle_time(self) -> float:
"""Time before initiating next transaction (call **sync**).
Applications can call message functions without checking idle_time(),
this is done automatically.
"""
if self.last_frame_end is None or self.silent_interval is None:
return 0
return self.last_frame_end + self.silent_interval
def execute(self, request: ModbusRequest) -> ModbusResponse:
"""Execute request and get response (call **sync/async**).
:param request: The request to process
:returns: The result of the request execution
:raises ConnectionException: Check exception text.
"""
if not self.connect():
raise ConnectionException(f"Failed to connect[{self!s}]")
return self.transaction.execute(request)
# ----------------------------------------------------------------------- #
# Internal methods
# ----------------------------------------------------------------------- #
def _start_send(self):
"""Send request.
:meta private:
"""
if self.state != ModbusTransactionState.RETRYING:
Log.debug('New Transaction state "SENDING"')
self.state = ModbusTransactionState.SENDING
@abstractmethod
def send(self, request: bytes) -> int:
"""Send request.
:meta private:
"""
@abstractmethod
def recv(self, size: int | None) -> bytes:
"""Receive data.
:meta private:
"""
@classmethod
def get_address_family(cls, address):
"""Get the correct address family."""
try:
_ = socket.inet_pton(socket.AF_INET6, address)
except OSError: # not a valid ipv6 address
return socket.AF_INET
return socket.AF_INET6
def connect(self) -> bool: # type: ignore[empty-body]
"""Connect to other end, overwritten."""
def close(self):
"""Close connection, overwritten."""
# ----------------------------------------------------------------------- #
# The magic methods
# ----------------------------------------------------------------------- #
def __enter__(self):
"""Implement the client with enter block.
:returns: The current instance of the client
:raises ConnectionException:
"""
self.connect()
return self
def __exit__(self, klass, value, traceback):
"""Implement the client with exit block."""
self.close()
def __str__(self):
"""Build a string representation of the connection.
:returns: The string representation
"""
return (
f"{self.__class__.__name__} {self.comm_params.host}:{self.comm_params.port}"
)

View File

@@ -0,0 +1,503 @@
"""Modbus Client Common."""
from __future__ import annotations
import struct
from enum import Enum
from typing import Generic, TypeVar
import pymodbus.pdu.bit_read_message as pdu_bit_read
import pymodbus.pdu.bit_write_message as pdu_bit_write
import pymodbus.pdu.diag_message as pdu_diag
import pymodbus.pdu.file_message as pdu_file_msg
import pymodbus.pdu.mei_message as pdu_mei
import pymodbus.pdu.other_message as pdu_other_msg
import pymodbus.pdu.register_read_message as pdu_reg_read
import pymodbus.pdu.register_write_message as pdu_req_write
from pymodbus.exceptions import ModbusException
from pymodbus.pdu import ModbusRequest
T = TypeVar("T", covariant=False)
class ModbusClientMixin(Generic[T]): # pylint: disable=too-many-public-methods
"""**ModbusClientMixin**.
This is an interface class to facilitate the sending requests/receiving responses like read_coils.
execute() allows to make a call with non-standard or user defined function codes (remember to add a PDU
in the transport class to interpret the request/response).
Simple modbus message call::
response = client.read_coils(1, 10)
# or
response = await client.read_coils(1, 10)
Advanced modbus message call::
request = ReadCoilsRequest(1,10)
response = client.execute(request)
# or
request = ReadCoilsRequest(1,10)
response = await client.execute(request)
.. tip::
All methods can be used directly (synchronous) or
with await <method> (asynchronous) depending on the client used.
"""
def __init__(self):
"""Initialize."""
def execute(self, _request: ModbusRequest) -> T:
"""Execute request (code ???).
:raises ModbusException:
Call with custom function codes.
.. tip::
Response is not interpreted.
"""
raise NotImplementedError("execute of ModbusClientMixin needs to be overridden")
def read_coils(self, address: int, count: int = 1, slave: int = 1) -> T:
"""Read coils (code 0x01).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_bit_read.ReadCoilsRequest(address, count, slave=slave))
def read_discrete_inputs(self, address: int, count: int = 1, slave: int = 1) -> T:
"""Read discrete inputs (code 0x02).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_bit_read.ReadDiscreteInputsRequest(address, count, slave=slave))
def read_holding_registers(self, address: int, count: int = 1, slave: int = 1) -> T:
"""Read holding registers (code 0x03).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_reg_read.ReadHoldingRegistersRequest(address, count, slave=slave))
def read_input_registers(self, address: int, count: int = 1, slave: int = 1) -> T:
"""Read input registers (code 0x04).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_reg_read.ReadInputRegistersRequest(address, count, slave=slave))
def write_coil(self, address: int, value: bool, slave: int = 1) -> T:
"""Write single coil (code 0x05).
:param address: Address to write to
:param value: Boolean to write
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_bit_write.WriteSingleCoilRequest(address, value, slave=slave))
def write_register(self, address: int, value: int, slave: int = 1) -> T:
"""Write register (code 0x06).
:param address: Address to write to
:param value: Value to write
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_req_write.WriteSingleRegisterRequest(address, value, slave=slave))
def read_exception_status(self, slave: int = 1) -> T:
"""Read Exception Status (code 0x07).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_other_msg.ReadExceptionStatusRequest(slave=slave))
def diag_query_data(
self, msg: bytes, slave: int = 1) -> T:
"""Diagnose query data (code 0x08 sub 0x00).
:param msg: Message to be returned
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.ReturnQueryDataRequest(msg, slave=slave))
def diag_restart_communication(
self, toggle: bool, slave: int = 1) -> T:
"""Diagnose restart communication (code 0x08 sub 0x01).
:param toggle: True if toggled.
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.RestartCommunicationsOptionRequest(toggle, slave=slave)
)
def diag_read_diagnostic_register(self, slave: int = 1) -> T:
"""Diagnose read diagnostic register (code 0x08 sub 0x02).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnDiagnosticRegisterRequest(slave=slave)
)
def diag_change_ascii_input_delimeter(self, slave: int = 1) -> T:
"""Diagnose change ASCII input delimiter (code 0x08 sub 0x03).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ChangeAsciiInputDelimiterRequest(slave=slave)
)
def diag_force_listen_only(self, slave: int = 1) -> T:
"""Diagnose force listen only (code 0x08 sub 0x04).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.ForceListenOnlyModeRequest(slave=slave))
def diag_clear_counters(self, slave: int = 1) -> T:
"""Diagnose clear counters (code 0x08 sub 0x0A).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.ClearCountersRequest(slave=slave))
def diag_read_bus_message_count(self, slave: int = 1) -> T:
"""Diagnose read bus message count (code 0x08 sub 0x0B).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnBusMessageCountRequest(slave=slave)
)
def diag_read_bus_comm_error_count(self, slave: int = 1) -> T:
"""Diagnose read Bus Communication Error Count (code 0x08 sub 0x0C).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnBusCommunicationErrorCountRequest(slave=slave)
)
def diag_read_bus_exception_error_count(self, slave: int = 1) -> T:
"""Diagnose read Bus Exception Error Count (code 0x08 sub 0x0D).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnBusExceptionErrorCountRequest(slave=slave)
)
def diag_read_slave_message_count(self, slave: int = 1) -> T:
"""Diagnose read Slave Message Count (code 0x08 sub 0x0E).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnSlaveMessageCountRequest(slave=slave)
)
def diag_read_slave_no_response_count(self, slave: int = 1) -> T:
"""Diagnose read Slave No Response Count (code 0x08 sub 0x0F).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnSlaveNoResponseCountRequest(slave=slave)
)
def diag_read_slave_nak_count(self, slave: int = 1) -> T:
"""Diagnose read Slave NAK Count (code 0x08 sub 0x10).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.ReturnSlaveNAKCountRequest(slave=slave))
def diag_read_slave_busy_count(self, slave: int = 1) -> T:
"""Diagnose read Slave Busy Count (code 0x08 sub 0x11).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.ReturnSlaveBusyCountRequest(slave=slave))
def diag_read_bus_char_overrun_count(self, slave: int = 1) -> T:
"""Diagnose read Bus Character Overrun Count (code 0x08 sub 0x12).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnSlaveBusCharacterOverrunCountRequest(slave=slave)
)
def diag_read_iop_overrun_count(self, slave: int = 1) -> T:
"""Diagnose read Iop overrun count (code 0x08 sub 0x13).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_diag.ReturnIopOverrunCountRequest(slave=slave)
)
def diag_clear_overrun_counter(self, slave: int = 1) -> T:
"""Diagnose Clear Overrun Counter and Flag (code 0x08 sub 0x14).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.ClearOverrunCountRequest(slave=slave))
def diag_getclear_modbus_response(self, slave: int = 1) -> T:
"""Diagnose Get/Clear modbus plus (code 0x08 sub 0x15).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_diag.GetClearModbusPlusRequest(slave=slave))
def diag_get_comm_event_counter(self, slave: int = 1) -> T:
"""Diagnose get event counter (code 0x0B).
:raises ModbusException:
"""
return self.execute(pdu_other_msg.GetCommEventCounterRequest(slave=slave))
def diag_get_comm_event_log(self, slave: int = 1) -> T:
"""Diagnose get event counter (code 0x0C).
:raises ModbusException:
"""
return self.execute(pdu_other_msg.GetCommEventLogRequest(slave=slave))
def write_coils(
self,
address: int,
values: list[bool] | bool,
slave: int = 1,
) -> T:
"""Write coils (code 0x0F).
:param address: Start address to write to
:param values: List of booleans to write, or a single boolean to write
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(
pdu_bit_write.WriteMultipleCoilsRequest(address, values, slave)
)
def write_registers(
self, address: int, values: list[int], slave: int = 1, skip_encode: bool = False) -> T:
"""Write registers (code 0x10).
:param address: Start address to write to
:param values: List of values to write
:param slave: (optional) Modbus slave ID
:param skip_encode: (optional) do not encode values
:raises ModbusException:
"""
return self.execute(
pdu_req_write.WriteMultipleRegistersRequest(address, values, slave=slave, skip_encode=skip_encode)
)
def report_slave_id(self, slave: int = 1) -> T:
"""Report slave ID (code 0x11).
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
return self.execute(pdu_other_msg.ReportSlaveIdRequest(slave=slave))
def read_file_record(self, records: list[tuple], slave: int = 1) -> T:
"""Read file record (code 0x14).
:param records: List of (Reference type, File number, Record Number, Record Length)
:param slave: device id
:raises ModbusException:
"""
return self.execute(pdu_file_msg.ReadFileRecordRequest(records, slave=slave))
def write_file_record(self, records: list[tuple], slave: int = 1) -> T:
"""Write file record (code 0x15).
:param records: List of (Reference type, File number, Record Number, Record Length)
:param slave: (optional) Device id
:raises ModbusException:
"""
return self.execute(pdu_file_msg.WriteFileRecordRequest(records, slave=slave))
def mask_write_register(
self,
address: int = 0x0000,
and_mask: int = 0xFFFF,
or_mask: int = 0x0000,
slave: int = 1,
) -> T:
"""Mask write register (code 0x16).
:param address: The mask pointer address (0x0000 to 0xffff)
:param and_mask: The and bitmask to apply to the register address
:param or_mask: The or bitmask to apply to the register address
:param slave: (optional) device id
:raises ModbusException:
"""
return self.execute(
pdu_req_write.MaskWriteRegisterRequest(address, and_mask, or_mask, slave=slave)
)
def readwrite_registers(
self,
read_address: int = 0,
read_count: int = 0,
write_address: int = 0,
address: int | None = None,
values: list[int] | int = 0,
slave: int = 1,
) -> T:
"""Read/Write registers (code 0x17).
:param read_address: The address to start reading from
:param read_count: The number of registers to read from address
:param write_address: The address to start writing to
:param address: (optional) use as read/write address
:param values: List of values to write, or a single value to write
:param slave: (optional) Modbus slave ID
:raises ModbusException:
"""
if address:
read_address = address
write_address = address
return self.execute(
pdu_reg_read.ReadWriteMultipleRegistersRequest(
read_address=read_address,
read_count=read_count,
write_address=write_address,
write_registers=values,
slave=slave,
)
)
def read_fifo_queue(self, address: int = 0x0000, slave: int = 1) -> T:
"""Read FIFO queue (code 0x18).
:param address: The address to start reading from
:param slave: (optional) device id
:raises ModbusException:
"""
return self.execute(pdu_file_msg.ReadFifoQueueRequest(address, slave=slave))
# code 0x2B sub 0x0D: CANopen General Reference Request and Response, NOT IMPLEMENTED
def read_device_information(
self, read_code: int | None = None, object_id: int = 0x00, slave: int = 1) -> T:
"""Read FIFO queue (code 0x2B sub 0x0E).
:param read_code: The device information read code
:param object_id: The object to read from
:param slave: (optional) Device id
:raises ModbusException:
"""
return self.execute(
pdu_mei.ReadDeviceInformationRequest(read_code, object_id, slave=slave)
)
# ------------------
# Converter methods
# ------------------
class DATATYPE(Enum):
"""Datatype enum (name and number of bytes), used for convert_* calls."""
INT16 = ("h", 1)
UINT16 = ("H", 1)
INT32 = ("i", 2)
UINT32 = ("I", 2)
INT64 = ("q", 4)
UINT64 = ("Q", 4)
FLOAT32 = ("f", 2)
FLOAT64 = ("d", 4)
STRING = ("s", 0)
@classmethod
def convert_from_registers(
cls, registers: list[int], data_type: DATATYPE
) -> int | float | str:
"""Convert registers to int/float/str.
:param registers: list of registers received from e.g. read_holding_registers()
:param data_type: data type to convert to
:returns: int, float or str depending on "data_type"
:raises ModbusException: when size of registers is not 1, 2 or 4
"""
byte_list = bytearray()
for x in registers:
byte_list.extend(int.to_bytes(x, 2, "big"))
if data_type == cls.DATATYPE.STRING:
if byte_list[-1:] == b"\00":
byte_list = byte_list[:-1]
return byte_list.decode("utf-8")
if len(registers) != data_type.value[1]:
raise ModbusException(
f"Illegal size ({len(registers)}) of register array, cannot convert!"
)
return struct.unpack(f">{data_type.value[0]}", byte_list)[0]
@classmethod
def convert_to_registers(
cls, value: int | float | str, data_type: DATATYPE
) -> list[int]:
"""Convert int/float/str to registers (16/32/64 bit).
:param value: value to be converted
:param data_type: data type to be encoded as registers
:returns: List of registers, can be used directly in e.g. write_registers()
:raises TypeError: when there is a mismatch between data_type and value
"""
if data_type == cls.DATATYPE.STRING:
if not isinstance(value, str):
raise TypeError(f"Value should be string but is {type(value)}.")
byte_list = value.encode()
if len(byte_list) % 2:
byte_list += b"\x00"
else:
byte_list = struct.pack(f">{data_type.value[0]}", value)
regs = [
int.from_bytes(byte_list[x : x + 2], "big")
for x in range(0, len(byte_list), 2)
]
return regs

View File

@@ -0,0 +1,81 @@
"""ModbusProtocol implementation for all clients."""
from __future__ import annotations
from collections.abc import Callable
from typing import cast
from pymodbus.factory import ClientDecoder
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerType, ModbusFramer
from pymodbus.logging import Log
from pymodbus.transaction import ModbusTransactionManager
from pymodbus.transport import CommParams, ModbusProtocol
class ModbusClientProtocol(ModbusProtocol):
"""**ModbusClientProtocol**.
:mod:`ModbusClientProtocol` is normally not referenced outside :mod:`pymodbus`.
"""
def __init__(
self,
framer: FramerType,
params: CommParams,
on_connect_callback: Callable[[bool], None] | None = None,
) -> None:
"""Initialize a client instance."""
ModbusProtocol.__init__(
self,
params,
False,
)
self.on_connect_callback = on_connect_callback
# Common variables.
self.framer = FRAMER_NAME_TO_CLASS.get(
framer, cast(type[ModbusFramer], framer)
)(ClientDecoder(), self)
self.transaction = ModbusTransactionManager()
def _handle_response(self, reply):
"""Handle the processed response and link to correct deferred."""
if reply is not None:
tid = reply.transaction_id
if handler := self.transaction.getTransaction(tid):
reply.request = handler
if not handler.fut.done():
handler.fut.set_result(reply)
else:
Log.debug("Unrequested message: {}", reply, ":str")
def callback_new_connection(self):
"""Call when listener receive new connection request."""
def callback_connected(self) -> None:
"""Call when connection is succcesfull."""
if self.on_connect_callback:
self.loop.call_soon(self.on_connect_callback, True)
self.framer.resetFrame()
def callback_disconnected(self, exc: Exception | None) -> None:
"""Call when connection is lost."""
Log.debug("callback_disconnected called: {}", exc)
if self.on_connect_callback:
self.loop.call_soon(self.on_connect_callback, False)
def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data.
returns number of bytes consumed
"""
self.framer.processIncomingPacket(data, self._handle_response, 0)
return len(data)
def __str__(self):
"""Build a string representation of the connection.
:returns: The string representation
"""
return (
f"{self.__class__.__name__} {self.comm_params.host}:{self.comm_params.port}"
)

View File

@@ -0,0 +1,308 @@
"""Modbus client async serial communication."""
from __future__ import annotations
import contextlib
import sys
import time
from collections.abc import Callable
from functools import partial
from pymodbus.client.base import ModbusBaseClient, ModbusBaseSyncClient
from pymodbus.exceptions import ConnectionException
from pymodbus.framer import FramerType
from pymodbus.logging import Log
from pymodbus.transport import CommParams, CommType
from pymodbus.utilities import ModbusTransactionState
with contextlib.suppress(ImportError):
import serial
class AsyncModbusSerialClient(ModbusBaseClient):
"""**AsyncModbusSerialClient**.
Fixed parameters:
:param port: Serial port used for communication.
Optional parameters:
:param framer: Framer name, default FramerType.RTU
:param baudrate: Bits per second.
:param bytesize: Number of bits per byte 7-8.
:param parity: 'E'ven, 'O'dd or 'N'one
:param stopbits: Number of stop bits 1, 1.5, 2.
:param handle_local_echo: Discard local echo from dongle.
:param name: Set communication name, used in logging
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
:param on_reconnect_callback: Function that will be called just before a reconnection attempt.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import AsyncModbusSerialClient
async def run():
client = AsyncModbusSerialClient("dev/serial0")
await client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
"""
def __init__( # pylint: disable=too-many-arguments
self,
port: str,
framer: FramerType = FramerType.RTU,
baudrate: int = 19200,
bytesize: int = 8,
parity: str = "N",
stopbits: int = 1,
handle_local_echo: bool = False,
name: str = "comm",
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
on_connect_callback: Callable[[bool], None] | None = None,
) -> None:
"""Initialize Asyncio Modbus Serial Client."""
if "serial" not in sys.modules:
raise RuntimeError(
"Serial client requires pyserial "
'Please install with "pip install pyserial" and try again.'
)
self.comm_params = CommParams(
comm_type=CommType.SERIAL,
host=port,
baudrate=baudrate,
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
handle_local_echo=handle_local_echo,
comm_name=name,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
ModbusBaseClient.__init__(
self,
framer,
retries,
on_connect_callback,
)
def close(self, reconnect: bool = False) -> None:
"""Close connection."""
super().close(reconnect=reconnect)
class ModbusSerialClient(ModbusBaseSyncClient):
"""**ModbusSerialClient**.
Fixed parameters:
:param port: Serial port used for communication.
Optional parameters:
:param framer: Framer name, default FramerType.RTU
:param baudrate: Bits per second.
:param bytesize: Number of bits per byte 7-8.
:param parity: 'E'ven, 'O'dd or 'N'one
:param stopbits: Number of stop bits 0-2.
:param handle_local_echo: Discard local echo from dongle.
:param name: Set communication name, used in logging
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import ModbusSerialClient
def run():
client = ModbusSerialClient("dev/serial0")
client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
Remark: There are no automatic reconnect as with AsyncModbusSerialClient
"""
state = ModbusTransactionState.IDLE
inter_byte_timeout: float = 0
silent_interval: float = 0
def __init__( # pylint: disable=too-many-arguments
self,
port: str,
framer: FramerType = FramerType.RTU,
baudrate: int = 19200,
bytesize: int = 8,
parity: str = "N",
stopbits: int = 1,
handle_local_echo: bool = False,
name: str = "comm",
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
) -> None:
"""Initialize Modbus Serial Client."""
self.comm_params = CommParams(
comm_type=CommType.SERIAL,
host=port,
baudrate=baudrate,
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
handle_local_echo=handle_local_echo,
comm_name=name,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
super().__init__(
framer,
retries,
)
if "serial" not in sys.modules:
raise RuntimeError(
"Serial client requires pyserial "
'Please install with "pip install pyserial" and try again.'
)
self.socket: serial.Serial | None = None
self.last_frame_end = None
self._t0 = float(1 + bytesize + stopbits) / baudrate
# Check every 4 bytes / 2 registers if the reading is ready
self._recv_interval = self._t0 * 4
# Set a minimum of 1ms for high baudrates
self._recv_interval = max(self._recv_interval, 0.001)
if baudrate > 19200:
self.silent_interval = 1.75 / 1000 # ms
else:
self.inter_byte_timeout = 1.5 * self._t0
self.silent_interval = 3.5 * self._t0
self.silent_interval = round(self.silent_interval, 6)
@property
def connected(self):
"""Connect internal."""
return self.connect()
def connect(self) -> bool:
"""Connect to the modbus serial server."""
if self.socket:
return True
try:
self.socket = serial.serial_for_url(
self.comm_params.host,
timeout=self.comm_params.timeout_connect,
bytesize=self.comm_params.bytesize,
stopbits=self.comm_params.stopbits,
baudrate=self.comm_params.baudrate,
parity=self.comm_params.parity,
exclusive=True,
)
self.socket.inter_byte_timeout = self.inter_byte_timeout
self.last_frame_end = None
# except serial.SerialException as msg:
# pyserial raises undocumented exceptions like termios
except Exception as msg: # pylint: disable=broad-exception-caught
Log.error("{}", msg)
self.close()
return self.socket is not None
def close(self):
"""Close the underlying socket connection."""
if self.socket:
self.socket.close()
self.socket = None
def _in_waiting(self):
"""Return waiting bytes."""
return getattr(self.socket, "in_waiting") if hasattr(self.socket, "in_waiting") else getattr(self.socket, "inWaiting")()
def send(self, request: bytes) -> int:
"""Send data on the underlying socket.
If receive buffer still holds some data then flush it.
Sleep if last send finished less than 3.5 character times ago.
"""
super()._start_send()
if not self.socket:
raise ConnectionException(str(self))
if request:
if waitingbytes := self._in_waiting():
result = self.socket.read(waitingbytes)
Log.warning("Cleanup recv buffer before send: {}", result, ":hex")
if (size := self.socket.write(request)) is None:
size = 0
return size
return 0
def _wait_for_data(self) -> int:
"""Wait for data."""
size = 0
more_data = False
condition = partial(
lambda start, timeout: (time.time() - start) <= timeout,
timeout=self.comm_params.timeout_connect,
)
start = time.time()
while condition(start):
available = self._in_waiting()
if (more_data and not available) or (more_data and available == size):
break
if available and available != size:
more_data = True
size = available
time.sleep(self._recv_interval)
return size
def recv(self, size: int | None) -> bytes:
"""Read data from the underlying descriptor."""
if not self.socket:
raise ConnectionException(str(self))
if size is None:
size = self._wait_for_data()
if size > self._in_waiting():
self._wait_for_data()
result = self.socket.read(size)
return result
def is_socket_open(self) -> bool:
"""Check if socket is open."""
if self.socket:
return self.socket.is_open
return False
def __repr__(self):
"""Return string representation."""
return (
f"<{self.__class__.__name__} at {hex(id(self))} socket={self.socket}, "
f"framer={self.framer}, timeout={self.comm_params.timeout_connect}>"
)

View File

@@ -0,0 +1,297 @@
"""Modbus client async TCP communication."""
from __future__ import annotations
import select
import socket
import time
from collections.abc import Callable
from pymodbus.client.base import ModbusBaseClient, ModbusBaseSyncClient
from pymodbus.exceptions import ConnectionException
from pymodbus.framer import FramerType
from pymodbus.logging import Log
from pymodbus.transport import CommParams, CommType
class AsyncModbusTcpClient(ModbusBaseClient):
"""**AsyncModbusTcpClient**.
Fixed parameters:
:param host: Host IP address or host name
Optional parameters:
:param framer: Framer name, default FramerType.SOCKET
:param port: Port used for communication
:param name: Set communication name, used in logging
:param source_address: source address of client
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
:param on_reconnect_callback: Function that will be called just before a reconnection attempt.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import AsyncModbusTcpClient
async def run():
client = AsyncModbusTcpClient("localhost")
await client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
"""
socket: socket.socket | None
def __init__( # pylint: disable=too-many-arguments
self,
host: str,
framer: FramerType = FramerType.SOCKET,
port: int = 502,
name: str = "comm",
source_address: tuple[str, int] | None = None,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
on_connect_callback: Callable[[bool], None] | None = None,
) -> None:
"""Initialize Asyncio Modbus TCP Client."""
if not hasattr(self,"comm_params"):
self.comm_params = CommParams(
comm_type=CommType.TCP,
host=host,
port=port,
comm_name=name,
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
ModbusBaseClient.__init__(
self,
framer,
retries,
on_connect_callback,
)
def close(self, reconnect: bool = False) -> None:
"""Close connection."""
super().close(reconnect=reconnect)
class ModbusTcpClient(ModbusBaseSyncClient):
"""**ModbusTcpClient**.
Fixed parameters:
:param host: Host IP address or host name
Optional parameters:
:param framer: Framer name, default FramerType.SOCKET
:param port: Port used for communication
:param name: Set communication name, used in logging
:param source_address: source address of client
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import ModbusTcpClient
async def run():
client = ModbusTcpClient("localhost")
client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
Remark: There are no automatic reconnect as with AsyncModbusTcpClient
"""
socket: socket.socket | None
def __init__(
self,
host: str,
framer: FramerType = FramerType.SOCKET,
port: int = 502,
name: str = "comm",
source_address: tuple[str, int] | None = None,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
) -> None:
"""Initialize Modbus TCP Client."""
if not hasattr(self,"comm_params"):
self.comm_params = CommParams(
comm_type=CommType.TCP,
host=host,
port=port,
comm_name=name,
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
super().__init__(framer, retries)
self.socket = None
@property
def connected(self) -> bool:
"""Connect internal."""
return self.socket is not None
def connect(self):
"""Connect to the modbus tcp server."""
if self.socket:
return True
try:
self.socket = socket.create_connection(
(self.comm_params.host, self.comm_params.port),
timeout=self.comm_params.timeout_connect,
source_address=self.comm_params.source_address,
)
Log.debug(
"Connection to Modbus server established. Socket {}",
self.socket.getsockname(),
)
except OSError as msg:
Log.error(
"Connection to ({}, {}) failed: {}",
self.comm_params.host,
self.comm_params.port,
msg,
)
self.close()
return self.socket is not None
def close(self):
"""Close the underlying socket connection."""
if self.socket:
self.socket.close()
self.socket = None
def send(self, request):
"""Send data on the underlying socket."""
super()._start_send()
if not self.socket:
raise ConnectionException(str(self))
if request:
return self.socket.send(request)
return 0
def recv(self, size: int | None) -> bytes:
"""Read data from the underlying descriptor."""
if not self.socket:
raise ConnectionException(str(self))
# socket.recv(size) waits until it gets some data from the host but
# not necessarily the entire response that can be fragmented in
# many packets.
# To avoid split responses to be recognized as invalid
# messages and to be discarded, loops socket.recv until full data
# is received or timeout is expired.
# If timeout expires returns the read data, also if its length is
# less than the expected size.
self.socket.setblocking(False)
timeout = self.comm_params.timeout_connect or 0
# If size isn't specified read up to 4096 bytes at a time.
if size is None:
recv_size = 4096
else:
recv_size = size
data: list[bytes] = []
data_length = 0
time_ = time.time()
end = time_ + timeout
while recv_size > 0:
try:
ready = select.select([self.socket], [], [], end - time_)
except ValueError:
return self._handle_abrupt_socket_close(size, data, time.time() - time_)
if ready[0]:
if (recv_data := self.socket.recv(recv_size)) == b"":
return self._handle_abrupt_socket_close(
size, data, time.time() - time_
)
data.append(recv_data)
data_length += len(recv_data)
time_ = time.time()
# If size isn't specified continue to read until timeout expires.
if size:
recv_size = size - data_length
# Timeout is reduced also if some data has been received in order
# to avoid infinite loops when there isn't an expected response
# size and the slave sends noisy data continuously.
if time_ > end:
break
return b"".join(data)
def _handle_abrupt_socket_close(self, size: int | None, data: list[bytes], duration: float) -> bytes:
"""Handle unexpected socket close by remote end.
Intended to be invoked after determining that the remote end
has unexpectedly closed the connection, to clean up and handle
the situation appropriately.
:param size: The number of bytes that was attempted to read
:param data: The actual data returned
:param duration: Duration from the read was first attempted
until it was determined that the remote closed the
socket
:return: The more than zero bytes read from the remote end
:raises ConnectionException: If the remote end didn't send any
data at all before closing the connection.
"""
self.close()
size_txt = size if size else "unbounded read"
readsize = f"read of {size_txt} bytes"
msg = (
f"{self}: Connection unexpectedly closed "
f"{duration:.3f} seconds into {readsize}"
)
if data:
result = b"".join(data)
Log.warning(" after returning {} bytes: {} ", len(result), result)
return result
msg += " without response from slave before it closed connection"
raise ConnectionException(msg)
def is_socket_open(self) -> bool:
"""Check if socket is open."""
return self.socket is not None
def __repr__(self):
"""Return string representation."""
return (
f"<{self.__class__.__name__} at {hex(id(self))} socket={self.socket}, "
f"ipaddr={self.comm_params.host}, port={self.comm_params.port}, timeout={self.comm_params.timeout_connect}>"
)

View File

@@ -0,0 +1,232 @@
"""Modbus client async TLS communication."""
from __future__ import annotations
import socket
import ssl
from collections.abc import Callable
from pymodbus.client.tcp import AsyncModbusTcpClient, ModbusTcpClient
from pymodbus.framer import FramerType
from pymodbus.logging import Log
from pymodbus.transport import CommParams, CommType
class AsyncModbusTlsClient(AsyncModbusTcpClient):
"""**AsyncModbusTlsClient**.
Fixed parameters:
:param host: Host IP address or host name
Optional parameters:
:param sslctx: SSLContext to use for TLS
:param framer: Framer name, default FramerType.TLS
:param port: Port used for communication
:param name: Set communication name, used in logging
:param source_address: Source address of client
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
:param on_reconnect_callback: Function that will be called just before a reconnection attempt.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import AsyncModbusTlsClient
async def run():
client = AsyncModbusTlsClient("localhost")
await client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
"""
def __init__( # pylint: disable=too-many-arguments
self,
host: str,
sslctx: ssl.SSLContext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT),
framer: FramerType = FramerType.TLS,
port: int = 802,
name: str = "comm",
source_address: tuple[str, int] | None = None,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
on_connect_callback: Callable[[bool], None] | None = None,
):
"""Initialize Asyncio Modbus TLS Client."""
self.comm_params = CommParams(
comm_type=CommType.TLS,
host=host,
sslctx=sslctx,
port=port,
comm_name=name,
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
AsyncModbusTcpClient.__init__(
self,
"",
framer=framer,
retries=retries,
on_connect_callback=on_connect_callback,
)
@classmethod
def generate_ssl(
cls,
certfile: str | None = None,
keyfile: str | None = None,
password: str | None = None,
) -> ssl.SSLContext:
"""Generate sslctx from cert/key/password.
:param certfile: Cert file path for TLS server request
:param keyfile: Key file path for TLS server request
:param password: Password for for decrypting private key file
Remark:
- MODBUS/TCP Security Protocol Specification demands TLSv2 at least
- verify_mode is set to ssl.NONE
"""
return CommParams.generate_ssl(
False, certfile=certfile, keyfile=keyfile, password=password
)
class ModbusTlsClient(ModbusTcpClient):
"""**ModbusTlsClient**.
Fixed parameters:
:param host: Host IP address or host name
Optional parameters:
:param sslctx: SSLContext to use for TLS
:param framer: Framer name, default FramerType.TLS
:param port: Port used for communication
:param name: Set communication name, used in logging
:param source_address: Source address of client
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import ModbusTlsClient
async def run():
client = ModbusTlsClient("localhost")
client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
Remark: There are no automatic reconnect as with AsyncModbusTlsClient
"""
def __init__( # pylint: disable=too-many-arguments
self,
host: str,
sslctx: ssl.SSLContext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT),
framer: FramerType = FramerType.TLS,
port: int = 802,
name: str = "comm",
source_address: tuple[str, int] | None = None,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
):
"""Initialize Modbus TLS Client."""
self.comm_params = CommParams(
comm_type=CommType.TLS,
host=host,
sslctx=sslctx,
port=port,
comm_name=name,
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
super().__init__(
"",
framer=framer,
retries=retries,
)
@classmethod
def generate_ssl(
cls,
certfile: str | None = None,
keyfile: str | None = None,
password: str | None = None,
) -> ssl.SSLContext:
"""Generate sslctx from cert/key/password.
:param certfile: Cert file path for TLS server request
:param keyfile: Key file path for TLS server request
:param password: Password for for decrypting private key file
Remark:
- MODBUS/TCP Security Protocol Specification demands TLSv2 at least
- verify_mode is set to ssl.NONE
"""
return CommParams.generate_ssl(
False, certfile=certfile, keyfile=keyfile, password=password,
)
@property
def connected(self) -> bool:
"""Connect internal."""
return self.transport is not None
def connect(self):
"""Connect to the modbus tls server."""
if self.socket:
return True
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.comm_params.source_address:
sock.bind(self.comm_params.source_address)
self.socket = self.comm_params.sslctx.wrap_socket(sock, server_side=False) # type: ignore[union-attr]
self.socket.settimeout(self.comm_params.timeout_connect)
self.socket.connect((self.comm_params.host, self.comm_params.port))
except OSError as msg:
Log.error(
"Connection to ({}, {}) failed: {}",
self.comm_params.host,
self.comm_params.port,
msg,
)
self.close()
return self.socket is not None
def __repr__(self):
"""Return string representation."""
return (
f"<{self.__class__.__name__} at {hex(id(self))} socket={self.socket}, "
f"ipaddr={self.comm_params.host}, port={self.comm_params.port}, sslctx={self.comm_params.sslctx}, "
f"timeout={self.comm_params.timeout_connect}>"
)

View File

@@ -0,0 +1,225 @@
"""Modbus client async UDP communication."""
from __future__ import annotations
import socket
from collections.abc import Callable
from pymodbus.client.base import ModbusBaseClient, ModbusBaseSyncClient
from pymodbus.exceptions import ConnectionException
from pymodbus.framer import FramerType
from pymodbus.logging import Log
from pymodbus.transport import CommParams, CommType
DGRAM_TYPE = socket.SOCK_DGRAM
class AsyncModbusUdpClient(ModbusBaseClient):
"""**AsyncModbusUdpClient**.
Fixed parameters:
:param host: Host IP address or host name
Optional parameters:
:param framer: Framer name, default FramerType.SOCKET
:param port: Port used for communication.
:param name: Set communication name, used in logging
:param source_address: source address of client,
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
:param on_reconnect_callback: Function that will be called just before a reconnection attempt.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import AsyncModbusUdpClient
async def run():
client = AsyncModbusUdpClient("localhost")
await client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
"""
def __init__( # pylint: disable=too-many-arguments
self,
host: str,
framer: FramerType = FramerType.SOCKET,
port: int = 502,
name: str = "comm",
source_address: tuple[str, int] | None = None,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
on_connect_callback: Callable[[bool], None] | None = None,
) -> None:
"""Initialize Asyncio Modbus UDP Client."""
self.comm_params = CommParams(
comm_type=CommType.UDP,
host=host,
port=port,
comm_name=name,
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
ModbusBaseClient.__init__(
self,
framer,
retries,
on_connect_callback,
)
self.source_address = source_address
@property
def connected(self):
"""Return true if connected."""
return self.ctx.is_active()
class ModbusUdpClient(ModbusBaseSyncClient):
"""**ModbusUdpClient**.
Fixed parameters:
:param host: Host IP address or host name
Optional parameters:
:param framer: Framer name, default FramerType.SOCKET
:param port: Port used for communication.
:param name: Set communication name, used in logging
:param source_address: source address of client,
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param timeout: Timeout for a connection request, in seconds.
:param retries: Max number of retries per request.
.. tip::
**reconnect_delay** doubles automatically with each unsuccessful connect, from
**reconnect_delay** to **reconnect_delay_max**.
Set `reconnect_delay=0` to avoid automatic reconnection.
Example::
from pymodbus.client import ModbusUdpClient
async def run():
client = ModbusUdpClient("localhost")
client.connect()
...
client.close()
Please refer to :ref:`Pymodbus internals` for advanced usage.
Remark: There are no automatic reconnect as with AsyncModbusUdpClient
"""
socket: socket.socket | None
def __init__(
self,
host: str,
framer: FramerType = FramerType.SOCKET,
port: int = 502,
name: str = "comm",
source_address: tuple[str, int] | None = None,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
timeout: float = 3,
retries: int = 3,
) -> None:
"""Initialize Modbus UDP Client."""
self.comm_params = CommParams(
comm_type=CommType.UDP,
host=host,
port=port,
comm_name=name,
source_address=source_address,
reconnect_delay=reconnect_delay,
reconnect_delay_max=reconnect_delay_max,
timeout_connect=timeout,
)
super().__init__(framer, retries)
self.socket = None
@property
def connected(self) -> bool:
"""Connect internal."""
return self.socket is not None
def connect(self):
"""Connect to the modbus tcp server.
:meta private:
"""
if self.socket:
return True
try:
family = ModbusUdpClient.get_address_family(self.comm_params.host)
self.socket = socket.socket(family, socket.SOCK_DGRAM)
self.socket.settimeout(self.comm_params.timeout_connect)
except OSError as exc:
Log.error("Unable to create udp socket {}", exc)
self.close()
return self.socket is not None
def close(self):
"""Close the underlying socket connection.
:meta private:
"""
self.socket = None
def send(self, request: bytes) -> int:
"""Send data on the underlying socket.
:meta private:
"""
super()._start_send()
if not self.socket:
raise ConnectionException(str(self))
if request:
return self.socket.sendto(
request, (self.comm_params.host, self.comm_params.port)
)
return 0
def recv(self, size: int | None) -> bytes:
"""Read data from the underlying descriptor.
:meta private:
"""
if not self.socket:
raise ConnectionException(str(self))
if size is None:
size = 0
return self.socket.recvfrom(size)[0]
def is_socket_open(self):
"""Check if socket is open.
:meta private:
"""
return True
def __repr__(self):
"""Return string representation."""
return (
f"<{self.__class__.__name__} at {hex(id(self))} socket={self.socket}, "
f"ipaddr={self.comm_params.host}, port={self.comm_params.port}, timeout={self.comm_params.timeout_connect}>"
)

View File

@@ -0,0 +1,144 @@
"""Constants For Modbus Server/Client.
This is the single location for storing default
values for the servers and clients.
"""
import enum
INTERNAL_ERROR = "Pymodbus internal error"
class ModbusStatus(int, enum.Enum):
"""These represent various status codes in the modbus protocol.
.. attribute:: WAITING
This indicates that a modbus device is currently
waiting for a given request to finish some running task.
.. attribute:: READY
This indicates that a modbus device is currently
free to perform the next request task.
.. attribute:: ON
This indicates that the given modbus entity is on
.. attribute:: OFF
This indicates that the given modbus entity is off
.. attribute:: SLAVE_ON
This indicates that the given modbus slave is running
.. attribute:: SLAVE_OFF
This indicates that the given modbus slave is not running
"""
WAITING = 0xFFFF
READY = 0x0000
ON = 0xFF00
OFF = 0x0000
SLAVE_ON = 0xFF
SLAVE_OFF = 0x00
class Endian(str, enum.Enum):
"""An enumeration representing the various byte endianness.
.. attribute:: AUTO
This indicates that the byte order is chosen by the
current native environment.
.. attribute:: BIG
This indicates that the bytes are in big endian format
.. attribute:: LITTLE
This indicates that the bytes are in little endian format
.. note:: I am simply borrowing the format strings from the
python struct module for my convenience.
"""
AUTO = "@"
BIG = ">"
LITTLE = "<"
class ModbusPlusOperation(int, enum.Enum):
"""Represents the type of modbus plus request.
.. attribute:: GET_STATISTICS
Operation requesting that the current modbus plus statistics
be returned in the response.
.. attribute:: CLEAR_STATISTICS
Operation requesting that the current modbus plus statistics
be cleared and not returned in the response.
"""
GET_STATISTICS = 0x0003
CLEAR_STATISTICS = 0x0004
class DeviceInformation(int, enum.Enum):
"""Represents what type of device information to read.
.. attribute:: BASIC
This is the basic (required) device information to be returned.
This includes VendorName, ProductCode, and MajorMinorRevision
code.
.. attribute:: REGULAR
In addition to basic data objects, the device provides additional
and optional identification and description data objects. All of
the objects of this category are defined in the standard but their
implementation is optional.
.. attribute:: EXTENDED
In addition to regular data objects, the device provides additional
and optional identification and description private data about the
physical device itself. All of these data are device dependent.
.. attribute:: SPECIFIC
Request to return a single data object.
"""
BASIC = 0x01
REGULAR = 0x02
EXTENDED = 0x03
SPECIFIC = 0x04
def __str__(self):
"""Override to force int representation for enum members."""
return str(int(self))
class MoreData(int, enum.Enum):
"""Represents the more follows condition.
.. attribute:: NOTHING
This indicates that no more objects are going to be returned.
.. attribute:: KEEP_READING
This indicates that there are more objects to be returned.
"""
NOTHING = 0x00
KEEP_READING = 0xFF

View File

@@ -0,0 +1,21 @@
"""Datastore."""
__all__ = [
"ModbusBaseSlaveContext",
"ModbusSequentialDataBlock",
"ModbusSparseDataBlock",
"ModbusSlaveContext",
"ModbusServerContext",
"ModbusSimulatorContext",
]
from pymodbus.datastore.context import (
ModbusBaseSlaveContext,
ModbusServerContext,
ModbusSlaveContext,
)
from pymodbus.datastore.simulator import ModbusSimulatorContext
from pymodbus.datastore.store import (
ModbusSequentialDataBlock,
ModbusSparseDataBlock,
)

View File

@@ -0,0 +1,250 @@
"""Context for datastore."""
from __future__ import annotations
# pylint: disable=missing-type-doc
from pymodbus.datastore.store import ModbusSequentialDataBlock
from pymodbus.exceptions import NoSuchSlaveException
from pymodbus.logging import Log
class ModbusBaseSlaveContext:
"""Interface for a modbus slave data context.
Derived classes must implemented the following methods:
reset(self)
validate(self, fx, address, count=1)
getValues/async_getValues(self, fc_as_hex, address, count=1)
setValues/async_setValues(self, fc_as_hex, address, values)
"""
_fx_mapper = {2: "d", 4: "i"}
_fx_mapper.update([(i, "h") for i in (3, 6, 16, 22, 23)])
_fx_mapper.update([(i, "c") for i in (1, 5, 15)])
def decode(self, fx):
"""Convert the function code to the datastore to.
:param fx: The function we are working with
:returns: one of [d(iscretes),i(nputs),h(olding),c(oils)
"""
return self._fx_mapper[fx]
async def async_getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool | None]:
"""Get `count` values from datastore.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param count: The number of values to retrieve
:returns: The requested values from a:a+c
"""
return self.getValues(fc_as_hex, address, count)
async def async_setValues(self, fc_as_hex: int, address: int, values: list[int | bool]) -> None:
"""Set the datastore with the supplied values.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param values: The new values to be set
"""
self.setValues(fc_as_hex, address, values)
def getValues(self, fc_as_hex: int, address: int, count: int = 1) -> list[int | bool | None]:
"""Get `count` values from datastore.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param count: The number of values to retrieve
:returns: The requested values from a:a+c
"""
Log.error("getValues({},{},{}) not implemented!", fc_as_hex, address, count)
return []
def setValues(self, fc_as_hex: int, address: int, values: list[int | bool]) -> None:
"""Set the datastore with the supplied values.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param values: The new values to be set
"""
# ---------------------------------------------------------------------------#
# Slave Contexts
# ---------------------------------------------------------------------------#
class ModbusSlaveContext(ModbusBaseSlaveContext):
"""Create a modbus data model with data stored in a block.
:param di: discrete inputs initializer ModbusDataBlock
:param co: coils initializer ModbusDataBlock
:param hr: holding register initializer ModbusDataBlock
:param ir: input registers initializer ModbusDataBlock
:param zero_mode: Not add one to address
When True, a request for address zero to n will map to
datastore address zero to n.
When False, a request for address zero to n will map to
datastore address one to n+1, based on section 4.4 of
specification.
Default is False.
"""
def __init__(self, *_args,
di=ModbusSequentialDataBlock.create(),
co=ModbusSequentialDataBlock.create(),
ir=ModbusSequentialDataBlock.create(),
hr=ModbusSequentialDataBlock.create(),
zero_mode=False):
"""Initialize the datastores."""
self.store = {}
self.store["d"] = di
self.store["c"] = co
self.store["i"] = ir
self.store["h"] = hr
self.zero_mode = zero_mode
def __str__(self):
"""Return a string representation of the context.
:returns: A string representation of the context
"""
return "Modbus Slave Context"
def reset(self):
"""Reset all the datastores to their default values."""
for datastore in iter(self.store.values()):
datastore.reset()
def validate(self, fc_as_hex, address, count=1):
"""Validate the request to make sure it is in range.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param count: The number of values to test
:returns: True if the request in within range, False otherwise
"""
if not self.zero_mode:
address += 1
Log.debug("validate: fc-[{}] address-{}: count-{}", fc_as_hex, address, count)
return self.store[self.decode(fc_as_hex)].validate(address, count)
def getValues(self, fc_as_hex, address, count=1):
"""Get `count` values from datastore.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param count: The number of values to retrieve
:returns: The requested values from a:a+c
"""
if not self.zero_mode:
address += 1
Log.debug("getValues: fc-[{}] address-{}: count-{}", fc_as_hex, address, count)
return self.store[self.decode(fc_as_hex)].getValues(address, count)
def setValues(self, fc_as_hex, address, values):
"""Set the datastore with the supplied values.
:param fc_as_hex: The function we are working with
:param address: The starting address
:param values: The new values to be set
"""
if not self.zero_mode:
address += 1
Log.debug("setValues[{}] address-{}: count-{}", fc_as_hex, address, len(values))
self.store[self.decode(fc_as_hex)].setValues(address, values)
def register(self, function_code, fc_as_hex, datablock=None):
"""Register a datablock with the slave context.
:param function_code: function code (int)
:param fc_as_hex: string representation of function code (e.g "cf" )
:param datablock: datablock to associate with this function code
"""
self.store[fc_as_hex] = datablock or ModbusSequentialDataBlock.create()
self._fx_mapper[function_code] = fc_as_hex
class ModbusServerContext:
"""This represents a master collection of slave contexts.
If single is set to true, it will be treated as a single
context so every slave_id returns the same context. If single
is set to false, it will be interpreted as a collection of
slave contexts.
"""
def __init__(self, slaves=None, single=True):
"""Initialize a new instance of a modbus server context.
:param slaves: A dictionary of client contexts
:param single: Set to true to treat this as a single context
"""
self.single = single
self._slaves = slaves or {}
if self.single:
self._slaves = {0: self._slaves}
def __iter__(self):
"""Iterate over the current collection of slave contexts.
:returns: An iterator over the slave contexts
"""
return iter(self._slaves.items())
def __contains__(self, slave):
"""Check if the given slave is in this list.
:param slave: slave The slave to check for existence
:returns: True if the slave exists, False otherwise
"""
if self.single and self._slaves:
return True
return slave in self._slaves
def __setitem__(self, slave, context):
"""Use to set a new slave context.
:param slave: The slave context to set
:param context: The new context to set for this slave
:raises NoSuchSlaveException:
"""
if self.single:
slave = 0
if 0xF7 >= slave >= 0x00:
self._slaves[slave] = context
else:
raise NoSuchSlaveException(f"slave index :{slave} out of range")
def __delitem__(self, slave):
"""Use to access the slave context.
:param slave: The slave context to remove
:raises NoSuchSlaveException:
"""
if not self.single and (0xF7 >= slave >= 0x00):
del self._slaves[slave]
else:
raise NoSuchSlaveException(f"slave index: {slave} out of range")
def __getitem__(self, slave):
"""Use to get access to a slave context.
:param slave: The slave context to get
:returns: The requested slave context
:raises NoSuchSlaveException:
"""
if self.single:
slave = 0
if slave in self._slaves:
return self._slaves.get(slave)
raise NoSuchSlaveException(
f"slave - {slave} does not exist, or is out of range"
)
def slaves(self):
"""Define slaves."""
# Python3 now returns keys() as iterable
return list(self._slaves.keys())

View File

@@ -0,0 +1,129 @@
"""Remote datastore."""
from pymodbus.datastore import ModbusBaseSlaveContext
from pymodbus.exceptions import NotImplementedException
from pymodbus.logging import Log
# ---------------------------------------------------------------------------#
# Context
# ---------------------------------------------------------------------------#
class RemoteSlaveContext(ModbusBaseSlaveContext):
"""TODO.
This creates a modbus data model that connects to
a remote device (depending on the client used)
"""
def __init__(self, client, slave=None):
"""Initialize the datastores.
:param client: The client to retrieve values with
:param slave: Unit ID of the remote slave
"""
self._client = client
self.slave = slave
self.result = None
self.__build_mapping()
if not self.__set_callbacks:
Log.error("Init went wrong.")
def reset(self):
"""Reset all the datastores to their default values."""
raise NotImplementedException()
def validate(self, _fc_as_hex, _address, _count):
"""Validate the request to make sure it is in range.
:returns: True
"""
return True
def getValues(self, fc_as_hex, _address, _count=1):
"""Get values from real call in validate."""
if fc_as_hex in self._write_fc:
return [0]
group_fx = self.decode(fc_as_hex)
func_fc = self.__get_callbacks[group_fx]
self.result = func_fc(_address, _count)
return self.__extract_result(self.decode(fc_as_hex), self.result)
def setValues(self, fc_as_hex, address, values):
"""Set the datastore with the supplied values."""
group_fx = self.decode(fc_as_hex)
if fc_as_hex not in self._write_fc:
raise ValueError(f"setValues() called with an non-write function code {fc_as_hex}")
func_fc = self.__set_callbacks[f"{group_fx}{fc_as_hex}"]
if fc_as_hex in {0x0F, 0x10}: # Write Multiple Coils, Write Multiple Registers
self.result = func_fc(address, values)
else:
self.result = func_fc(address, values[0])
# if self.result.isError():
# return self.result
def __str__(self):
"""Return a string representation of the context.
:returns: A string representation of the context
"""
return f"Remote Slave Context({self._client})"
def __build_mapping(self):
"""Build the function code mapper."""
params = {}
if self.slave:
params["slave"] = self.slave
self.__get_callbacks = {
"d": lambda a, c: self._client.read_discrete_inputs(
a, c, **params
),
"c": lambda a, c: self._client.read_coils(
a, c, **params
),
"h": lambda a, c: self._client.read_holding_registers(
a, c, **params
),
"i": lambda a, c: self._client.read_input_registers(
a, c, **params
),
}
self.__set_callbacks = {
"d5": lambda a, v: self._client.write_coil(
a, v, **params
),
"d15": lambda a, v: self._client.write_coils(
a, v, **params
),
"c5": lambda a, v: self._client.write_coil(
a, v, **params
),
"c15": lambda a, v: self._client.write_coils(
a, v, **params
),
"h6": lambda a, v: self._client.write_register(
a, v, **params
),
"h16": lambda a, v: self._client.write_registers(
a, v, **params
),
"i6": lambda a, v: self._client.write_register(
a, v, **params
),
"i16": lambda a, v: self._client.write_registers(
a, v, **params
),
}
self._write_fc = (0x05, 0x06, 0x0F, 0x10)
def __extract_result(self, fc_as_hex, result):
"""Extract the values out of a response.
TODO make this consistent (values?)
"""
if not result.isError():
if fc_as_hex in {"d", "c"}:
return result.bits
if fc_as_hex in {"h", "i"}:
return result.registers
else:
return result
return None

View File

@@ -0,0 +1,803 @@
"""Pymodbus ModbusSimulatorContext."""
from __future__ import annotations
import dataclasses
import random
import struct
from collections.abc import Callable
from datetime import datetime
from typing import Any
from pymodbus.datastore.context import ModbusBaseSlaveContext
WORD_SIZE = 16
@dataclasses.dataclass(frozen=True)
class CellType:
"""Define single cell types."""
INVALID: int = 0
BITS: int = 1
UINT16: int = 2
UINT32: int = 3
FLOAT32: int = 4
STRING: int = 5
NEXT: int = 6
@dataclasses.dataclass(repr=False, eq=False)
class Cell:
"""Handle a single cell."""
type: int = CellType.INVALID
access: bool = False
value: int = 0
action: int = 0
action_parameters: dict[str, Any] | None = None
count_read: int = 0
count_write: int = 0
class TextCell: # pylint: disable=too-few-public-methods
"""A textual representation of a single cell."""
type: str
access: str
value: str
action: str
action_parameters: str
count_read: str
count_write: str
@dataclasses.dataclass
class Label: # pylint: disable=too-many-instance-attributes
"""Defines all dict values.
:meta private:
"""
action: str = "action"
addr: str = "addr"
any: str = "any"
co_size: str = "co size"
defaults: str = "defaults"
di_size: str = "di size"
hr_size: str = "hr size"
increment: str = "increment"
invalid: str = "invalid"
ir_size: str = "ir size"
parameters: str = "parameters"
method: str = "method"
next: str = "next"
none: str = "none"
random: str = "random"
repeat: str = "repeat"
reset: str = "reset"
setup: str = "setup"
shared_blocks: str = "shared blocks"
timestamp: str = "timestamp"
repeat_to: str = "to"
type: str = "type"
type_bits = "bits"
type_exception: str = "type exception"
type_uint16: str = "uint16"
type_uint32: str = "uint32"
type_float32: str = "float32"
type_string: str = "string"
uptime: str = "uptime"
value: str = "value"
write: str = "write"
@classmethod
def try_get(cls, key, config_part):
"""Check if entry is present in config."""
if key not in config_part:
txt = f"ERROR Configuration invalid, missing {key} in {config_part}"
raise RuntimeError(txt)
return config_part[key]
class Setup:
"""Setup simulator.
:meta private:
"""
def __init__(self, runtime):
"""Initialize."""
self.runtime = runtime
self.config = {}
self.config_types: dict[str, dict[str, Any]] = {
Label.type_bits: {
Label.type: CellType.BITS,
Label.next: None,
Label.value: 0,
Label.action: None,
Label.method: self.handle_type_bits,
},
Label.type_uint16: {
Label.type: CellType.UINT16,
Label.next: None,
Label.value: 0,
Label.action: None,
Label.method: self.handle_type_uint16,
},
Label.type_uint32: {
Label.type: CellType.UINT32,
Label.next: CellType.NEXT,
Label.value: 0,
Label.action: None,
Label.method: self.handle_type_uint32,
},
Label.type_float32: {
Label.type: CellType.FLOAT32,
Label.next: CellType.NEXT,
Label.value: 0,
Label.action: None,
Label.method: self.handle_type_float32,
},
Label.type_string: {
Label.type: CellType.STRING,
Label.next: CellType.NEXT,
Label.value: 0,
Label.action: None,
Label.method: self.handle_type_string,
},
}
def handle_type_bits(self, start, stop, value, action, action_parameters):
"""Handle type bits."""
for reg in self.runtime.registers[start:stop]:
if reg.type != CellType.INVALID:
raise RuntimeError(f'ERROR "{Label.type_bits}" {reg} used')
reg.value = value
reg.type = CellType.BITS
reg.action = action
reg.action_parameters = action_parameters
def handle_type_uint16(self, start, stop, value, action, action_parameters):
"""Handle type uint16."""
for reg in self.runtime.registers[start:stop]:
if reg.type != CellType.INVALID:
raise RuntimeError(f'ERROR "{Label.type_uint16}" {reg} used')
reg.value = value
reg.type = CellType.UINT16
reg.action = action
reg.action_parameters = action_parameters
def handle_type_uint32(self, start, stop, value, action, action_parameters):
"""Handle type uint32."""
regs_value = ModbusSimulatorContext.build_registers_from_value(value, True)
for i in range(start, stop, 2):
regs = self.runtime.registers[i : i + 2]
if regs[0].type != CellType.INVALID or regs[1].type != CellType.INVALID:
raise RuntimeError(f'ERROR "{Label.type_uint32}" {i},{i + 1} used')
regs[0].value = regs_value[0]
regs[0].type = CellType.UINT32
regs[0].action = action
regs[0].action_parameters = action_parameters
regs[1].value = regs_value[1]
regs[1].type = CellType.NEXT
def handle_type_float32(self, start, stop, value, action, action_parameters):
"""Handle type uint32."""
regs_value = ModbusSimulatorContext.build_registers_from_value(value, False)
for i in range(start, stop, 2):
regs = self.runtime.registers[i : i + 2]
if regs[0].type != CellType.INVALID or regs[1].type != CellType.INVALID:
raise RuntimeError(f'ERROR "{Label.type_float32}" {i},{i + 1} used')
regs[0].value = regs_value[0]
regs[0].type = CellType.FLOAT32
regs[0].action = action
regs[0].action_parameters = action_parameters
regs[1].value = regs_value[1]
regs[1].type = CellType.NEXT
def handle_type_string(self, start, stop, value, action, action_parameters):
"""Handle type string."""
regs = stop - start
reg_len = regs * 2
if len(value) > reg_len:
raise RuntimeError(
f'ERROR "{Label.type_string}" {start} too long "{value}"'
)
value = value.ljust(reg_len)
for i in range(stop - start):
reg = self.runtime.registers[start + i]
if reg.type != CellType.INVALID:
raise RuntimeError(f'ERROR "{Label.type_string}" {start + i} used')
j = i * 2
reg.value = int.from_bytes(bytes(value[j : j + 2], "UTF-8"), "big")
reg.type = CellType.NEXT
self.runtime.registers[start].type = CellType.STRING
self.runtime.registers[start].action = action
self.runtime.registers[start].action_parameters = action_parameters
def handle_setup_section(self):
"""Load setup section."""
layout = Label.try_get(Label.setup, self.config)
self.runtime.fc_offset = {key: 0 for key in range(25)}
size_co = Label.try_get(Label.co_size, layout)
size_di = Label.try_get(Label.di_size, layout)
size_hr = Label.try_get(Label.hr_size, layout)
size_ir = Label.try_get(Label.ir_size, layout)
if Label.try_get(Label.shared_blocks, layout):
total_size = max(size_co, size_di, size_hr, size_ir)
else:
# set offset (block) for each function code
# starting with fc = 1, 5, 15
self.runtime.fc_offset[2] = size_co
total_size = size_co + size_di
self.runtime.fc_offset[4] = total_size
total_size += size_ir
for i in (3, 6, 16, 22, 23):
self.runtime.fc_offset[i] = total_size
total_size += size_hr
first_cell = Cell()
self.runtime.registers = [
dataclasses.replace(first_cell) for i in range(total_size)
]
self.runtime.register_count = total_size
self.runtime.type_exception = bool(Label.try_get(Label.type_exception, layout))
defaults = Label.try_get(Label.defaults, layout)
defaults_value = Label.try_get(Label.value, defaults)
defaults_action = Label.try_get(Label.action, defaults)
for key, entry in self.config_types.items():
entry[Label.value] = Label.try_get(key, defaults_value)
if (
action := Label.try_get(key, defaults_action)
) not in self.runtime.action_name_to_id:
raise RuntimeError(f"ERROR illegal action {key} in {defaults_action}")
entry[Label.action] = action
del self.config[Label.setup]
def handle_invalid_address(self):
"""Handle invalid address."""
for entry in Label.try_get(Label.invalid, self.config):
if isinstance(entry, int):
entry = [entry, entry]
for i in range(entry[0], entry[1] + 1):
if i >= self.runtime.register_count:
raise RuntimeError(
f'Error section "{Label.invalid}" addr {entry} out of range'
)
reg = self.runtime.registers[i]
reg.type = CellType.INVALID
del self.config[Label.invalid]
def handle_write_allowed(self):
"""Handle write allowed."""
for entry in Label.try_get(Label.write, self.config):
if isinstance(entry, int):
entry = [entry, entry]
for i in range(entry[0], entry[1] + 1):
if i >= self.runtime.register_count:
raise RuntimeError(
f'Error section "{Label.write}" addr {entry} out of range'
)
reg = self.runtime.registers[i]
if reg.type == CellType.INVALID:
txt = f'ERROR Configuration invalid in section "write" register {i} not defined'
raise RuntimeError(txt)
reg.access = True
del self.config[Label.write]
def handle_types(self):
"""Handle the different types."""
for section, type_entry in self.config_types.items():
layout = Label.try_get(section, self.config)
for entry in layout:
if not isinstance(entry, dict):
entry = {Label.addr: entry}
regs = Label.try_get(Label.addr, entry)
if not isinstance(regs, list):
regs = [regs, regs]
start = regs[0]
if (stop := regs[1]) >= self.runtime.register_count:
raise RuntimeError(f'Error "{section}" {start}, {stop} illegal')
type_entry[Label.method](
start,
stop + 1,
entry.get(Label.value, type_entry[Label.value]),
self.runtime.action_name_to_id[
entry.get(Label.action, type_entry[Label.action])
],
entry.get(Label.parameters, None),
)
del self.config[section]
def handle_repeat(self):
"""Handle repeat."""
for entry in Label.try_get(Label.repeat, self.config):
addr = Label.try_get(Label.addr, entry)
copy_start = addr[0]
copy_end = addr[1]
copy_inx = copy_start - 1
addr_to = Label.try_get(Label.repeat_to, entry)
for inx in range(addr_to[0], addr_to[1] + 1):
copy_inx = copy_start if copy_inx >= copy_end else copy_inx + 1
if inx >= self.runtime.register_count:
raise RuntimeError(
f'Error section "{Label.repeat}" entry {entry} out of range'
)
self.runtime.registers[inx] = dataclasses.replace(
self.runtime.registers[copy_inx]
)
del self.config[Label.repeat]
def setup(self, config, custom_actions) -> None:
"""Load layout from dict with json structure."""
actions = {
Label.increment: self.runtime.action_increment,
Label.random: self.runtime.action_random,
Label.reset: self.runtime.action_reset,
Label.timestamp: self.runtime.action_timestamp,
Label.uptime: self.runtime.action_uptime,
}
if custom_actions:
actions.update(custom_actions)
self.runtime.action_name_to_id = {None: 0}
self.runtime.action_id_to_name = [Label.none]
self.runtime.action_methods = [None]
i = 1
for key, method in actions.items():
self.runtime.action_name_to_id[key] = i
self.runtime.action_id_to_name.append(key)
self.runtime.action_methods.append(method)
i += 1
self.runtime.registerType_name_to_id = {
Label.type_bits: CellType.BITS,
Label.type_uint16: CellType.UINT16,
Label.type_uint32: CellType.UINT32,
Label.type_float32: CellType.FLOAT32,
Label.type_string: CellType.STRING,
Label.next: CellType.NEXT,
Label.invalid: CellType.INVALID,
}
self.runtime.registerType_id_to_name = [None] * len(
self.runtime.registerType_name_to_id
)
for name, cell_type in self.runtime.registerType_name_to_id.items():
self.runtime.registerType_id_to_name[cell_type] = name
self.config = config
self.handle_setup_section()
self.handle_invalid_address()
self.handle_types()
self.handle_write_allowed()
self.handle_repeat()
if self.config:
raise RuntimeError(f"INVALID key in setup: {self.config}")
class ModbusSimulatorContext(ModbusBaseSlaveContext):
"""Modbus simulator.
:param config: A dict with structure as shown below.
:param actions: A dict with "<name>": <function> structure.
:raises RuntimeError: if json contains errors (msg explains what)
It builds and maintains a virtual copy of a device, with simulation of
device specific functions.
The device is described in a dict, user supplied actions will
be added to the builtin actions.
It is used in conjunction with a pymodbus server.
Example::
store = ModbusSimulatorContext(<config dict>, <actions dict>)
StartAsyncTcpServer(<host>, context=store)
Now the server will simulate the defined device with features like:
- invalid addresses
- write protected addresses
- optional control of access for string, uint32, bit/bits
- builtin actions for e.g. reset/datetime, value increment by read
- custom actions
Description of the json file or dict to be supplied::
{
"setup": {
"di size": 0, --> Size of discrete input block (8 bit)
"co size": 0, --> Size of coils block (8 bit)
"ir size": 0, --> Size of input registers block (16 bit)
"hr size": 0, --> Size of holding registers block (16 bit)
"shared blocks": True, --> share memory for all blocks (largest size wins)
"defaults": {
"value": { --> Initial values (can be overwritten)
"bits": 0x01,
"uint16": 122,
"uint32": 67000,
"float32": 127.4,
"string": " ",
},
"action": { --> default action (can be overwritten)
"bits": None,
"uint16": None,
"uint32": None,
"float32": None,
"string": None,
},
},
"type exception": False, --> return IO exception if read/write on non boundary
},
"invalid": [ --> List of invalid addresses, IO exception returned
51, --> single register
[78, 99], --> start, end registers, repeated as needed
],
"write": [ --> allow write, efault is ReadOnly
[5, 5] --> start, end bytes, repeated as needed
],
"bits": [ --> Define bits (1 register == 2 bytes)
[30, 31], --> start, end registers, repeated as needed
{"addr": [32, 34], "value": 0xF1}, --> with value
{"addr": [35, 36], "action": "increment"}, --> with action
{"addr": [37, 38], "action": "increment", "value": 0xF1} --> with action and value
{"addr": [37, 38], "action": "increment", "parameters": {"min": 0, "max": 100}} --> with action with arguments
],
"uint16": [ --> Define uint16 (1 register == 2 bytes)
--> same as type_bits
],
"uint32": [ --> Define 32 bit integers (2 registers == 4 bytes)
--> same as type_bits
],
"float32": [ --> Define 32 bit floats (2 registers == 4 bytes)
--> same as type_bits
],
"string": [ --> Define strings (variable number of registers (each 2 bytes))
[21, 22], --> start, end registers, define 1 string
{"addr": 23, 25], "value": "ups"}, --> with value
{"addr": 26, 27], "action": "user"}, --> with action
{"addr": 28, 29], "action": "", "value": "user"} --> with action and value
],
"repeat": [ --> allows to repeat section e.g. for n devices
{"addr": [100, 200], "to": [50, 275]} --> Repeat registers 100-200 to 50+ until 275
]
}
"""
# --------------------------------------------
# External interfaces
# --------------------------------------------
start_time = int(datetime.now().timestamp())
def __init__(
self, config: dict[str, Any], custom_actions: dict[str, Callable] | None
) -> None:
"""Initialize."""
self.registers: list[Cell] = []
self.fc_offset: dict[int, int] = {}
self.register_count = 0
self.type_exception = False
self.action_name_to_id: dict[str, int] = {}
self.action_id_to_name: list[str] = []
self.action_methods: list[Callable] = []
self.registerType_name_to_id: dict[str, int] = {}
self.registerType_id_to_name: list[str] = []
Setup(self).setup(config, custom_actions)
# --------------------------------------------
# Simulator server interface
# --------------------------------------------
def get_text_register(self, register):
"""Get raw register."""
reg = self.registers[register]
text_cell = TextCell()
text_cell.type = self.registerType_id_to_name[reg.type]
text_cell.access = str(reg.access)
text_cell.count_read = str(reg.count_read)
text_cell.count_write = str(reg.count_write)
text_cell.action = self.action_id_to_name[reg.action]
if reg.action_parameters:
text_cell.action = f"{text_cell.action}({reg.action_parameters})"
if reg.type in (CellType.INVALID, CellType.UINT16, CellType.NEXT):
text_cell.value = str(reg.value)
build_len = 0
elif reg.type == CellType.BITS:
text_cell.value = hex(reg.value)
build_len = 0
elif reg.type == CellType.UINT32:
tmp_regs = [reg.value, self.registers[register + 1].value]
text_cell.value = str(self.build_value_from_registers(tmp_regs, True))
build_len = 1
elif reg.type == CellType.FLOAT32:
tmp_regs = [reg.value, self.registers[register + 1].value]
text_cell.value = str(self.build_value_from_registers(tmp_regs, False))
build_len = 1
else: # reg.type == CellType.STRING:
j = register
text_cell.value = ""
while True:
text_cell.value += str(
self.registers[j].value.to_bytes(2, "big"),
encoding="utf-8",
errors="ignore",
)
j += 1
if self.registers[j].type != CellType.NEXT:
break
build_len = j - register - 1
reg_txt = f"{register}-{register + build_len}" if build_len else f"{register}"
return reg_txt, text_cell
# --------------------------------------------
# Modbus server interface
# --------------------------------------------
_write_func_code = (5, 6, 15, 16, 22, 23)
_bits_func_code = (1, 2, 5, 15)
def loop_validate(self, address, end_address, fx_write):
"""Validate entry in loop.
:meta private:
"""
i = address
while i < end_address:
reg = self.registers[i]
if fx_write and not reg.access or reg.type == CellType.INVALID:
return False
if not self.type_exception:
i += 1
continue
if reg.type == CellType.NEXT:
return False
if reg.type in (CellType.BITS, CellType.UINT16):
i += 1
elif reg.type in (CellType.UINT32, CellType.FLOAT32):
if i + 1 >= end_address:
return False
i += 2
else:
i += 1
while i < end_address:
if self.registers[i].type == CellType.NEXT:
i += 1
return True
def validate(self, func_code, address, count=1):
"""Check to see if the request is in range.
:meta private:
"""
if func_code in self._bits_func_code:
# Bit count, correct to register count
count = int((count + WORD_SIZE - 1) / WORD_SIZE)
address = int(address / 16)
real_address = self.fc_offset[func_code] + address
if real_address < 0 or real_address > self.register_count:
return False
fx_write = func_code in self._write_func_code
return self.loop_validate(real_address, real_address + count, fx_write)
def getValues(self, func_code, address, count=1):
"""Return the requested values of the datastore.
:meta private:
"""
result = []
if func_code not in self._bits_func_code:
real_address = self.fc_offset[func_code] + address
for i in range(real_address, real_address + count):
reg = self.registers[i]
parameters = reg.action_parameters if reg.action_parameters else {}
if reg.action:
self.action_methods[reg.action](self.registers, i, reg, **parameters)
self.registers[i].count_read += 1
result.append(reg.value)
else:
# bit access
real_address = self.fc_offset[func_code] + int(address / 16)
bit_index = address % 16
reg_count = int((count + bit_index + 15) / 16)
for i in range(real_address, real_address + reg_count):
reg = self.registers[i]
if reg.action:
parameters = reg.action_parameters or {}
self.action_methods[reg.action](
self.registers, i, reg, **parameters
)
self.registers[i].count_read += 1
while count and bit_index < 16:
result.append(bool(reg.value & (2**bit_index)))
count -= 1
bit_index += 1
bit_index = 0
return result
def setValues(self, func_code, address, values):
"""Set the requested values of the datastore.
:meta private:
"""
if func_code not in self._bits_func_code:
real_address = self.fc_offset[func_code] + address
for value in values:
self.registers[real_address].value = value
self.registers[real_address].count_write += 1
real_address += 1
return
# bit access
real_address = self.fc_offset[func_code] + int(address / 16)
bit_index = address % 16
for value in values:
bit_mask = 2**bit_index
if bool(value):
self.registers[real_address].value |= bit_mask
else:
self.registers[real_address].value &= ~bit_mask
self.registers[real_address].count_write += 1
bit_index += 1
if bit_index == 16:
bit_index = 0
real_address += 1
return
# --------------------------------------------
# Internal action methods
# --------------------------------------------
@classmethod
def action_random(cls, registers, inx, cell, minval=1, maxval=65536):
"""Update with random value.
:meta private:
"""
if cell.type in (CellType.BITS, CellType.UINT16):
registers[inx].value = random.randint(int(minval), int(maxval))
elif cell.type == CellType.FLOAT32:
regs = cls.build_registers_from_value(
random.uniform(float(minval), float(maxval)), False
)
registers[inx].value = regs[0]
registers[inx + 1].value = regs[1]
elif cell.type == CellType.UINT32:
regs = cls.build_registers_from_value(
random.randint(int(minval), int(maxval)), True
)
registers[inx].value = regs[0]
registers[inx + 1].value = regs[1]
@classmethod
def action_increment(cls, registers, inx, cell, minval=None, maxval=None):
"""Increment value reset with overflow.
:meta private:
"""
reg = registers[inx]
reg2 = registers[inx + 1]
if cell.type in (CellType.BITS, CellType.UINT16):
value = reg.value + 1
if maxval and value > maxval:
value = minval
if minval and value < minval:
value = minval
reg.value = value
elif cell.type == CellType.FLOAT32:
tmp_reg = [reg.value, reg2.value]
value = cls.build_value_from_registers(tmp_reg, False)
value += 1.0
if maxval and value > maxval:
value = minval
if minval and value < minval:
value = minval
new_regs = cls.build_registers_from_value(value, False)
reg.value = new_regs[0]
reg2.value = new_regs[1]
elif cell.type == CellType.UINT32:
tmp_reg = [reg.value, reg2.value]
value = cls.build_value_from_registers(tmp_reg, True)
value += 1
if maxval and value > maxval:
value = minval
if minval and value < minval:
value = minval
new_regs = cls.build_registers_from_value(value, True)
reg.value = new_regs[0]
reg2.value = new_regs[1]
@classmethod
def action_timestamp(cls, registers, inx, _cell, **_parameters):
"""Set current time.
:meta private:
"""
system_time = datetime.now()
registers[inx].value = system_time.year
registers[inx + 1].value = system_time.month - 1
registers[inx + 2].value = system_time.day
registers[inx + 3].value = system_time.weekday() + 1
registers[inx + 4].value = system_time.hour
registers[inx + 5].value = system_time.minute
registers[inx + 6].value = system_time.second
@classmethod
def action_reset(cls, _registers, _inx, _cell, **_parameters):
"""Reboot server.
:meta private:
"""
raise RuntimeError("RESET server")
@classmethod
def action_uptime(cls, registers, inx, cell, **_parameters):
"""Return uptime in seconds.
:meta private:
"""
value = int(datetime.now().timestamp()) - cls.start_time + 1
if cell.type in (CellType.BITS, CellType.UINT16):
registers[inx].value = value
elif cell.type == CellType.FLOAT32:
regs = cls.build_registers_from_value(value, False)
registers[inx].value = regs[0]
registers[inx + 1].value = regs[1]
elif cell.type == CellType.UINT32:
regs = cls.build_registers_from_value(value, True)
registers[inx].value = regs[0]
registers[inx + 1].value = regs[1]
# --------------------------------------------
# Internal helper methods
# --------------------------------------------
def validate_type(self, func_code, real_address, count) -> bool:
"""Check if request is done against correct type.
:meta private:
"""
check: tuple
if func_code in self._bits_func_code:
# Bit access
check = (CellType.BITS, -1)
reg_step = 1
elif count % 2:
# 16 bit access
check = (CellType.UINT16, CellType.STRING)
reg_step = 1
else:
check = (CellType.UINT32, CellType.FLOAT32, CellType.STRING)
reg_step = 2
for i in range(real_address, real_address + count, reg_step):
if self.registers[i].type in check:
continue
if self.registers[i].type is CellType.NEXT:
continue
return False
return True
@classmethod
def build_registers_from_value(cls, value, is_int):
"""Build registers from int32 or float32."""
regs = [0, 0]
if is_int:
value_bytes = int.to_bytes(value, 4, "big")
else:
value_bytes = struct.pack(">f", value)
regs[0] = int.from_bytes(value_bytes[:2], "big")
regs[1] = int.from_bytes(value_bytes[-2:], "big")
return regs
@classmethod
def build_value_from_registers(cls, registers, is_int):
"""Build int32 or float32 value from registers."""
value_bytes = int.to_bytes(registers[0], 2, "big") + int.to_bytes(
registers[1], 2, "big"
)
if is_int:
value = int.from_bytes(value_bytes, "big")
else:
value = struct.unpack(">f", value_bytes)[0]
return value

View File

@@ -0,0 +1,344 @@
"""Modbus Server Datastore.
For each server, you will create a ModbusServerContext and pass
in the default address space for each data access. The class
will create and manage the data.
Further modification of said data accesses should be performed
with [get,set][access]Values(address, count)
Datastore Implementation
-------------------------
There are two ways that the server datastore can be implemented.
The first is a complete range from "address" start to "count"
number of indices. This can be thought of as a straight array::
data = range(1, 1 + count)
[1,2,3,...,count]
The other way that the datastore can be implemented (and how
many devices implement it) is a associate-array::
data = {1:"1", 3:"3", ..., count:"count"}
[1,3,...,count]
The difference between the two is that the latter will allow
arbitrary gaps in its datastore while the former will not.
This is seen quite commonly in some modbus implementations.
What follows is a clear example from the field:
Say a company makes two devices to monitor power usage on a rack.
One works with three-phase and the other with a single phase. The
company will dictate a modbus data mapping such that registers::
n: phase 1 power
n+1: phase 2 power
n+2: phase 3 power
Using this, layout, the first device will implement n, n+1, and n+2,
however, the second device may set the latter two values to 0 or
will simply not implemented the registers thus causing a single read
or a range read to fail.
I have both methods implemented, and leave it up to the user to change
based on their preference.
"""
# pylint: disable=missing-type-doc
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Iterable
from typing import Any, Generic, TypeVar
from pymodbus.exceptions import ParameterException
# ---------------------------------------------------------------------------#
# Datablock Storage
# ---------------------------------------------------------------------------#
V = TypeVar('V', list, dict[int, Any])
class BaseModbusDataBlock(ABC, Generic[V]):
"""Base class for a modbus datastore.
Derived classes must create the following fields:
@address The starting address point
@defult_value The default value of the datastore
@values The actual datastore values
Derived classes must implemented the following methods:
validate(self, address, count=1)
getValues(self, address, count=1)
setValues(self, address, values)
reset(self)
Derived classes can implemented the following async methods:
async_getValues(self, address, count=1)
async_setValues(self, address, values)
but are not needed since these standard call the sync. methods.
"""
values: V
address: int
default_value: Any
@abstractmethod
def validate(self, address:int, count=1) -> bool:
"""Check to see if the request is in range.
:param address: The starting address
:param count: The number of values to test for
:raises TypeError:
"""
async def async_getValues(self, address: int, count=1) -> Iterable:
"""Return the requested values from the datastore.
:param address: The starting address
:param count: The number of values to retrieve
:raises TypeError:
"""
return self.getValues(address, count)
@abstractmethod
def getValues(self, address:int, count=1) -> Iterable:
"""Return the requested values from the datastore.
:param address: The starting address
:param count: The number of values to retrieve
:raises TypeError:
"""
async def async_setValues(self, address: int, values: list[int|bool]) -> None:
"""Set the requested values in the datastore.
:param address: The starting address
:param values: The values to store
:raises TypeError:
"""
self.setValues(address, values)
@abstractmethod
def setValues(self, address:int, values) -> None:
"""Set the requested values in the datastore.
:param address: The starting address
:param values: The values to store
:raises TypeError:
"""
def __str__(self):
"""Build a representation of the datastore.
:returns: A string representation of the datastore
"""
return f"DataStore({len(self.values)}, {self.default_value})"
def __iter__(self):
"""Iterate over the data block data.
:returns: An iterator of the data block data
"""
if isinstance(self.values, dict):
return iter(self.values.items())
return enumerate(self.values, self.address)
class ModbusSequentialDataBlock(BaseModbusDataBlock[list]):
"""Creates a sequential modbus datastore."""
def __init__(self, address, values):
"""Initialize the datastore.
:param address: The starting address of the datastore
:param values: Either a list or a dictionary of values
"""
self.address = address
if hasattr(values, "__iter__"):
self.values = list(values)
else:
self.values = [values]
self.default_value = self.values[0].__class__()
@classmethod
def create(cls):
"""Create a datastore.
With the full address space initialized to 0x00
:returns: An initialized datastore
"""
return cls(0x00, [0x00] * 65536)
def default(self, count, value=False):
"""Use to initialize a store to one value.
:param count: The number of fields to set
:param value: The default value to set to the fields
"""
self.default_value = value
self.values = [self.default_value] * count
self.address = 0x00
def reset(self):
"""Reset the datastore to the initialized default value."""
self.values = [self.default_value] * len(self.values)
def validate(self, address, count=1):
"""Check to see if the request is in range.
:param address: The starting address
:param count: The number of values to test for
:returns: True if the request in within range, False otherwise
"""
result = self.address <= address
result &= (self.address + len(self.values)) >= (address + count)
return result
def getValues(self, address, count=1):
"""Return the requested values of the datastore.
:param address: The starting address
:param count: The number of values to retrieve
:returns: The requested values from a:a+c
"""
start = address - self.address
return self.values[start : start + count]
def setValues(self, address, values):
"""Set the requested values of the datastore.
:param address: The starting address
:param values: The new values to be set
"""
if not isinstance(values, list):
values = [values]
start = address - self.address
self.values[start : start + len(values)] = values
class ModbusSparseDataBlock(BaseModbusDataBlock[dict[int, Any]]):
"""A sparse modbus datastore.
E.g Usage.
sparse = ModbusSparseDataBlock({10: [3, 5, 6, 8], 30: 1, 40: [0]*20})
This would create a datablock with 3 blocks
One starts at offset 10 with length 4, one at 30 with length 1, and one at 40 with length 20
sparse = ModbusSparseDataBlock([10]*100)
Creates a sparse datablock of length 100 starting at offset 0 and default value of 10
sparse = ModbusSparseDataBlock() --> Create empty datablock
sparse.setValues(0, [10]*10) --> Add block 1 at offset 0 with length 10 (default value 10)
sparse.setValues(30, [20]*5) --> Add block 2 at offset 30 with length 5 (default value 20)
Unless 'mutable' is set to True during initialization, the datablock cannot be altered with
setValues (new datablocks cannot be added)
"""
def __init__(self, values=None, mutable=True):
"""Initialize a sparse datastore.
Will only answer to addresses registered,
either initially here, or later via setValues()
:param values: Either a list or a dictionary of values
:param mutable: Whether the data-block can be altered later with setValues (i.e add more blocks)
If values is a list, a sequential datablock will be created.
If values is a dictionary, it should be in {offset: <int | list>} format
For each list, a sparse datablock is created, starting at 'offset' with the length of the list
For each integer, the value is set for the corresponding offset.
"""
self.values = {}
self._process_values(values)
self.mutable = mutable
self.default_value = self.values.copy()
@classmethod
def create(cls, values=None):
"""Create sparse datastore.
Use setValues to initialize registers.
:param values: Either a list or a dictionary of values
:returns: An initialized datastore
"""
return cls(values)
def reset(self):
"""Reset the store to the initially provided defaults."""
self.values = self.default_value.copy()
def validate(self, address, count=1):
"""Check to see if the request is in range.
:param address: The starting address
:param count: The number of values to test for
:returns: True if the request in within range, False otherwise
"""
if not count:
return False
handle = set(range(address, address + count))
return handle.issubset(set(iter(self.values.keys())))
def getValues(self, address, count=1):
"""Return the requested values of the datastore.
:param address: The starting address
:param count: The number of values to retrieve
:returns: The requested values from a:a+c
"""
return [self.values[i] for i in range(address, address + count)]
def _process_values(self, values):
"""Process values."""
def _process_as_dict(values):
for idx, val in iter(values.items()):
if isinstance(val, (list, tuple)):
for i, v_item in enumerate(val):
self.values[idx + i] = v_item
else:
self.values[idx] = int(val)
if isinstance(values, dict):
_process_as_dict(values)
return
if hasattr(values, "__iter__"):
values = dict(enumerate(values))
elif values is None:
values = {} # Must make a new dict here per instance
else:
raise ParameterException(
"Values for datastore must be a list or dictionary"
)
_process_as_dict(values)
def setValues(self, address, values, use_as_default=False):
"""Set the requested values of the datastore.
:param address: The starting address
:param values: The new values to be set
:param use_as_default: Use the values as default
:raises ParameterException:
"""
if isinstance(values, dict):
new_offsets = list(set(values.keys()) - set(self.values.keys()))
if new_offsets and not self.mutable:
raise ParameterException(f"Offsets {new_offsets} not in range")
self._process_values(values)
else:
if not isinstance(values, list):
values = [values]
for idx, val in enumerate(values):
if address + idx not in self.values and not self.mutable:
raise ParameterException("Offset {address+idx} not in range")
self.values[address + idx] = val
if use_as_default:
for idx, val in iter(self.values.items()):
self.default_value[idx] = val

View File

@@ -0,0 +1,589 @@
"""Modbus Device Controller.
These are the device management handlers. They should be
maintained in the server context and the various methods
should be inserted in the correct locations.
"""
from __future__ import annotations
__all__ = [
"ModbusPlusStatistics",
"ModbusDeviceIdentification",
"DeviceInformationFactory",
]
import struct
# pylint: disable=missing-type-doc
from collections import OrderedDict
from pymodbus.constants import INTERNAL_ERROR, DeviceInformation
from pymodbus.events import ModbusEvent
from pymodbus.utilities import dict_property
# ---------------------------------------------------------------------------#
# Modbus Plus Statistics
# ---------------------------------------------------------------------------#
class ModbusPlusStatistics:
"""This is used to maintain the current modbus plus statistics count.
As of right now this is simply a stub to complete the modbus implementation.
For more information, see the modbus implementation guide page 87.
"""
__data = OrderedDict(
{
"node_type_id": [0x00] * 2, # 00
"software_version_number": [0x00] * 2, # 01
"network_address": [0x00] * 2, # 02
"mac_state_variable": [0x00] * 2, # 03
"peer_status_code": [0x00] * 2, # 04
"token_pass_counter": [0x00] * 2, # 05
"token_rotation_time": [0x00] * 2, # 06
"program_master_token_failed": [0x00], # 07 hi
"data_master_token_failed": [0x00], # 07 lo
"program_master_token_owner": [0x00], # 08 hi
"data_master_token_owner": [0x00], # 08 lo
"program_slave_token_owner": [0x00], # 09 hi
"data_slave_token_owner": [0x00], # 09 lo
"data_slave_command_transfer": [0x00], # 10 hi
"__unused_10_lowbit": [0x00], # 10 lo
"program_slave_command_transfer": [0x00], # 11 hi
"program_master_rsp_transfer": [0x00], # 11 lo
"program_slave_auto_logout": [0x00], # 12 hi
"program_master_connect_status": [0x00], # 12 lo
"receive_buffer_dma_overrun": [0x00], # 13 hi
"pretransmit_deferral_error": [0x00], # 13 lo
"frame_size_error": [0x00], # 14 hi
"repeated_command_received": [0x00], # 14 lo
"receiver_alignment_error": [0x00], # 15 hi
"receiver_collision_abort_error": [0x00], # 15 lo
"bad_packet_length_error": [0x00], # 16 hi
"receiver_crc_error": [0x00], # 16 lo
"transmit_buffer_dma_underrun": [0x00], # 17 hi
"bad_link_address_error": [0x00], # 17 lo
"bad_mac_function_code_error": [0x00], # 18 hi
"internal_packet_length_error": [0x00], # 18 lo
"communication_failed_error": [0x00], # 19 hi
"communication_retries": [0x00], # 19 lo
"no_response_error": [0x00], # 20 hi
"good_receive_packet": [0x00], # 20 lo
"unexpected_path_error": [0x00], # 21 hi
"exception_response_error": [0x00], # 21 lo
"forgotten_transaction_error": [0x00], # 22 hi
"unexpected_response_error": [0x00], # 22 lo
"active_station_bit_map": [0x00] * 8, # 23-26
"token_station_bit_map": [0x00] * 8, # 27-30
"global_data_bit_map": [0x00] * 8, # 31-34
"receive_buffer_use_bit_map": [0x00] * 8, # 35-37
"data_master_output_path": [0x00] * 8, # 38-41
"data_slave_input_path": [0x00] * 8, # 42-45
"program_master_outptu_path": [0x00] * 8, # 46-49
"program_slave_input_path": [0x00] * 8, # 50-53
}
)
def __init__(self):
"""Initialize the modbus plus statistics with the default information."""
self.reset()
def __iter__(self):
"""Iterate over the statistics.
:returns: An iterator of the modbus plus statistics
"""
return iter(self.__data.items())
def reset(self):
"""Clear all of the modbus plus statistics."""
for key in self.__data:
self.__data[key] = [0x00] * len(self.__data[key])
def summary(self):
"""Return a summary of the modbus plus statistics.
:returns: 54 16-bit words representing the status
"""
return iter(self.__data.values())
def encode(self):
"""Return a summary of the modbus plus statistics.
:returns: 54 16-bit words representing the status
"""
total, values = [], sum(self.__data.values(), []) # noqa: RUF017
for i in range(0, len(values), 2):
total.append((values[i] << 8) | values[i + 1])
return total
# ---------------------------------------------------------------------------#
# Device Information Control
# ---------------------------------------------------------------------------#
class ModbusDeviceIdentification:
"""This is used to supply the device identification.
For the readDeviceIdentification function
For more information read section 6.21 of the modbus
application protocol.
"""
__data = {
0x00: "", # VendorName
0x01: "", # ProductCode
0x02: "", # MajorMinorRevision
0x03: "", # VendorUrl
0x04: "", # ProductName
0x05: "", # ModelName
0x06: "", # UserApplicationName
0x07: "", # reserved
0x08: "", # reserved
# 0x80 -> 0xFF are privatek
}
__names = [
"VendorName",
"ProductCode",
"MajorMinorRevision",
"VendorUrl",
"ProductName",
"ModelName",
"UserApplicationName",
]
def __init__(self, info=None, info_name=None):
"""Initialize the datastore with the elements you need.
(note acceptable range is [0x00-0x06,0x80-0xFF] inclusive)
:param info: A dictionary of {int:string} of values
:param set: A dictionary of {name:string} of values
"""
if isinstance(info_name, dict):
for key in info_name:
inx = self.__names.index(key)
self.__data[inx] = info_name[key]
if isinstance(info, dict):
for key in info:
if (0x06 >= key >= 0x00) or (0xFF >= key >= 0x80):
self.__data[key] = info[key]
def __iter__(self):
"""Iterate over the device information.
:returns: An iterator of the device information
"""
return iter(self.__data.items())
def summary(self):
"""Return a summary of the main items.
:returns: An dictionary of the main items
"""
return dict(zip(self.__names, iter(self.__data.values())))
def update(self, value):
"""Update the values of this identity.
using another identify as the value
:param value: The value to copy values from
"""
self.__data.update(value)
def __setitem__(self, key, value):
"""Access the device information.
:param key: The register to set
:param value: The new value for referenced register
"""
if key not in [0x07, 0x08]:
self.__data[key] = value
def __getitem__(self, key):
"""Access the device information.
:param key: The register to read
"""
return self.__data.setdefault(key, "")
def __str__(self):
"""Build a representation of the device.
:returns: A string representation of the device
"""
return "DeviceIdentity"
# -------------------------------------------------------------------------#
# Properties
# -------------------------------------------------------------------------#
# fmt: off
VendorName = dict_property(lambda s: s.__data, 0) # pylint: disable=protected-access
ProductCode = dict_property(lambda s: s.__data, 1) # pylint: disable=protected-access
MajorMinorRevision = dict_property(lambda s: s.__data, 2) # pylint: disable=protected-access
VendorUrl = dict_property(lambda s: s.__data, 3) # pylint: disable=protected-access
ProductName = dict_property(lambda s: s.__data, 4) # pylint: disable=protected-access
ModelName = dict_property(lambda s: s.__data, 5) # pylint: disable=protected-access
UserApplicationName = dict_property(lambda s: s.__data, 6) # pylint: disable=protected-access
# fmt: on
class DeviceInformationFactory: # pylint: disable=too-few-public-methods
"""This is a helper factory.
That really just hides
some of the complexity of processing the device information
requests (function code 0x2b 0x0e).
"""
__lookup = {
DeviceInformation.BASIC: lambda c, r, i: c.__gets( # pylint: disable=protected-access
r, list(range(i, 0x03))
),
DeviceInformation.REGULAR: lambda c, r, i: c.__gets( # pylint: disable=protected-access
r,
list(range(i, 0x07))
if c.__get(r, i)[i] # pylint: disable=protected-access
else list(range(0, 0x07)),
),
DeviceInformation.EXTENDED: lambda c, r, i: c.__gets( # pylint: disable=protected-access
r,
[x for x in range(i, 0x100) if x not in range(0x07, 0x80)]
if c.__get(r, i)[i] # pylint: disable=protected-access
else [x for x in range(0, 0x100) if x not in range(0x07, 0x80)],
),
DeviceInformation.SPECIFIC: lambda c, r, i: c.__get( # pylint: disable=protected-access
r, i
),
}
@classmethod
def get(cls, control, read_code=DeviceInformation.BASIC, object_id=0x00):
"""Get the requested device data from the system.
:param control: The control block to pull data from
:param read_code: The read code to process
:param object_id: The specific object_id to read
:returns: The requested data (id, length, value)
"""
identity = control.Identity
return cls.__lookup[read_code](cls, identity, object_id)
@classmethod
def __get(cls, identity, object_id): # pylint: disable=unused-private-member
"""Read a single object_id from the device information.
:param identity: The identity block to pull data from
:param object_id: The specific object id to read
:returns: The requested data (id, length, value)
"""
return {object_id: identity[object_id]}
@classmethod
def __gets(cls, identity, object_ids): # pylint: disable=unused-private-member
"""Read multiple object_ids from the device information.
:param identity: The identity block to pull data from
:param object_ids: The specific object ids to read
:returns: The requested data (id, length, value)
"""
return {oid: identity[oid] for oid in object_ids if identity[oid]}
def __init__(self):
"""Prohibit objects."""
raise RuntimeError(INTERNAL_ERROR)
# ---------------------------------------------------------------------------#
# Counters Handler
# ---------------------------------------------------------------------------#
class ModbusCountersHandler:
"""This is a helper class to simplify the properties for the counters.
0x0B 1 Return Bus Message Count
Quantity of messages that the remote
device has detected on the communications system since its
last restart, clear counters operation, or power-up. Messages
with bad CRC are not taken into account.
0x0C 2 Return Bus Communication Error Count
Quantity of CRC errors encountered by the remote device since its
last restart, clear counters operation, or power-up. In case of
an error detected on the character level, (overrun, parity error),
or in case of a message length < 3 bytes, the receiving device is
not able to calculate the CRC. In such cases, this counter is
also incremented.
0x0D 3 Return Slave Exception Error Count
Quantity of MODBUS exception error detected by the remote device
since its last restart, clear counters operation, or power-up. It
comprises also the error detected in broadcast messages even if an
exception message is not returned in this case.
Exception errors are described and listed in "MODBUS Application
Protocol Specification" document.
0xOE 4 Return Slave Message Count
Quantity of messages addressed to the remote device, including
broadcast messages, that the remote device has processed since its
last restart, clear counters operation, or power-up.
0x0F 5 Return Slave No Response Count
Quantity of messages received by the remote device for which it
returned no response (neither a normal response nor an exception
response), since its last restart, clear counters operation, or
power-up. Then, this counter counts the number of broadcast
messages it has received.
0x10 6 Return Slave NAK Count
Quantity of messages addressed to the remote device for which it
returned a Negative Acknowledge (NAK) exception response, since
its last restart, clear counters operation, or power-up. Exception
responses are described and listed in "MODBUS Application Protocol
Specification" document.
0x11 7 Return Slave Busy Count
Quantity of messages addressed to the remote device for which it
returned a Slave Device Busy exception response, since its last
restart, clear counters operation, or power-up. Exception
responses are described and listed in "MODBUS Application
Protocol Specification" document.
0x12 8 Return Bus Character Overrun Count
Quantity of messages addressed to the remote device that it could
not handle due to a character overrun condition, since its last
restart, clear counters operation, or power-up. A character
overrun is caused by data characters arriving at the port faster
than they can.
.. note:: I threw the event counter in here for convenience
"""
__data = {i: 0x0000 for i in range(9)}
__names = [
"BusMessage",
"BusCommunicationError",
"SlaveExceptionError",
"SlaveMessage",
"SlaveNoResponse",
"SlaveNAK",
"SlaveBusy",
"BusCharacterOverrun",
]
def __iter__(self):
"""Iterate over the device counters.
:returns: An iterator of the device counters
"""
return zip(self.__names, iter(self.__data.values()))
def update(self, values):
"""Update the values of this identity.
using another identify as the value
:param values: The value to copy values from
"""
for k, v_item in iter(values.items()):
v_item += self.__getattribute__( # pylint: disable=unnecessary-dunder-call
k
)
self.__setattr__(k, v_item) # pylint: disable=unnecessary-dunder-call
def reset(self):
"""Clear all of the system counters."""
self.__data = {i: 0x0000 for i in range(9)}
def summary(self):
"""Return a summary of the counters current status.
:returns: A byte with each bit representing each counter
"""
count, result = 0x01, 0x00
for i in iter(self.__data.values()):
if i != 0x00: # pylint: disable=compare-to-zero
result |= count
count <<= 1
return result
# -------------------------------------------------------------------------#
# Properties
# -------------------------------------------------------------------------#
# fmt: off
BusMessage = dict_property(lambda s: s.__data, 0) # pylint: disable=protected-access
BusCommunicationError = dict_property(lambda s: s.__data, 1) # pylint: disable=protected-access
BusExceptionError = dict_property(lambda s: s.__data, 2) # pylint: disable=protected-access
SlaveMessage = dict_property(lambda s: s.__data, 3) # pylint: disable=protected-access
SlaveNoResponse = dict_property(lambda s: s.__data, 4) # pylint: disable=protected-access
SlaveNAK = dict_property(lambda s: s.__data, 5) # pylint: disable=protected-access
SlaveBusy = dict_property(lambda s: s.__data, 6) # pylint: disable=protected-access
BusCharacterOverrun = dict_property(lambda s: s.__data, 7) # pylint: disable=protected-access
Event = dict_property(lambda s: s.__data, 8) # pylint: disable=protected-access
# fmt: on
# ---------------------------------------------------------------------------#
# Main server control block
# ---------------------------------------------------------------------------#
class ModbusControlBlock:
"""This is a global singleton that controls all system information.
All activity should be logged here and all diagnostic requests
should come from here.
"""
_mode = "ASCII"
_diagnostic = [False] * 16
_listen_only = False
_delimiter = b"\r"
_counters = ModbusCountersHandler()
_identity = ModbusDeviceIdentification()
_plus = ModbusPlusStatistics()
_events: list[ModbusEvent] = []
# -------------------------------------------------------------------------#
# Magic
# -------------------------------------------------------------------------#
def __str__(self):
"""Build a representation of the control block.
:returns: A string representation of the control block
"""
return "ModbusControl"
def __iter__(self):
"""Iterate over the device counters.
:returns: An iterator of the device counters
"""
return self._counters.__iter__()
def __new__(cls):
"""Create a new instance."""
if "_inst" not in vars(cls):
cls._inst = object.__new__(cls)
return cls._inst
# -------------------------------------------------------------------------#
# Events
# -------------------------------------------------------------------------#
def addEvent(self, event: ModbusEvent):
"""Add a new event to the event log.
:param event: A new event to add to the log
"""
self._events.insert(0, event)
self._events = self._events[0:64] # chomp to 64 entries
self.Counter.Event += 1
def getEvents(self):
"""Return an encoded collection of the event log.
:returns: The encoded events packet
"""
events = [event.encode() for event in self._events]
return b"".join(events)
def clearEvents(self):
"""Clear the current list of events."""
self._events = []
# -------------------------------------------------------------------------#
# Other Properties
# -------------------------------------------------------------------------#
Identity = property(lambda s: s._identity)
Counter = property(lambda s: s._counters)
Events = property(lambda s: s._events)
Plus = property(lambda s: s._plus)
def reset(self):
"""Clear all of the system counters and the diagnostic register."""
self._events = []
self._counters.reset()
self._diagnostic = [False] * 16
# -------------------------------------------------------------------------#
# Listen Properties
# -------------------------------------------------------------------------#
def _setListenOnly(self, value):
"""Toggle the listen only status.
:param value: The value to set the listen status to
"""
self._listen_only = bool(value)
ListenOnly = property(lambda s: s._listen_only, _setListenOnly)
# -------------------------------------------------------------------------#
# Mode Properties
# -------------------------------------------------------------------------#
def _setMode(self, mode):
"""Toggle the current serial mode.
:param mode: The data transfer method in (RTU, ASCII)
"""
if mode in {"ASCII", "RTU"}:
self._mode = mode
Mode = property(lambda s: s._mode, _setMode)
# -------------------------------------------------------------------------#
# Delimiter Properties
# -------------------------------------------------------------------------#
def _setDelimiter(self, char):
"""Change the serial delimiter character.
:param char: The new serial delimiter character
"""
if isinstance(char, str):
self._delimiter = char.encode()
if isinstance(char, bytes):
self._delimiter = char
elif isinstance(char, int):
self._delimiter = struct.pack(">B", char)
Delimiter = property(lambda s: s._delimiter, _setDelimiter)
# -------------------------------------------------------------------------#
# Diagnostic Properties
# -------------------------------------------------------------------------#
def setDiagnostic(self, mapping):
"""Set the value in the diagnostic register.
:param mapping: Dictionary of key:value pairs to set
"""
for entry in iter(mapping.items()):
if entry[0] >= 0 and entry[0] < len(self._diagnostic):
self._diagnostic[entry[0]] = bool(entry[1])
def getDiagnostic(self, bit):
"""Get the value in the diagnostic register.
:param bit: The bit to get
:returns: The current value of the requested bit
"""
try:
if bit and 0 <= bit < len(self._diagnostic):
return self._diagnostic[bit]
except Exception: # pylint: disable=broad-except
return None
return None
def getDiagnosticRegister(self):
"""Get the entire diagnostic register.
:returns: The diagnostic register collection
"""
return self._diagnostic

View File

@@ -0,0 +1,203 @@
"""Modbus Remote Events.
An event byte returned by the Get Communications Event Log function
can be any one of four types. The type is defined by bit 7
(the high-order bit) in each byte. It may be further defined by bit 6.
"""
# pylint: disable=missing-type-doc
from pymodbus.exceptions import NotImplementedException, ParameterException
from pymodbus.utilities import pack_bitstring, unpack_bitstring
class ModbusEvent:
"""Define modbus events."""
def encode(self):
"""Encode the status bits to an event message.
:raises NotImplementedException:
"""
raise NotImplementedException
def decode(self, event):
"""Decode the event message to its status bits.
:param event: The event to decode
:raises NotImplementedException:
"""
raise NotImplementedException
class RemoteReceiveEvent(ModbusEvent):
"""Remote device MODBUS Receive Event.
The remote device stores this type of event byte when a query message
is received. It is stored before the remote device processes the message.
This event is defined by bit 7 set to logic "1". The other bits will be
set to a logic "1" if the corresponding condition is TRUE. The bit layout
is::
Bit Contents
----------------------------------
0 Not Used
2 Not Used
3 Not Used
4 Character Overrun
5 Currently in Listen Only Mode
6 Broadcast Receive
7 1
"""
def __init__(self, overrun=False, listen=False, broadcast=False):
"""Initialize a new event instance."""
self.overrun = overrun
self.listen = listen
self.broadcast = broadcast
def encode(self) -> bytes:
"""Encode the status bits to an event message.
:returns: The encoded event message
"""
bits = [False] * 3
bits += [self.overrun, self.listen, self.broadcast, True]
packet = pack_bitstring(bits)
return packet
def decode(self, event: bytes) -> None:
"""Decode the event message to its status bits.
:param event: The event to decode
"""
bits = unpack_bitstring(event)
self.overrun = bits[4]
self.listen = bits[5]
self.broadcast = bits[6]
class RemoteSendEvent(ModbusEvent):
"""Remote device MODBUS Send Event.
The remote device stores this type of event byte when it finishes
processing a request message. It is stored if the remote device
returned a normal or exception response, or no response.
This event is defined by bit 7 set to a logic "0", with bit 6 set to a "1".
The other bits will be set to a logic "1" if the corresponding
condition is TRUE. The bit layout is::
Bit Contents
-----------------------------------------------------------
0 Read Exception Sent (Exception Codes 1-3)
1 Slave Abort Exception Sent (Exception Code 4)
2 Slave Busy Exception Sent (Exception Codes 5-6)
3 Slave Program NAK Exception Sent (Exception Code 7)
4 Write Timeout Error Occurred
5 Currently in Listen Only Mode
6 1
7 0
"""
def __init__(self, read=False, slave_abort=False, slave_busy=False, slave_nak=False, write_timeout=False, listen=False):
"""Initialize a new event instance."""
self.read = read
self.slave_abort = slave_abort
self.slave_busy = slave_busy
self.slave_nak = slave_nak
self.write_timeout = write_timeout
self.listen = listen
def encode(self):
"""Encode the status bits to an event message.
:returns: The encoded event message
"""
bits = [
self.read,
self.slave_abort,
self.slave_busy,
self.slave_nak,
self.write_timeout,
self.listen,
]
bits += [True, False]
packet = pack_bitstring(bits)
return packet
def decode(self, event):
"""Decode the event message to its status bits.
:param event: The event to decode
"""
# todo fix the start byte count # pylint: disable=fixme
bits = unpack_bitstring(event)
self.read = bits[0]
self.slave_abort = bits[1]
self.slave_busy = bits[2]
self.slave_nak = bits[3]
self.write_timeout = bits[4]
self.listen = bits[5]
class EnteredListenModeEvent(ModbusEvent):
"""Enter Remote device Listen Only Mode.
The remote device stores this type of event byte when it enters
the Listen Only Mode. The event is defined by a content of 04 hex.
"""
value = 0x04
__encoded = b"\x04"
def encode(self):
"""Encode the status bits to an event message.
:returns: The encoded event message
"""
return self.__encoded
def decode(self, event):
"""Decode the event message to its status bits.
:param event: The event to decode
:raises ParameterException:
"""
if event != self.__encoded:
raise ParameterException("Invalid decoded value")
class CommunicationRestartEvent(ModbusEvent):
"""Restart remote device Initiated Communication.
The remote device stores this type of event byte when its communications
port is restarted. The remote device can be restarted by the Diagnostics
function (code 08), with sub-function Restart Communications Option
(code 00 01).
That function also places the remote device into a "Continue on Error"
or "Stop on Error" mode. If the remote device is placed into "Continue on
Error" mode, the event byte is added to the existing event log. If the
remote device is placed into "Stop on Error" mode, the byte is added to
the log and the rest of the log is cleared to zeros.
The event is defined by a content of zero.
"""
value = 0x00
__encoded = b"\x00"
def encode(self):
"""Encode the status bits to an event message.
:returns: The encoded event message
"""
return self.__encoded
def decode(self, event):
"""Decode the event message to its status bits.
:param event: The event to decode
:raises ParameterException:
"""
if event != self.__encoded:
raise ParameterException("Invalid decoded value")

View File

@@ -0,0 +1,116 @@
"""Pymodbus Exceptions.
Custom exceptions to be used in the Modbus code.
"""
__all__ = [
"ModbusIOException",
"ParameterException",
"NotImplementedException",
"ConnectionException",
"NoSuchSlaveException",
"InvalidMessageReceivedException",
"MessageRegisterException",
]
class ModbusException(Exception):
"""Base modbus exception."""
def __init__(self, string):
"""Initialize the exception.
:param string: The message to append to the error
"""
self.string = string
super().__init__(string)
def __str__(self):
"""Return string representation."""
return f"Modbus Error: {self.string}"
def isError(self):
"""Error"""
return True
class ModbusIOException(ModbusException):
"""Error resulting from data i/o."""
def __init__(self, string="", function_code=None):
"""Initialize the exception.
:param string: The message to append to the error
"""
self.fcode = function_code
self.message = f"[Input/Output] {string}"
ModbusException.__init__(self, self.message)
class ParameterException(ModbusException):
"""Error resulting from invalid parameter."""
def __init__(self, string=""):
"""Initialize the exception.
:param string: The message to append to the error
"""
message = f"[Invalid Parameter] {string}"
ModbusException.__init__(self, message)
class NoSuchSlaveException(ModbusException):
"""Error resulting from making a request to a slave that does not exist."""
def __init__(self, string=""):
"""Initialize the exception.
:param string: The message to append to the error
"""
message = f"[No Such Slave] {string}"
ModbusException.__init__(self, message)
class NotImplementedException(ModbusException):
"""Error resulting from not implemented function."""
def __init__(self, string=""):
"""Initialize the exception.
:param string: The message to append to the error
"""
message = f"[Not Implemented] {string}"
ModbusException.__init__(self, message)
class ConnectionException(ModbusException):
"""Error resulting from a bad connection."""
def __init__(self, string=""):
"""Initialize the exception.
:param string: The message to append to the error
"""
message = f"[Connection] {string}"
ModbusException.__init__(self, message)
class InvalidMessageReceivedException(ModbusException):
"""Error resulting from invalid response received or decoded."""
def __init__(self, string=""):
"""Initialize the exception.
:param string: The message to append to the error
"""
message = f"[Invalid Message] {string}"
ModbusException.__init__(self, message)
class MessageRegisterException(ModbusException):
"""Error resulting from failing to register a custom message request/response."""
def __init__(self, string=""):
"""Initialize."""
message = f"[Error registering message] {string}"
ModbusException.__init__(self, message)

View File

@@ -0,0 +1,289 @@
"""Modbus Request/Response Decoder Factories.
The following factories make it easy to decode request/response messages.
To add a new request/response pair to be decodeable by the library, simply
add them to the respective function lookup table (order doesn't matter, but
it does help keep things organized).
Regardless of how many functions are added to the lookup, O(1) behavior is
kept as a result of a pre-computed lookup dictionary.
"""
# pylint: disable=missing-type-doc
from collections.abc import Callable
from pymodbus.exceptions import MessageRegisterException, ModbusException
from pymodbus.logging import Log
from pymodbus.pdu import bit_read_message as bit_r_msg
from pymodbus.pdu import bit_write_message as bit_w_msg
from pymodbus.pdu import diag_message as diag_msg
from pymodbus.pdu import file_message as file_msg
from pymodbus.pdu import mei_message as mei_msg
from pymodbus.pdu import other_message as o_msg
from pymodbus.pdu import pdu
from pymodbus.pdu import register_read_message as reg_r_msg
from pymodbus.pdu import register_write_message as reg_w_msg
# --------------------------------------------------------------------------- #
# Server Decoder
# --------------------------------------------------------------------------- #
class ServerDecoder:
"""Request Message Factory (Server).
To add more implemented functions, simply add them to the list
"""
__function_table = [
reg_r_msg.ReadHoldingRegistersRequest,
bit_r_msg.ReadDiscreteInputsRequest,
reg_r_msg.ReadInputRegistersRequest,
bit_r_msg.ReadCoilsRequest,
bit_w_msg.WriteMultipleCoilsRequest,
reg_w_msg.WriteMultipleRegistersRequest,
reg_w_msg.WriteSingleRegisterRequest,
bit_w_msg.WriteSingleCoilRequest,
reg_r_msg.ReadWriteMultipleRegistersRequest,
diag_msg.DiagnosticStatusRequest,
o_msg.ReadExceptionStatusRequest,
o_msg.GetCommEventCounterRequest,
o_msg.GetCommEventLogRequest,
o_msg.ReportSlaveIdRequest,
file_msg.ReadFileRecordRequest,
file_msg.WriteFileRecordRequest,
reg_w_msg.MaskWriteRegisterRequest,
file_msg.ReadFifoQueueRequest,
mei_msg.ReadDeviceInformationRequest,
]
__sub_function_table = [
diag_msg.ReturnQueryDataRequest,
diag_msg.RestartCommunicationsOptionRequest,
diag_msg.ReturnDiagnosticRegisterRequest,
diag_msg.ChangeAsciiInputDelimiterRequest,
diag_msg.ForceListenOnlyModeRequest,
diag_msg.ClearCountersRequest,
diag_msg.ReturnBusMessageCountRequest,
diag_msg.ReturnBusCommunicationErrorCountRequest,
diag_msg.ReturnBusExceptionErrorCountRequest,
diag_msg.ReturnSlaveMessageCountRequest,
diag_msg.ReturnSlaveNoResponseCountRequest,
diag_msg.ReturnSlaveNAKCountRequest,
diag_msg.ReturnSlaveBusyCountRequest,
diag_msg.ReturnSlaveBusCharacterOverrunCountRequest,
diag_msg.ReturnIopOverrunCountRequest,
diag_msg.ClearOverrunCountRequest,
diag_msg.GetClearModbusPlusRequest,
mei_msg.ReadDeviceInformationRequest,
]
@classmethod
def getFCdict(cls) -> dict[int, Callable]:
"""Build function code - class list."""
return {f.function_code: f for f in cls.__function_table} # type: ignore[attr-defined]
def __init__(self) -> None:
"""Initialize the client lookup tables."""
functions = {f.function_code for f in self.__function_table} # type: ignore[attr-defined]
self.lookup = self.getFCdict()
self.__sub_lookup: dict[int, dict[int, Callable]] = {f: {} for f in functions}
for f in self.__sub_function_table:
self.__sub_lookup[f.function_code][f.sub_function_code] = f # type: ignore[attr-defined]
def decode(self, message):
"""Decode a request packet.
:param message: The raw modbus request packet
:return: The decoded modbus message or None if error
"""
try:
return self._helper(message)
except ModbusException as exc:
Log.warning("Unable to decode request {}", exc)
return None
def lookupPduClass(self, function_code):
"""Use `function_code` to determine the class of the PDU.
:param function_code: The function code specified in a frame.
:returns: The class of the PDU that has a matching `function_code`.
"""
return self.lookup.get(function_code, pdu.ExceptionResponse)
def _helper(self, data: str):
"""Generate the correct request object from a valid request packet.
This decodes from a list of the currently implemented request types.
:param data: The request packet to decode
:returns: The decoded request or illegal function request object
"""
function_code = int(data[0])
if not (request := self.lookup.get(function_code, lambda: None)()):
Log.debug("Factory Request[{}]", function_code)
request = pdu.IllegalFunctionRequest(function_code, 0, 0, 0, False)
else:
fc_string = "{}: {}".format( # pylint: disable=consider-using-f-string
str(self.lookup[function_code]) # pylint: disable=use-maxsplit-arg
.split(".")[-1]
.rstrip('">"'),
function_code,
)
Log.debug("Factory Request[{}]", fc_string)
request.decode(data[1:])
if hasattr(request, "sub_function_code"):
lookup = self.__sub_lookup.get(request.function_code, {})
if subtype := lookup.get(request.sub_function_code, None):
request.__class__ = subtype
return request
def register(self, function):
"""Register a function and sub function class with the decoder.
:param function: Custom function class to register
:raises MessageRegisterException:
"""
if not issubclass(function, pdu.ModbusRequest):
raise MessageRegisterException(
f'"{function.__class__.__name__}" is Not a valid Modbus Message'
". Class needs to be derived from "
"`pymodbus.pdu.ModbusRequest` "
)
self.lookup[function.function_code] = function
if hasattr(function, "sub_function_code"):
if function.function_code not in self.__sub_lookup:
self.__sub_lookup[function.function_code] = {}
self.__sub_lookup[function.function_code][
function.sub_function_code
] = function
# --------------------------------------------------------------------------- #
# Client Decoder
# --------------------------------------------------------------------------- #
class ClientDecoder:
"""Response Message Factory (Client).
To add more implemented functions, simply add them to the list
"""
function_table = [
reg_r_msg.ReadHoldingRegistersResponse,
bit_r_msg.ReadDiscreteInputsResponse,
reg_r_msg.ReadInputRegistersResponse,
bit_r_msg.ReadCoilsResponse,
bit_w_msg.WriteMultipleCoilsResponse,
reg_w_msg.WriteMultipleRegistersResponse,
reg_w_msg.WriteSingleRegisterResponse,
bit_w_msg.WriteSingleCoilResponse,
reg_r_msg.ReadWriteMultipleRegistersResponse,
diag_msg.DiagnosticStatusResponse,
o_msg.ReadExceptionStatusResponse,
o_msg.GetCommEventCounterResponse,
o_msg.GetCommEventLogResponse,
o_msg.ReportSlaveIdResponse,
file_msg.ReadFileRecordResponse,
file_msg.WriteFileRecordResponse,
reg_w_msg.MaskWriteRegisterResponse,
file_msg.ReadFifoQueueResponse,
mei_msg.ReadDeviceInformationResponse,
]
__sub_function_table = [
diag_msg.ReturnQueryDataResponse,
diag_msg.RestartCommunicationsOptionResponse,
diag_msg.ReturnDiagnosticRegisterResponse,
diag_msg.ChangeAsciiInputDelimiterResponse,
diag_msg.ForceListenOnlyModeResponse,
diag_msg.ClearCountersResponse,
diag_msg.ReturnBusMessageCountResponse,
diag_msg.ReturnBusCommunicationErrorCountResponse,
diag_msg.ReturnBusExceptionErrorCountResponse,
diag_msg.ReturnSlaveMessageCountResponse,
diag_msg.ReturnSlaveNoResponseCountResponse,
diag_msg.ReturnSlaveNAKCountResponse,
diag_msg.ReturnSlaveBusyCountResponse,
diag_msg.ReturnSlaveBusCharacterOverrunCountResponse,
diag_msg.ReturnIopOverrunCountResponse,
diag_msg.ClearOverrunCountResponse,
diag_msg.GetClearModbusPlusResponse,
mei_msg.ReadDeviceInformationResponse,
]
def __init__(self) -> None:
"""Initialize the client lookup tables."""
functions = {f.function_code for f in self.function_table} # type: ignore[attr-defined]
self.lookup = {f.function_code: f for f in self.function_table} # type: ignore[attr-defined]
self.__sub_lookup: dict[int, dict[int, Callable]] = {f: {} for f in functions}
for f in self.__sub_function_table:
self.__sub_lookup[f.function_code][f.sub_function_code] = f # type: ignore[attr-defined]
def lookupPduClass(self, function_code):
"""Use `function_code` to determine the class of the PDU.
:param function_code: The function code specified in a frame.
:returns: The class of the PDU that has a matching `function_code`.
"""
return self.lookup.get(function_code, pdu.ExceptionResponse)
def decode(self, message):
"""Decode a response packet.
:param message: The raw packet to decode
:return: The decoded modbus message or None if error
"""
try:
return self._helper(message)
except ModbusException as exc:
Log.error("Unable to decode response {}", exc)
return None
def _helper(self, data: str):
"""Generate the correct response object from a valid response packet.
This decodes from a list of the currently implemented request types.
:param data: The response packet to decode
:returns: The decoded request or an exception response object
:raises ModbusException:
"""
fc_string = data[0]
function_code = int(fc_string)
if function_code in self.lookup: # pylint: disable=consider-using-assignment-expr
fc_string = "{}: {}".format( # pylint: disable=consider-using-f-string
str(self.lookup[function_code]) # pylint: disable=use-maxsplit-arg
.split(".")[-1]
.rstrip('">"'),
function_code,
)
Log.debug("Factory Response[{}]", fc_string)
response = self.lookup.get(function_code, lambda: None)()
if function_code > 0x80:
code = function_code & 0x7F # strip error portion
response = pdu.ExceptionResponse(code, pdu.ModbusExceptions.IllegalFunction)
if not response:
raise ModbusException(f"Unknown response {function_code}")
response.decode(data[1:])
if hasattr(response, "sub_function_code"):
lookup = self.__sub_lookup.get(response.function_code, {})
if subtype := lookup.get(response.sub_function_code, None):
response.__class__ = subtype
return response
def register(self, function):
"""Register a function and sub function class with the decoder."""
if function and not issubclass(function, pdu.ModbusResponse):
raise MessageRegisterException(
f'"{function.__class__.__name__}" is Not a valid Modbus Message'
". Class needs to be derived from "
"`pymodbus.pdu.ModbusResponse` "
)
self.lookup[function.function_code] = function
if hasattr(function, "sub_function_code"):
if function.function_code not in self.__sub_lookup:
self.__sub_lookup[function.function_code] = {}
self.__sub_lookup[function.function_code][
function.sub_function_code
] = function

View File

@@ -0,0 +1,27 @@
"""Framer."""
__all__ = [
"Framer",
"FRAMER_NAME_TO_CLASS",
"ModbusFramer",
"ModbusAsciiFramer",
"ModbusRtuFramer",
"ModbusSocketFramer",
"ModbusTlsFramer",
"Framer",
"FramerType",
]
from pymodbus.framer.framer import Framer, FramerType
from pymodbus.framer.old_framer_ascii import ModbusAsciiFramer
from pymodbus.framer.old_framer_base import ModbusFramer
from pymodbus.framer.old_framer_rtu import ModbusRtuFramer
from pymodbus.framer.old_framer_socket import ModbusSocketFramer
from pymodbus.framer.old_framer_tls import ModbusTlsFramer
FRAMER_NAME_TO_CLASS = {
FramerType.ASCII: ModbusAsciiFramer,
FramerType.RTU: ModbusRtuFramer,
FramerType.SOCKET: ModbusSocketFramer,
FramerType.TLS: ModbusTlsFramer,
}

View File

@@ -0,0 +1,84 @@
"""ModbusMessage layer.
is extending ModbusProtocol to handle receiving and sending of messsagees.
ModbusMessage provides a unified interface to send/receive Modbus requests/responses.
"""
from __future__ import annotations
from binascii import a2b_hex, b2a_hex
from pymodbus.framer.base import FramerBase
from pymodbus.logging import Log
class FramerAscii(FramerBase):
r"""Modbus ASCII Frame Controller.
[ Start ][ Dev id ][ Function ][ Data ][ LRC ][ End ]
1c 2c 2c N*2c 1c 2c
* data can be 1 - 2x252 chars
* end is "\\r\\n" (Carriage return line feed), however the line feed
character can be changed via a special command
* start is ":"
This framer is used for serial transmission. Unlike the RTU protocol,
the data in this framer is transferred in plain text ascii.
"""
START = b':'
END = b'\r\n'
MIN_SIZE = 10
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
buf_len = len(data)
used_len = 0
while True:
if buf_len - used_len < self.MIN_SIZE:
return used_len, 0, 0, self.EMPTY
buffer = data[used_len:]
if buffer[0:1] != self.START:
if (i := buffer.find(self.START)) == -1:
Log.debug("No frame start in data: {}, wait for data", data, ":hex")
return buf_len, 0, 0, self.EMPTY
used_len += i
continue
if (end := buffer.find(self.END)) == -1:
Log.debug("Incomplete frame: {} wait for more data", data, ":hex")
return used_len, 0, 0, self.EMPTY
dev_id = int(buffer[1:3], 16)
lrc = int(buffer[end - 2: end], 16)
msg = a2b_hex(buffer[1 : end - 2])
used_len += end + 2
if not self.check_LRC(msg, lrc):
Log.debug("LRC wrong in frame: {} skipping", data, ":hex")
continue
return used_len, dev_id, dev_id, msg[1:]
def encode(self, data: bytes, device_id: int, _tid: int) -> bytes:
"""Encode ADU."""
dev_id = device_id.to_bytes(1,'big')
checksum = self.compute_LRC(dev_id + data)
packet = (
self.START +
f"{device_id:02x}".encode() +
b2a_hex(data) +
f"{checksum:02x}".encode() +
self.END
).upper()
return packet
@classmethod
def compute_LRC(cls, data: bytes) -> int:
"""Use to compute the longitudinal redundancy check against a string."""
lrc = sum(int(a) for a in data) & 0xFF
lrc = (lrc ^ 0xFF) + 1
return lrc & 0xFF
@classmethod
def check_LRC(cls, data: bytes, check: int) -> bool:
"""Check if the passed in data matches the LRC."""
return cls.compute_LRC(data) == check

View File

@@ -0,0 +1,43 @@
"""Framer implementations.
The implementation is responsible for encoding/decoding requests/responses.
According to the selected type of modbus frame a prefix/suffix is added/removed
"""
from __future__ import annotations
from abc import abstractmethod
class FramerBase:
"""Intern base."""
EMPTY = b''
def __init__(self) -> None:
"""Initialize a ADU instance."""
def set_dev_ids(self, _dev_ids: list[int]):
"""Set/update allowed device ids."""
def set_fc_calc(self, _fc: int, _msg_size: int, _count_pos: int):
"""Set/Update function code information."""
@abstractmethod
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU.
returns:
used_len (int) or 0 to read more
transaction_id (int) or 0
device_id (int) or 0
modbus request/response (bytes)
"""
@abstractmethod
def encode(self, pdu: bytes, dev_id: int, tid: int) -> bytes:
"""Encode ADU.
returns:
modbus ADU (bytes)
"""

View File

@@ -0,0 +1,113 @@
"""Framing layer.
The framer layer is responsible for isolating/generating the request/request from
the frame (prefix - postfix)
According to the selected type of modbus frame a prefix/suffix is added/removed
This layer is also responsible for discarding invalid frames and frames for other slaves.
"""
from __future__ import annotations
from abc import abstractmethod
from enum import Enum
from pymodbus.framer.ascii import FramerAscii
from pymodbus.framer.raw import FramerRaw
from pymodbus.framer.rtu import FramerRTU
from pymodbus.framer.socket import FramerSocket
from pymodbus.framer.tls import FramerTLS
from pymodbus.transport.transport import CommParams, ModbusProtocol
class FramerType(str, Enum):
"""Type of Modbus frame."""
RAW = "raw" # only used for testing
ASCII = "ascii"
RTU = "rtu"
SOCKET = "socket"
TLS = "tls"
class Framer(ModbusProtocol):
"""Framer layer extending transport layer.
extends the ModbusProtocol to handle receiving and sending of complete modbus PDU.
When receiving:
- Secures full valid Modbus PDU is received (across multiple callbacks)
- Validates and removes Modbus prefix/suffix (CRC for serial, MBAP for others)
- Callback with pure request/response
- Skips invalid messagees
- Hunt for valid message (RTU type)
When sending:
- Add prefix/suffix to request/response (CRC for serial, MBAP for others)
- Call transport to send
The class is designed to take care of differences between the modbus message types,
and provide a neutral interface with pure requests/responses to/from the upper layers.
"""
def __init__(self,
framer_type: FramerType,
params: CommParams,
is_server: bool,
device_ids: list[int],
):
"""Initialize a framer instance.
:param framer_type: Modbus message type
:param params: parameter dataclass
:param is_server: true if object act as a server (listen/connect)
:param device_ids: list of device id to accept, 0 in list means broadcast.
"""
super().__init__(params, is_server)
self.device_ids = device_ids
self.broadcast: bool = (0 in device_ids)
self.handle = {
FramerType.RAW: FramerRaw(),
FramerType.ASCII: FramerAscii(),
FramerType.RTU: FramerRTU(),
FramerType.SOCKET: FramerSocket(),
FramerType.TLS: FramerTLS(),
}[framer_type]
def validate_device_id(self, dev_id: int) -> bool:
"""Check if device id is expected."""
return self.broadcast or (dev_id in self.device_ids)
def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data."""
tot_len = 0
buf_len = len(data)
while True:
used_len, tid, device_id, msg = self.handle.decode(data[tot_len:])
tot_len += used_len
if msg:
if self.broadcast or device_id in self.device_ids:
self.callback_request_response(msg, device_id, tid)
if tot_len == buf_len:
return tot_len
else:
return tot_len
@abstractmethod
def callback_request_response(self, data: bytes, device_id: int, tid: int) -> None:
"""Handle received modbus request/response."""
def build_send(self, data: bytes, device_id: int, tid: int, addr: tuple | None = None) -> None:
"""Send request/response.
:param data: non-empty bytes object with data to send.
:param device_id: device identifier (slave/unit)
:param tid: transaction id (0 if not used).
:param addr: optional addr, only used for UDP server.
"""
send_data = self.handle.encode(data, device_id, tid)
self.send(send_data, addr)

View File

@@ -0,0 +1,71 @@
"""Ascii_framer."""
from pymodbus.exceptions import ModbusIOException
from pymodbus.framer.old_framer_base import BYTE_ORDER, FRAME_HEADER, ModbusFramer
from pymodbus.logging import Log
from .ascii import FramerAscii
ASCII_FRAME_HEADER = BYTE_ORDER + FRAME_HEADER
# --------------------------------------------------------------------------- #
# Modbus ASCII olf framer
# --------------------------------------------------------------------------- #
class ModbusAsciiFramer(ModbusFramer):
r"""Modbus ASCII Frame Controller.
[ Start ][Address ][ Function ][ Data ][ LRC ][ End ]
1c 2c 2c Nc 2c 2c
* data can be 0 - 2x252 chars
* end is "\\r\\n" (Carriage return line feed), however the line feed
character can be changed via a special command
* start is ":"
This framer is used for serial transmission. Unlike the RTU protocol,
the data in this framer is transferred in plain text ascii.
"""
method = "ascii"
def __init__(self, decoder, client=None):
"""Initialize a new instance of the framer.
:param decoder: The decoder implementation to use
"""
super().__init__(decoder, client)
self._hsize = 0x02
self._start = b":"
self._end = b"\r\n"
self.message_handler = FramerAscii()
def decode_data(self, data):
"""Decode data."""
if len(data) > 1:
uid = int(data[1:3], 16)
fcode = int(data[3:5], 16)
return {"slave": uid, "fcode": fcode}
return {}
def frameProcessIncomingPacket(self, single, callback, slave, tid=None):
"""Process new packet pattern."""
while len(self._buffer):
used_len, tid, dev_id, data = self.message_handler.decode(self._buffer)
if not data:
if not used_len:
return
self._buffer = self._buffer[used_len :]
continue
self._header["uid"] = dev_id
if not self._validate_slave_id(slave, single):
Log.error("Not a valid slave id - {}, ignoring!!", dev_id)
self.resetFrame()
return
if (result := self.decoder.decode(data)) is None:
raise ModbusIOException("Unable to decode response")
self.populateResult(result)
self._buffer = self._buffer[used_len :]
self._header = {"uid": 0x00}
callback(result) # defer this

View File

@@ -0,0 +1,168 @@
"""Framer start."""
# pylint: disable=missing-type-doc
from __future__ import annotations
import time
from typing import TYPE_CHECKING, Any
from pymodbus.factory import ClientDecoder, ServerDecoder
from pymodbus.framer.base import FramerBase
from pymodbus.logging import Log
from pymodbus.pdu import ModbusRequest, ModbusResponse
if TYPE_CHECKING:
from pymodbus.client.base import ModbusBaseSyncClient
# 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: ClientDecoder | ServerDecoder,
client: ModbusBaseSyncClient,
) -> None:
"""Initialize a new instance of the framer.
:param decoder: The decoder implementation to use
"""
self.decoder = decoder
self.client = client
self._header: dict[str, Any]
self._reset_header()
self._buffer = b""
self.message_handler: FramerBase
def _reset_header(self) -> None:
self._header = {
"lrc": "0000",
"len": 0,
"uid": 0x00,
"tid": 0,
"pid": 0,
"crc": b"\x00\x00",
}
def _validate_slave_id(self, slaves: list, single: bool) -> bool:
"""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
def sendPacket(self, message: bytes):
"""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: int) -> bytes:
"""Receive packet from the bus.
With specified len
:param size: Number of bytes to read
:return:
"""
packet = self.client.recv(size)
self.client.last_frame_end = round(time.time(), 6)
return packet
def resetFrame(self):
"""Reset the entire message frame.
This allows us to skip ovver errors that may be in the stream.
It is hard to know if we are simply out of sync or if there is
an error in the stream as we have no way to check the start or
end of the message (python just doesn't have the resolution to
check for millisecond delays).
"""
Log.debug(
"Resetting frame - Current Frame in buffer - {}", self._buffer, ":hex"
)
self._buffer = b""
self._header = {
"lrc": "0000",
"crc": b"\x00\x00",
"len": 0,
"uid": 0x00,
"pid": 0,
"tid": 0,
}
def populateResult(self, result):
"""Populate the modbus result header.
The serial packets do not have any header information
that is copied.
:param result: The response packet
"""
result.slave_id = self._header.get("uid", 0)
result.transaction_id = self._header.get("tid", 0)
result.protocol_id = self._header.get("pid", 0)
def processIncomingPacket(self, data: bytes, callback, slave, single=False, tid=None):
"""Process new packet pattern.
This takes in a new request packet, adds it to the current
packet stream, and performs framing on it. That is, checks
for complete messages, and once found, will process all that
exist. This handles the case when we read N + 1 or 1 // N
messages at a time instead of 1.
The processed and decoded messages are pushed to the callback
function to process and send.
:param data: The new packet data
:param callback: The function to send results to
:param slave: Process if slave id matches, ignore otherwise (could be a
list of slave ids (server) or single slave id(client/server))
:param single: multiple slave ?
:param tid: transaction id
:raises ModbusIOException:
"""
Log.debug("Processing: {}", data, ":hex")
self._buffer += data
if self._buffer == b'':
return
if not isinstance(slave, (list, tuple)):
slave = [slave]
self.frameProcessIncomingPacket(single, callback, slave, tid=tid)
def frameProcessIncomingPacket(
self, _single, _callback, _slave, tid=None
) -> None:
"""Process new packet pattern."""
def buildPacket(self, message: ModbusRequest | ModbusResponse) -> bytes:
"""Create a ready to send modbus packet.
:param message: The populated request/response to send
"""
data = message.function_code.to_bytes(1,'big') + message.encode()
packet = self.message_handler.encode(data, message.slave_id, message.transaction_id)
return packet

View File

@@ -0,0 +1,227 @@
"""RTU framer."""
# pylint: disable=missing-type-doc
import struct
import time
from pymodbus.exceptions import ModbusIOException
from pymodbus.framer.old_framer_base import BYTE_ORDER, FRAME_HEADER, ModbusFramer
from pymodbus.framer.rtu import FramerRTU
from pymodbus.logging import Log
from pymodbus.utilities import ModbusTransactionState
RTU_FRAME_HEADER = BYTE_ORDER + FRAME_HEADER
# --------------------------------------------------------------------------- #
# Modbus RTU old Framer
# --------------------------------------------------------------------------- #
class ModbusRtuFramer(ModbusFramer):
"""Modbus RTU Frame controller.
[ Start Wait ] [Address ][ Function Code] [ Data ][ CRC ][ End Wait ]
3.5 chars 1b 1b Nb 2b 3.5 chars
Wait refers to the amount of time required to transmit at least x many
characters. In this case it is 3.5 characters. Also, if we receive a
wait of 1.5 characters at any point, we must trigger an error message.
Also, it appears as though this message is little endian. The logic is
simplified as the following::
block-on-read:
read until 3.5 delay
check for errors
decode
The following table is a listing of the baud wait times for the specified
baud rates::
------------------------------------------------------------------
Baud 1.5c (18 bits) 3.5c (38 bits)
------------------------------------------------------------------
1200 13333.3 us 31666.7 us
4800 3333.3 us 7916.7 us
9600 1666.7 us 3958.3 us
19200 833.3 us 1979.2 us
38400 416.7 us 989.6 us
------------------------------------------------------------------
1 Byte = start + 8 bits + parity + stop = 11 bits
(1/Baud)(bits) = delay seconds
"""
method = "rtu"
def __init__(self, decoder, client=None):
"""Initialize a new instance of the framer.
:param decoder: The decoder factory implementation to use
"""
super().__init__(decoder, client)
self._hsize = 0x01
self.function_codes = decoder.lookup.keys() if decoder else {}
self.message_handler = FramerRTU()
def decode_data(self, data):
"""Decode data."""
if len(data) > self._hsize:
uid = int(data[0])
fcode = int(data[1])
return {"slave": uid, "fcode": fcode}
return {}
def frameProcessIncomingPacket(self, _single, callback, slave, tid=None): # noqa: C901
"""Process new packet pattern."""
def is_frame_ready(self):
"""Check if we should continue decode logic."""
size = self._header.get("len", 0)
if not size and len(self._buffer) > self._hsize:
try:
self._header["uid"] = int(self._buffer[0])
self._header["tid"] = int(self._buffer[0])
self._header["tid"] = 0 # fix for now
func_code = int(self._buffer[1])
pdu_class = self.decoder.lookupPduClass(func_code)
size = pdu_class.calculateRtuFrameSize(self._buffer)
self._header["len"] = size
if len(self._buffer) < size:
raise IndexError
self._header["crc"] = self._buffer[size - 2 : size]
except IndexError:
return False
return len(self._buffer) >= size if size > 0 else False
def get_frame_start(self, slaves, broadcast, skip_cur_frame):
"""Scan buffer for a relevant frame start."""
start = 1 if skip_cur_frame else 0
if (buf_len := len(self._buffer)) < 4:
return False
for i in range(start, buf_len - 3): # <slave id><function code><crc 2 bytes>
if not broadcast and self._buffer[i] not in slaves:
continue
if (
self._buffer[i + 1] not in self.function_codes
and (self._buffer[i + 1] - 0x80) not in self.function_codes
):
continue
if i:
self._buffer = self._buffer[i:] # remove preceding trash.
return True
if buf_len > 3:
self._buffer = self._buffer[-3:]
return False
def check_frame(self):
"""Check if the next frame is available."""
try:
self._header["uid"] = int(self._buffer[0])
self._header["tid"] = int(self._buffer[0])
self._header["tid"] = 0 # fix for now
func_code = int(self._buffer[1])
pdu_class = self.decoder.lookupPduClass(func_code)
size = pdu_class.calculateRtuFrameSize(self._buffer)
self._header["len"] = size
if len(self._buffer) < size:
raise IndexError
self._header["crc"] = self._buffer[size - 2 : size]
frame_size = self._header["len"]
data = self._buffer[: frame_size - 2]
crc = self._header["crc"]
crc_val = (int(crc[0]) << 8) + int(crc[1])
return FramerRTU.check_CRC(data, crc_val)
except (IndexError, KeyError, struct.error):
return False
broadcast = not slave[0]
skip_cur_frame = False
while get_frame_start(self, slave, broadcast, skip_cur_frame):
self._header = {"uid": 0x00, "len": 0, "crc": b"\x00\x00"}
if not is_frame_ready(self):
Log.debug("Frame - not ready")
break
if not check_frame(self):
Log.debug("Frame check failed, ignoring!!")
x = self._buffer
self.resetFrame()
self._buffer: bytes = x
skip_cur_frame = True
continue
start = self._hsize
end = self._header["len"] - 2
buffer = self._buffer[start:end]
if end > 0:
Log.debug("Getting Frame - {}", buffer, ":hex")
data = buffer
else:
data = b""
if (result := self.decoder.decode(data)) is None:
raise ModbusIOException("Unable to decode request")
result.slave_id = self._header["uid"]
result.transaction_id = 0
self._buffer = self._buffer[self._header["len"] :]
Log.debug("Frame advanced, resetting header!!")
callback(result) # defer or push to a thread?
def buildPacket(self, message):
"""Create a ready to send modbus packet.
:param message: The populated request/response to send
"""
packet = super().buildPacket(message)
# Ensure that transaction is actually the slave id for serial comms
message.transaction_id = 0
return packet
def sendPacket(self, message: bytes) -> int:
"""Send packets on the bus with 3.5char delay between frames.
:param message: Message to be sent over the bus
:return:
"""
super().resetFrame()
start = time.time()
if hasattr(self.client,"ctx"):
timeout = start + self.client.ctx.comm_params.timeout_connect
else:
timeout = start + self.client.comm_params.timeout_connect
while self.client.state != ModbusTransactionState.IDLE:
if self.client.state == ModbusTransactionState.TRANSACTION_COMPLETE:
timestamp = round(time.time(), 6)
Log.debug(
"Changing state to IDLE - Last Frame End - {} Current Time stamp - {}",
self.client.last_frame_end,
timestamp,
)
if self.client.last_frame_end:
idle_time = self.client.idle_time()
if round(timestamp - idle_time, 6) <= self.client.silent_interval:
Log.debug(
"Waiting for 3.5 char before next send - {} ms",
self.client.silent_interval * 1000,
)
time.sleep(self.client.silent_interval)
else:
# Recovering from last error ??
time.sleep(self.client.silent_interval)
self.client.state = ModbusTransactionState.IDLE
elif self.client.state == ModbusTransactionState.RETRYING:
# Simple lets settle down!!!
# To check for higher baudrates
time.sleep(self.client.comm_params.timeout_connect)
break
elif time.time() > timeout:
Log.debug(
"Spent more time than the read time out, "
"resetting the transaction to IDLE"
)
self.client.state = ModbusTransactionState.IDLE
else:
Log.debug("Sleeping")
time.sleep(self.client.silent_interval)
size = self.client.send(message)
self.client.last_frame_end = round(time.time(), 6)
return size

View File

@@ -0,0 +1,96 @@
"""Socket framer."""
import struct
from pymodbus.exceptions import (
ModbusIOException,
)
from pymodbus.framer.old_framer_base import SOCKET_FRAME_HEADER, ModbusFramer
from pymodbus.framer.socket import FramerSocket
from pymodbus.logging import Log
# --------------------------------------------------------------------------- #
# Modbus TCP old framer
# --------------------------------------------------------------------------- #
class ModbusSocketFramer(ModbusFramer):
"""Modbus Socket Frame controller.
Before each modbus TCP message is an MBAP header which is used as a
message frame. It allows us to easily separate messages as follows::
[ MBAP Header ] [ Function Code] [ Data ] \
[ tid ][ pid ][ length ][ uid ]
2b 2b 2b 1b 1b Nb
while len(message) > 0:
tid, pid, length`, uid = struct.unpack(">HHHB", message)
request = message[0:7 + length - 1`]
message = [7 + length - 1:]
* length = uid + function code + data
* The -1 is to account for the uid byte
"""
method = "socket"
def __init__(self, decoder, client=None):
"""Initialize a new instance of the framer.
:param decoder: The decoder factory implementation to use
"""
super().__init__(decoder, client)
self._hsize = 0x07
self.message_handler = FramerSocket()
def decode_data(self, data):
"""Decode data."""
if len(data) > self._hsize:
tid, pid, length, uid, fcode = struct.unpack(
SOCKET_FRAME_HEADER, data[0 : self._hsize + 1]
)
return {
"tid": tid,
"pid": pid,
"length": length,
"slave": uid,
"fcode": fcode,
}
return {}
def frameProcessIncomingPacket(self, single, callback, slave, tid=None):
"""Process new packet pattern.
This takes in a new request packet, adds it to the current
packet stream, and performs framing on it. That is, checks
for complete messages, and once found, will process all that
exist. This handles the case when we read N + 1 or 1 // N
messages at a time instead of 1.
The processed and decoded messages are pushed to the callback
function to process and send.
"""
while True:
if self._buffer == b'':
return
used_len, use_tid, dev_id, data = self.message_handler.decode(self._buffer)
if not data:
return
self._header["uid"] = dev_id
self._header["tid"] = use_tid
self._header["pid"] = 0
if not self._validate_slave_id(slave, single):
Log.debug("Not a valid slave id - {}, ignoring!!", dev_id)
self.resetFrame()
return
if (result := self.decoder.decode(data)) is None:
self.resetFrame()
raise ModbusIOException("Unable to decode request")
self.populateResult(result)
self._buffer: bytes = self._buffer[used_len:]
self._reset_header()
if tid and tid != result.transaction_id:
self.resetFrame()
else:
callback(result) # defer or push to a thread?

View File

@@ -0,0 +1,69 @@
"""TLS framer."""
import struct
from time import sleep
from pymodbus.exceptions import (
ModbusIOException,
)
from pymodbus.framer.old_framer_base import TLS_FRAME_HEADER, ModbusFramer
from pymodbus.framer.tls import FramerTLS
# --------------------------------------------------------------------------- #
# Modbus TLS old framer
# --------------------------------------------------------------------------- #
class ModbusTlsFramer(ModbusFramer):
"""Modbus TLS Frame controller.
No prefix MBAP header before decrypted PDU is used as a message frame for
Modbus Security Application Protocol. It allows us to easily separate
decrypted messages which is PDU as follows:
[ Function Code] [ Data ]
1b Nb
"""
method = "tls"
def __init__(self, decoder, client=None):
"""Initialize a new instance of the framer.
:param decoder: The decoder factory implementation to use
"""
super().__init__(decoder, client)
self._hsize = 0x0
self.message_handler = FramerTLS()
def decode_data(self, data):
"""Decode data."""
if len(data) > self._hsize:
(fcode,) = struct.unpack(TLS_FRAME_HEADER, data[0 : self._hsize + 1])
return {"fcode": fcode}
return {}
def recvPacket(self, size):
"""Receive packet from the bus."""
sleep(0.5)
return super().recvPacket(size)
def frameProcessIncomingPacket(self, _single, callback, _slave, tid=None):
"""Process new packet pattern."""
# no slave id for Modbus Security Application Protocol
while True:
used_len, use_tid, dev_id, data = self.message_handler.decode(self._buffer)
if not data:
return
self._header["uid"] = dev_id
self._header["tid"] = use_tid
self._header["pid"] = 0
if (result := self.decoder.decode(data)) is None:
self.resetFrame()
raise ModbusIOException("Unable to decode request")
self.populateResult(result)
self._buffer: bytes = self._buffer[used_len:]
self._reset_header()
callback(result) # defer or push to a thread?

View File

@@ -0,0 +1,32 @@
"""Modbus Raw (passthrough) implementation."""
from __future__ import annotations
from pymodbus.framer.base import FramerBase
from pymodbus.logging import Log
class FramerRaw(FramerBase):
r"""Modbus RAW Frame Controller.
[ Device id ][Transaction id ][ Data ]
1b 2b Nb
* data can be 0 - X bytes
This framer is used for non modbus communication and testing purposes.
"""
MIN_SIZE = 3
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
if len(data) < self.MIN_SIZE:
Log.debug("Short frame: {} wait for more data", data, ":hex")
return 0, 0, 0, self.EMPTY
dev_id = int(data[0])
tid = int(data[1])
return len(data), dev_id, tid, data[2:]
def encode(self, pdu: bytes, dev_id: int, tid: int) -> bytes:
"""Encode ADU."""
return dev_id.to_bytes(1, 'big') + tid.to_bytes(1, 'big') + pdu

View File

@@ -0,0 +1,153 @@
"""Modbus RTU frame implementation."""
from __future__ import annotations
from collections import namedtuple
from pymodbus.framer.base import FramerBase
from pymodbus.logging import Log
class FramerRTU(FramerBase):
"""Modbus RTU frame type.
[ Start Wait ] [Address ][ Function Code] [ Data ][ CRC ]
3.5 chars 1b 1b Nb 2b
* Note: due to the USB converter and the OS drivers, timing cannot be quaranteed
neither when receiving nor when sending.
Decoding is a complicated process because the RTU frame does not have a fixed prefix
only suffix, therefore it is necessary to decode the content (PDU) to get length etc.
There are some protocol restrictions that help with the detection.
For client:
- a request causes 1 response !
- Multiple requests are NOT allowed (master-slave protocol)
- the server will not retransmit responses
this means decoding is always exactly 1 frame (response)
For server (Single device)
- only 1 request allowed (master-slave) protocol
- the client (master) may retransmit but in larger time intervals
this means decoding is always exactly 1 frame (request)
For server (Multidrop line --> devices in parallel)
- only 1 request allowed (master-slave) protocol
- other devices will send responses
- the client (master) may retransmit but in larger time intervals
this means decoding is always exactly 1 frame request, however some requests
will be for unknown slaves, which must be ignored together with the
response from the unknown slave.
Recovery from bad cabling and unstable USB etc is important,
the following scenarios is possible:
- garble data before frame
- garble data in frame
- garble data after frame
- data in frame garbled (wrong CRC)
decoding assumes the frame is sound, and if not enters a hunting mode.
The 3.5 byte transmission time at the slowest speed 1.200Bps is 31ms.
Device drivers will typically flush buffer after 10ms of silence.
If no data is received for 50ms the transmission / frame can be considered
complete.
"""
MIN_SIZE = 5
FC_LEN = namedtuple("FC_LEN", "req_len req_bytepos resp_len resp_bytepos")
def __init__(self) -> None:
"""Initialize a ADU instance."""
super().__init__()
self.fc_len: dict[int, FramerRTU.FC_LEN] = {}
@classmethod
def generate_crc16_table(cls) -> list[int]:
"""Generate a crc16 lookup table.
.. note:: This will only be generated once
"""
result = []
for byte in range(256):
crc = 0x0000
for _ in range(8):
if (byte ^ crc) & 0x0001:
crc = (crc >> 1) ^ 0xA001
else:
crc >>= 1
byte >>= 1
result.append(crc)
return result
crc16_table: list[int] = [0]
def setup_fc_len(self, _fc: int,
_req_len: int, _req_byte_pos: int,
_resp_len: int, _resp_byte_pos: int
):
"""Define request/response lengths pr function code."""
return
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
if (buf_len := len(data)) < self.MIN_SIZE:
Log.debug("Short frame: {} wait for more data", data, ":hex")
return 0, 0, 0, b''
i = -1
try:
while True:
i += 1
if i > buf_len - self.MIN_SIZE + 1:
break
dev_id = int(data[i])
fc_len = 5
msg_len = fc_len -2 if fc_len > 0 else int(data[i-fc_len])-fc_len+1
if msg_len + i + 2 > buf_len:
break
crc_val = (int(data[i+msg_len]) << 8) + int(data[i+msg_len+1])
if not self.check_CRC(data[i:i+msg_len], crc_val):
Log.debug("Skipping frame CRC with len {} at index {}!", msg_len, i)
raise KeyError
return i+msg_len+2, dev_id, dev_id, data[i+1:i+msg_len]
except KeyError:
i = buf_len
return i, 0, 0, b''
def encode(self, pdu: bytes, device_id: int, _tid: int) -> bytes:
"""Encode ADU."""
packet = device_id.to_bytes(1,'big') + pdu
return packet + FramerRTU.compute_CRC(packet).to_bytes(2,'big')
@classmethod
def check_CRC(cls, data: bytes, check: int) -> bool:
"""Check if the data matches the passed in CRC.
:param data: The data to create a crc16 of
:param check: The CRC to validate
:returns: True if matched, False otherwise
"""
return cls.compute_CRC(data) == check
@classmethod
def compute_CRC(cls, data: bytes) -> int:
"""Compute a crc16 on the passed in bytes.
The difference between modbus's crc16 and a normal crc16
is that modbus starts the crc value out at 0xffff.
:param data: The data to create a crc16 of
:returns: The calculated CRC
"""
crc = 0xFFFF
for data_byte in data:
idx = cls.crc16_table[(crc ^ int(data_byte)) & 0xFF]
crc = ((crc >> 8) & 0xFF) ^ idx
swapped = ((crc << 8) & 0xFF00) | ((crc >> 8) & 0x00FF)
return swapped
FramerRTU.crc16_table = FramerRTU.generate_crc16_table()

View File

@@ -0,0 +1,44 @@
"""Modbus Socket frame implementation."""
from __future__ import annotations
from pymodbus.framer.base import FramerBase
from pymodbus.logging import Log
class FramerSocket(FramerBase):
"""Modbus Socket frame type.
[ MBAP Header ] [ Function Code] [ Data ]
[ tid ][ pid ][ length ][ uid ]
2b 2b 2b 1b 1b Nb
* length = uid + function code + data
"""
MIN_SIZE = 8
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
if (used_len := len(data)) < self.MIN_SIZE:
Log.debug("Very short frame (NO MBAP): {} wait for more data", data, ":hex")
return 0, 0, 0, self.EMPTY
msg_tid = int.from_bytes(data[0:2], 'big')
msg_len = int.from_bytes(data[4:6], 'big') + 6
msg_dev = int(data[6])
if used_len < msg_len:
Log.debug("Short frame: {} wait for more data", data, ":hex")
return 0, 0, 0, self.EMPTY
if msg_len == 8 and used_len == 9:
msg_len = 9
return msg_len, msg_tid, msg_dev, data[7:msg_len]
def encode(self, pdu: bytes, device_id: int, tid: int) -> bytes:
"""Encode ADU."""
packet = (
tid.to_bytes(2, 'big') +
b'\x00\x00' +
(len(pdu) + 1).to_bytes(2, 'big') +
device_id.to_bytes(1, 'big') +
pdu
)
return packet

View File

@@ -0,0 +1,20 @@
"""Modbus TLS frame implementation."""
from __future__ import annotations
from pymodbus.framer.base import FramerBase
class FramerTLS(FramerBase):
"""Modbus TLS frame type.
[ Function Code] [ Data ]
1b Nb
"""
def decode(self, data: bytes) -> tuple[int, int, int, bytes]:
"""Decode ADU."""
return len(data), 0, 0, data
def encode(self, pdu: bytes, _device_id: int, _tid: int) -> bytes:
"""Encode ADU."""
return pdu

View File

@@ -0,0 +1,121 @@
"""Pymodbus: Modbus Protocol Implementation.
Released under the BSD license
"""
from __future__ import annotations
import logging
from binascii import b2a_hex
from logging import NullHandler as __null
from pymodbus.utilities import hexlify_packets
# ---------------------------------------------------------------------------#
# Block unhandled logging
# ---------------------------------------------------------------------------#
logging.getLogger("pymodbus_internal").addHandler(__null())
def pymodbus_apply_logging_config(
level: str | int = logging.DEBUG, log_file_name: str | None = None
):
"""Apply basic logging configuration used by default by Pymodbus maintainers.
:param level: (optional) set log level, if not set it is inherited.
:param log_file_name: (optional) log additional to file
Please call this function to format logging appropriately when opening issues.
"""
if isinstance(level, str):
level = level.upper()
Log.apply_logging_config(level, log_file_name)
class Log:
"""Class to hide logging complexity.
:meta private:
"""
_logger = logging.getLogger(__name__)
@classmethod
def apply_logging_config(cls, level, log_file_name):
"""Apply basic logging configuration."""
if level == logging.NOTSET:
level = cls._logger.getEffectiveLevel()
if isinstance(level, str):
level = level.upper()
log_stream_handler = logging.StreamHandler()
log_formatter = logging.Formatter(
"%(asctime)s %(levelname)-5s %(module)s:%(lineno)s %(message)s"
)
log_stream_handler.setFormatter(log_formatter)
cls._logger.addHandler(log_stream_handler)
if log_file_name:
log_file_handler = logging.FileHandler(log_file_name)
log_file_handler.setFormatter(log_formatter)
cls._logger.addHandler(log_file_handler)
cls.setLevel(level)
@classmethod
def setLevel(cls, level):
"""Apply basic logging level."""
cls._logger.setLevel(level)
@classmethod
def build_msg(cls, txt, *args):
"""Build message."""
string_args = []
count_args = len(args) - 1
skip = False
for i in range(count_args + 1):
if skip:
skip = False
continue
if (
i < count_args
and isinstance(args[i + 1], str)
and args[i + 1][0] == ":"
):
if args[i + 1] == ":hex":
string_args.append(hexlify_packets(args[i]))
elif args[i + 1] == ":str":
string_args.append(str(args[i]))
elif args[i + 1] == ":b2a":
string_args.append(b2a_hex(args[i]))
skip = True
else:
string_args.append(args[i])
return txt.format(*string_args)
@classmethod
def info(cls, txt, *args):
"""Log info messages."""
if cls._logger.isEnabledFor(logging.INFO):
cls._logger.info(cls.build_msg(txt, *args))
@classmethod
def debug(cls, txt, *args):
"""Log debug messages."""
if cls._logger.isEnabledFor(logging.DEBUG):
cls._logger.debug(cls.build_msg(txt, *args))
@classmethod
def warning(cls, txt, *args):
"""Log warning messages."""
if cls._logger.isEnabledFor(logging.WARNING):
cls._logger.warning(cls.build_msg(txt, *args))
@classmethod
def error(cls, txt, *args):
"""Log error messages."""
if cls._logger.isEnabledFor(logging.ERROR):
cls._logger.error(cls.build_msg(txt, *args))
@classmethod
def critical(cls, txt, *args):
"""Log critical messages."""
if cls._logger.isEnabledFor(logging.CRITICAL):
cls._logger.critical(cls.build_msg(txt, *args))

View File

@@ -0,0 +1,455 @@
"""Modbus Payload Builders.
A collection of utilities for building and decoding
modbus messages payloads.
"""
from __future__ import annotations
__all__ = [
"BinaryPayloadBuilder",
"BinaryPayloadDecoder",
]
from array import array
# pylint: disable=missing-type-doc
from struct import pack, unpack
from pymodbus.constants import Endian
from pymodbus.exceptions import ParameterException
from pymodbus.logging import Log
from pymodbus.utilities import (
pack_bitstring,
unpack_bitstring,
)
class BinaryPayloadBuilder:
"""A utility that helps build payload messages to be written with the various modbus messages.
It really is just a simple wrapper around the struct module,
however it saves time looking up the format strings.
What follows is a simple example::
builder = BinaryPayloadBuilder(byteorder=Endian.Little)
builder.add_8bit_uint(1)
builder.add_16bit_uint(2)
payload = builder.build()
"""
def __init__(
self, payload=None, byteorder=Endian.LITTLE, wordorder=Endian.BIG, repack=False
):
"""Initialize a new instance of the payload builder.
:param payload: Raw binary payload data to initialize with
:param byteorder: The endianness of the bytes in the words
:param wordorder: The endianness of the word (when wordcount is >= 2)
:param repack: Repack the provided payload based on BO
"""
self._payload = payload or []
self._byteorder = byteorder
self._wordorder = wordorder
self._repack = repack
def _pack_words(self, fstring: str, value) -> bytes:
"""Pack words based on the word order and byte order.
# ---------------------------------------------- #
# pack in to network ordered value #
# unpack in to network ordered unsigned integer #
# Change Word order if little endian word order #
# Pack values back based on correct byte order #
# ---------------------------------------------- #
:param fstring:
:param value: Value to be packed
:return:
"""
value = pack(f"!{fstring}", value)
if Endian.LITTLE in {self._byteorder, self._wordorder}:
value = array("H", value)
if self._byteorder == Endian.LITTLE:
value.byteswap()
if self._wordorder == Endian.LITTLE:
value.reverse()
value = value.tobytes()
return value
def encode(self) -> bytes:
"""Get the payload buffer encoded in bytes."""
return b"".join(self._payload)
def __str__(self) -> str:
"""Return the payload buffer as a string.
:returns: The payload buffer as a string
"""
return self.encode().decode("utf-8")
def reset(self) -> None:
"""Reset the payload buffer."""
self._payload = []
def to_registers(self):
"""Convert the payload buffer to register layout that can be used as a context block.
:returns: The register layout to use as a block
"""
# fstring = self._byteorder+"H"
fstring = "!H"
payload = self.build()
if self._repack:
payload = [unpack(self._byteorder + "H", value)[0] for value in payload]
else:
payload = [unpack(fstring, value)[0] for value in payload]
Log.debug("{}", payload)
return payload
def to_coils(self) -> list[bool]:
"""Convert the payload buffer into a coil layout that can be used as a context block.
:returns: The coil layout to use as a block
"""
payload = self.to_registers()
coils = [bool(int(bit)) for reg in payload for bit in format(reg, "016b")]
return coils
def build(self) -> list[bytes]:
"""Return the payload buffer as a list.
This list is two bytes per element and can
thus be treated as a list of registers.
:returns: The payload buffer as a list
"""
buffer = self.encode()
length = len(buffer)
buffer += b"\x00" * (length % 2)
return [buffer[i : i + 2] for i in range(0, length, 2)]
def add_bits(self, values: list[bool]) -> None:
"""Add a collection of bits to be encoded.
If these are less than a multiple of eight,
they will be left padded with 0 bits to make
it so.
:param values: The value to add to the buffer
"""
value = pack_bitstring(values)
self._payload.append(value)
def add_8bit_uint(self, value: int) -> None:
"""Add a 8 bit unsigned int to the buffer.
:param value: The value to add to the buffer
"""
fstring = self._byteorder + "B"
self._payload.append(pack(fstring, value))
def add_16bit_uint(self, value: int) -> None:
"""Add a 16 bit unsigned int to the buffer.
:param value: The value to add to the buffer
"""
fstring = self._byteorder + "H"
self._payload.append(pack(fstring, value))
def add_32bit_uint(self, value: int) -> None:
"""Add a 32 bit unsigned int to the buffer.
:param value: The value to add to the buffer
"""
fstring = "I"
# fstring = self._byteorder + "I"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_64bit_uint(self, value: int) -> None:
"""Add a 64 bit unsigned int to the buffer.
:param value: The value to add to the buffer
"""
fstring = "Q"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_8bit_int(self, value: int) -> None:
"""Add a 8 bit signed int to the buffer.
:param value: The value to add to the buffer
"""
fstring = self._byteorder + "b"
self._payload.append(pack(fstring, value))
def add_16bit_int(self, value: int) -> None:
"""Add a 16 bit signed int to the buffer.
:param value: The value to add to the buffer
"""
fstring = self._byteorder + "h"
self._payload.append(pack(fstring, value))
def add_32bit_int(self, value: int) -> None:
"""Add a 32 bit signed int to the buffer.
:param value: The value to add to the buffer
"""
fstring = "i"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_64bit_int(self, value: int) -> None:
"""Add a 64 bit signed int to the buffer.
:param value: The value to add to the buffer
"""
fstring = "q"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_16bit_float(self, value: float) -> None:
"""Add a 16 bit float to the buffer.
:param value: The value to add to the buffer
"""
fstring = "e"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_32bit_float(self, value: float) -> None:
"""Add a 32 bit float to the buffer.
:param value: The value to add to the buffer
"""
fstring = "f"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_64bit_float(self, value: float) -> None:
"""Add a 64 bit float(double) to the buffer.
:param value: The value to add to the buffer
"""
fstring = "d"
p_string = self._pack_words(fstring, value)
self._payload.append(p_string)
def add_string(self, value: str) -> None:
"""Add a string to the buffer.
:param value: The value to add to the buffer
"""
fstring = self._byteorder + str(len(value)) + "s"
self._payload.append(pack(fstring, value.encode()))
class BinaryPayloadDecoder:
"""A utility that helps decode payload messages from a modbus response message.
It really is just a simple wrapper around
the struct module, however it saves time looking up the format
strings. What follows is a simple example::
decoder = BinaryPayloadDecoder(payload)
first = decoder.decode_8bit_uint()
second = decoder.decode_16bit_uint()
"""
def __init__(self, payload, byteorder=Endian.LITTLE, wordorder=Endian.BIG):
"""Initialize a new payload decoder.
:param payload: The payload to decode with
:param byteorder: The endianness of the payload
:param wordorder: The endianness of the word (when wordcount is >= 2)
"""
self._payload = payload
self._pointer = 0x00
self._byteorder = byteorder
self._wordorder = wordorder
@classmethod
def fromRegisters(
cls,
registers,
byteorder=Endian.LITTLE,
wordorder=Endian.BIG,
):
"""Initialize a payload decoder.
With the result of reading a collection of registers from a modbus device.
The registers are treated as a list of 2 byte values.
We have to do this because of how the data has already
been decoded by the rest of the library.
:param registers: The register results to initialize with
:param byteorder: The Byte order of each word
:param wordorder: The endianness of the word (when wordcount is >= 2)
:returns: An initialized PayloadDecoder
:raises ParameterException:
"""
Log.debug("{}", registers)
if isinstance(registers, list): # repack into flat binary
payload = pack(f"!{len(registers)}H", *registers)
return cls(payload, byteorder, wordorder)
raise ParameterException("Invalid collection of registers supplied")
@classmethod
def bit_chunks(cls, coils, size=8):
"""Return bit chunks."""
chunks = [coils[i : i + size] for i in range(0, len(coils), size)]
return chunks
@classmethod
def fromCoils(
cls,
coils,
byteorder=Endian.LITTLE,
_wordorder=Endian.BIG,
):
"""Initialize a payload decoder with the result of reading of coils."""
if isinstance(coils, list):
payload = b""
if padding := len(coils) % 8: # Pad zeros
extra = [False] * padding
coils = extra + coils
chunks = cls.bit_chunks(coils)
for chunk in chunks:
payload += pack_bitstring(chunk[::-1])
return cls(payload, byteorder)
raise ParameterException("Invalid collection of coils supplied")
def _unpack_words(self, handle) -> bytes:
"""Unpack words based on the word order and byte order.
# ---------------------------------------------- #
# Unpack in to network ordered unsigned integer #
# Change Word order if little endian word order #
# Pack values back based on correct byte order #
# ---------------------------------------------- #
:param fstring:
:param handle: Value to be unpacked
:return:
"""
if Endian.LITTLE in {self._byteorder, self._wordorder}:
handle = array("H", handle)
if self._byteorder == Endian.LITTLE:
handle.byteswap()
if self._wordorder == Endian.LITTLE:
handle.reverse()
handle = handle.tobytes()
Log.debug("handle: {}", handle)
return handle
def reset(self):
"""Reset the decoder pointer back to the start."""
self._pointer = 0x00
def decode_8bit_uint(self):
"""Decode a 8 bit unsigned int from the buffer."""
self._pointer += 1
fstring = self._byteorder + "B"
handle = self._payload[self._pointer - 1 : self._pointer]
return unpack(fstring, handle)[0]
def decode_bits(self, package_len=1):
"""Decode a byte worth of bits from the buffer."""
self._pointer += package_len
# fstring = self._endian + "B"
handle = self._payload[self._pointer - 1 : self._pointer]
return unpack_bitstring(handle)
def decode_16bit_uint(self):
"""Decode a 16 bit unsigned int from the buffer."""
self._pointer += 2
fstring = self._byteorder + "H"
handle = self._payload[self._pointer - 2 : self._pointer]
return unpack(fstring, handle)[0]
def decode_32bit_uint(self):
"""Decode a 32 bit unsigned int from the buffer."""
self._pointer += 4
fstring = "I"
handle = self._payload[self._pointer - 4 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_64bit_uint(self):
"""Decode a 64 bit unsigned int from the buffer."""
self._pointer += 8
fstring = "Q"
handle = self._payload[self._pointer - 8 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_8bit_int(self):
"""Decode a 8 bit signed int from the buffer."""
self._pointer += 1
fstring = self._byteorder + "b"
handle = self._payload[self._pointer - 1 : self._pointer]
return unpack(fstring, handle)[0]
def decode_16bit_int(self):
"""Decode a 16 bit signed int from the buffer."""
self._pointer += 2
fstring = self._byteorder + "h"
handle = self._payload[self._pointer - 2 : self._pointer]
return unpack(fstring, handle)[0]
def decode_32bit_int(self):
"""Decode a 32 bit signed int from the buffer."""
self._pointer += 4
fstring = "i"
handle = self._payload[self._pointer - 4 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_64bit_int(self):
"""Decode a 64 bit signed int from the buffer."""
self._pointer += 8
fstring = "q"
handle = self._payload[self._pointer - 8 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_16bit_float(self):
"""Decode a 16 bit float from the buffer."""
self._pointer += 2
fstring = "e"
handle = self._payload[self._pointer - 2 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_32bit_float(self):
"""Decode a 32 bit float from the buffer."""
self._pointer += 4
fstring = "f"
handle = self._payload[self._pointer - 4 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_64bit_float(self):
"""Decode a 64 bit float(double) from the buffer."""
self._pointer += 8
fstring = "d"
handle = self._payload[self._pointer - 8 : self._pointer]
handle = self._unpack_words(handle)
return unpack("!" + fstring, handle)[0]
def decode_string(self, size=1):
"""Decode a string from the buffer.
:param size: The size of the string to decode
"""
self._pointer += size
return self._payload[self._pointer - size : self._pointer]
def skip_bytes(self, nbytes):
"""Skip n bytes in the buffer.
:param nbytes: The number of bytes to skip
"""
self._pointer += nbytes

View File

@@ -0,0 +1,18 @@
"""Framer."""
__all__ = [
"ExceptionResponse",
"IllegalFunctionRequest",
"ModbusExceptions",
"ModbusPDU",
"ModbusRequest",
"ModbusResponse",
]
from pymodbus.pdu.pdu import (
ExceptionResponse,
IllegalFunctionRequest,
ModbusExceptions,
ModbusPDU,
ModbusRequest,
ModbusResponse,
)

View File

@@ -0,0 +1,262 @@
"""Bit Reading Request/Response messages."""
# pylint: disable=missing-type-doc
import struct
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.utilities import pack_bitstring, unpack_bitstring
class ReadBitsRequestBase(ModbusRequest):
"""Base class for Messages Requesting bit values."""
_rtu_frame_size = 8
def __init__(self, address, count, slave, transaction, protocol, skip_encode):
"""Initialize the read request data.
:param address: The start address to read from
:param count: The number of bits after "address" to read
:param slave: Modbus slave slave ID
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
self.count = count
def encode(self):
"""Encode a request pdu.
:returns: The encoded pdu
"""
return struct.pack(">HH", self.address, self.count)
def decode(self, data):
"""Decode a request pdu.
:param data: The packet data to decode
"""
self.address, self.count = struct.unpack(">HH", data)
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Byte Count(1 byte) + Quantity of Coils (n Bytes)/8,
if the remainder is different of 0 then N = N+1
:return:
"""
count = self.count // 8
if self.count % 8:
count += 1
return 1 + 1 + count
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"ReadBitRequest({self.address},{self.count})"
class ReadBitsResponseBase(ModbusResponse):
"""Base class for Messages responding to bit-reading values.
The requested bits can be found in the .bits list.
"""
_rtu_byte_count_pos = 2
def __init__(self, values, slave, transaction, protocol, skip_encode):
"""Initialize a new instance.
:param values: The requested values to be returned
:param slave: Modbus slave slave ID
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
#: A list of booleans representing bit values
self.bits = values or []
def encode(self):
"""Encode response pdu.
:returns: The encoded packet message
"""
result = pack_bitstring(self.bits)
packet = struct.pack(">B", len(result)) + result
return packet
def decode(self, data):
"""Decode response pdu.
:param data: The packet data to decode
"""
self.byte_count = int(data[0]) # pylint: disable=attribute-defined-outside-init
self.bits = unpack_bitstring(data[1:])
def setBit(self, address, value=1):
"""Set the specified bit.
:param address: The bit to set
:param value: The value to set the bit to
"""
self.bits[address] = bool(value)
def resetBit(self, address):
"""Set the specified bit to 0.
:param address: The bit to reset
"""
self.setBit(address, 0)
def getBit(self, address):
"""Get the specified bit's value.
:param address: The bit to query
:returns: The value of the requested bit
"""
return self.bits[address]
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"{self.__class__.__name__}({len(self.bits)})"
class ReadCoilsRequest(ReadBitsRequestBase):
"""This function code is used to read from 1 to 2000(0x7d0) contiguous status of coils in a remote device.
The Request PDU specifies the starting
address, ie the address of the first coil specified, and the number of
coils. In the PDU Coils are addressed starting at zero. Therefore coils
numbered 1-16 are addressed as 0-15.
"""
function_code = 1
function_code_name = "read_coils"
def __init__(self, address=None, count=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start reading from
:param count: The number of bits to read
:param slave: Modbus slave slave ID
"""
ReadBitsRequestBase.__init__(self, address, count, slave, transaction, protocol, skip_encode)
async def execute(self, context):
"""Run a read coils request against a datastore.
Before running the request, we make sure that the request is in
the max valid range (0x001-0x7d0). Next we make sure that the
request is valid against the current datastore.
:param context: The datastore to request from
:returns: An initialized :py:class:`~pymodbus.register_read_message.ReadCoilsResponse`, or an :py:class:`~pymodbus.pdu.ExceptionResponse` if an error occurred
"""
if not (1 <= self.count <= 0x7D0):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
values = await context.async_getValues(
self.function_code, self.address, self.count
)
return ReadCoilsResponse(values)
class ReadCoilsResponse(ReadBitsResponseBase):
"""The coils in the response message are packed as one coil per bit of the data field.
Status is indicated as 1= ON and 0= OFF. The LSB of the
first data byte contains the output addressed in the query. The other
coils follow toward the high order end of this byte, and from low order
to high order in subsequent bytes.
If the returned output quantity is not a multiple of eight, the
remaining bits in the final data byte will be padded with zeros
(toward the high order end of the byte). The Byte Count field specifies
the quantity of complete bytes of data.
The requested coils can be found in boolean form in the .bits list.
"""
function_code = 1
def __init__(self, values=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The request values to respond with
:param slave: Modbus slave slave ID
"""
ReadBitsResponseBase.__init__(self, values, slave, transaction, protocol, skip_encode)
class ReadDiscreteInputsRequest(ReadBitsRequestBase):
"""This function code is used to read from 1 to 2000(0x7d0).
Contiguous status of discrete inputs in a remote device. The Request PDU specifies the
starting address, ie the address of the first input specified, and the
number of inputs. In the PDU Discrete Inputs are addressed starting at
zero. Therefore Discrete inputs numbered 1-16 are addressed as 0-15.
"""
function_code = 2
function_code_name = "read_discrete_input"
def __init__(self, address=None, count=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start reading from
:param count: The number of bits to read
:param slave: Modbus slave slave ID
"""
ReadBitsRequestBase.__init__(self, address, count, slave, transaction, protocol, skip_encode)
async def execute(self, context):
"""Run a read discrete input request against a datastore.
Before running the request, we make sure that the request is in
the max valid range (0x001-0x7d0). Next we make sure that the
request is valid against the current datastore.
:param context: The datastore to request from
:returns: An initialized :py:class:`~pymodbus.register_read_message.ReadDiscreteInputsResponse`, or an :py:class:`~pymodbus.pdu.ExceptionResponse` if an error occurred
"""
if not (1 <= self.count <= 0x7D0):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
values = await context.async_getValues(
self.function_code, self.address, self.count
)
return ReadDiscreteInputsResponse(values)
class ReadDiscreteInputsResponse(ReadBitsResponseBase):
"""The discrete inputs in the response message are packed as one input per bit of the data field.
Status is indicated as 1= ON; 0= OFF. The LSB of
the first data byte contains the input addressed in the query. The other
inputs follow toward the high order end of this byte, and from low order
to high order in subsequent bytes.
If the returned input quantity is not a multiple of eight, the
remaining bits in the final data byte will be padded with zeros
(toward the high order end of the byte). The Byte Count field specifies
the quantity of complete bytes of data.
The requested coils can be found in boolean form in the .bits list.
"""
function_code = 2
def __init__(self, values=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The request values to respond with
:param slave: Modbus slave slave ID
"""
ReadBitsResponseBase.__init__(self, values, slave, transaction, protocol, skip_encode)

View File

@@ -0,0 +1,282 @@
"""Bit Writing Request/Response.
TODO write mask request/response
"""
# pylint: disable=missing-type-doc
import struct
from pymodbus.constants import ModbusStatus
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.utilities import pack_bitstring, unpack_bitstring
# ---------------------------------------------------------------------------#
# Local Constants
# ---------------------------------------------------------------------------#
# These are defined in the spec to turn a coil on/off
# ---------------------------------------------------------------------------#
_turn_coil_on = struct.pack(">H", ModbusStatus.ON)
_turn_coil_off = struct.pack(">H", ModbusStatus.OFF)
class WriteSingleCoilRequest(ModbusRequest):
"""This function code is used to write a single output to either ON or OFF in a remote device.
The requested ON/OFF state is specified by a constant in the request
data field. A value of FF 00 hex requests the output to be ON. A value
of 00 00 requests it to be OFF. All other values are illegal and will
not affect the output.
The Request PDU specifies the address of the coil to be forced. Coils
are addressed starting at zero. Therefore coil numbered 1 is addressed
as 0. The requested ON/OFF state is specified by a constant in the Coil
Value field. A value of 0XFF00 requests the coil to be ON. A value of
0X0000 requests the coil to be off. All other values are illegal and
will not affect the coil.
"""
function_code = 5
function_code_name = "write_coil"
_rtu_frame_size = 8
def __init__(self, address=None, value=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance.
:param address: The variable address to write
:param value: The value to write at address
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
self.value = bool(value)
def encode(self):
"""Encode write coil request.
:returns: The byte encoded message
"""
result = struct.pack(">H", self.address)
if self.value:
result += _turn_coil_on
else:
result += _turn_coil_off
return result
def decode(self, data):
"""Decode a write coil request.
:param data: The packet data to decode
"""
self.address, value = struct.unpack(">HH", data)
self.value = value == ModbusStatus.ON
async def execute(self, context):
"""Run a write coil request against a datastore.
:param context: The datastore to request from
:returns: The populated response or exception message
"""
# if self.value not in [ModbusStatus.Off, ModbusStatus.On]:
# return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, 1):
return self.doException(merror.IllegalAddress)
await context.async_setValues(self.function_code, self.address, [self.value])
values = await context.async_getValues(self.function_code, self.address, 1)
return WriteSingleCoilResponse(self.address, values[0])
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Output Address (2 byte) + Output Value (2 Bytes)
:return:
"""
return 1 + 2 + 2
def __str__(self):
"""Return a string representation of the instance.
:return: A string representation of the instance
"""
return f"WriteCoilRequest({self.address}, {self.value}) => "
class WriteSingleCoilResponse(ModbusResponse):
"""The normal response is an echo of the request.
Returned after the coil state has been written.
"""
function_code = 5
_rtu_frame_size = 8
def __init__(self, address=None, value=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The variable address written to
:param value: The value written at address
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
self.value = value
def encode(self):
"""Encode write coil response.
:return: The byte encoded message
"""
result = struct.pack(">H", self.address)
if self.value:
result += _turn_coil_on
else:
result += _turn_coil_off
return result
def decode(self, data):
"""Decode a write coil response.
:param data: The packet data to decode
"""
self.address, value = struct.unpack(">HH", data)
self.value = value == ModbusStatus.ON
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"WriteCoilResponse({self.address}) => {self.value}"
class WriteMultipleCoilsRequest(ModbusRequest):
"""This function code is used to forcea sequence of coils.
To either ON or OFF in a remote device. The Request PDU specifies the coil
references to be forced. Coils are addressed starting at zero. Therefore
coil numbered 1 is addressed as 0.
The requested ON/OFF states are specified by contents of the request
data field. A logical "1" in a bit position of the field requests the
corresponding output to be ON. A logical "0" requests it to be OFF."
"""
function_code = 15
function_code_name = "write_coils"
_rtu_byte_count_pos = 6
def __init__(self, address=None, values=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance.
:param address: The starting request address
:param values: The values to write
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
if values is None:
values = []
elif not hasattr(values, "__iter__"):
values = [values]
self.values = values
self.byte_count = (len(self.values) + 7) // 8
def encode(self):
"""Encode write coils request.
:returns: The byte encoded message
"""
count = len(self.values)
self.byte_count = (count + 7) // 8
packet = struct.pack(">HHB", self.address, count, self.byte_count)
packet += pack_bitstring(self.values)
return packet
def decode(self, data):
"""Decode a write coils request.
:param data: The packet data to decode
"""
self.address, count, self.byte_count = struct.unpack(">HHB", data[0:5])
values = unpack_bitstring(data[5:])
self.values = values[:count]
async def execute(self, context):
"""Run a write coils request against a datastore.
:param context: The datastore to request from
:returns: The populated response or exception message
"""
count = len(self.values)
if not 1 <= count <= 0x07B0:
return self.doException(merror.IllegalValue)
if self.byte_count != (count + 7) // 8:
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, count):
return self.doException(merror.IllegalAddress)
await context.async_setValues(
self.function_code, self.address, self.values
)
return WriteMultipleCoilsResponse(self.address, count)
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
params = (self.address, len(self.values))
return (
"WriteNCoilRequest (%d) => %d " # pylint: disable=consider-using-f-string
% params
)
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Output Address (2 byte) + Quantity of Outputs (2 Bytes)
:return:
"""
return 1 + 2 + 2
class WriteMultipleCoilsResponse(ModbusResponse):
"""The normal response returns the function code.
Starting address, and quantity of coils forced.
"""
function_code = 15
_rtu_frame_size = 8
def __init__(self, address=None, count=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The starting variable address written to
:param count: The number of values written
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
self.count = count
def encode(self):
"""Encode write coils response.
:returns: The byte encoded message
"""
return struct.pack(">HH", self.address, self.count)
def decode(self, data):
"""Decode a write coils response.
:param data: The packet data to decode
"""
self.address, self.count = struct.unpack(">HH", data)
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"WriteNCoilResponse({self.address}, {self.count})"

View File

@@ -0,0 +1,833 @@
"""Diagnostic Record Read/Write.
These need to be tied into a the current server context
or linked to the appropriate data
"""
# pylint: disable=missing-type-doc
import struct
from pymodbus.constants import ModbusPlusOperation, ModbusStatus
from pymodbus.device import ModbusControlBlock
from pymodbus.exceptions import ModbusException, NotImplementedException
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.utilities import pack_bitstring
_MCB = ModbusControlBlock()
# ---------------------------------------------------------------------------#
# Diagnostic Function Codes Base Classes
# diagnostic 08, 00-18,20
# ---------------------------------------------------------------------------#
# TODO Make sure all the data is decoded from the response # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class DiagnosticStatusRequest(ModbusRequest):
"""This is a base class for all of the diagnostic request functions."""
function_code = 0x08
function_code_name = "diagnostic_status"
_rtu_frame_size = 8
def __init__(self, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a diagnostic request."""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.message = None
def encode(self):
"""Encode a diagnostic response.
we encode the data set in self.message
:returns: The encoded packet
"""
packet = struct.pack(">H", self.sub_function_code)
if self.message is not None:
if isinstance(self.message, str):
packet += self.message.encode()
elif isinstance(self.message, bytes):
packet += self.message
elif isinstance(self.message, (list, tuple)):
for piece in self.message:
packet += struct.pack(">H", piece)
elif isinstance(self.message, int):
packet += struct.pack(">H", self.message)
return packet
def decode(self, data):
"""Decode a diagnostic request.
:param data: The data to decode into the function code
"""
(
self.sub_function_code, # pylint: disable=attribute-defined-outside-init
) = struct.unpack(">H", data[:2])
if self.sub_function_code == ReturnQueryDataRequest.sub_function_code:
self.message = data[2:]
else:
(self.message,) = struct.unpack(">H", data[2:])
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Sub function code (2 byte) + Data (2 * N bytes)
:return:
"""
if not isinstance(self.message, list):
self.message = [self.message]
return 1 + 2 + 2 * len(self.message)
class DiagnosticStatusResponse(ModbusResponse):
"""Diagnostic status.
This is a base class for all of the diagnostic response functions
It works by performing all of the encoding and decoding of variable
data and lets the higher classes define what extra data to append
and how to execute a request
"""
function_code = 0x08
_rtu_frame_size = 8
def __init__(self, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a diagnostic response."""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.message = None
def encode(self):
"""Encode diagnostic response.
we encode the data set in self.message
:returns: The encoded packet
"""
packet = struct.pack(">H", self.sub_function_code)
if self.message is not None:
if isinstance(self.message, str):
packet += self.message.encode()
elif isinstance(self.message, bytes):
packet += self.message
elif isinstance(self.message, (list, tuple)):
for piece in self.message:
packet += struct.pack(">H", piece)
elif isinstance(self.message, int):
packet += struct.pack(">H", self.message)
return packet
def decode(self, data):
"""Decode diagnostic response.
:param data: The data to decode into the function code
"""
(
self.sub_function_code, # pylint: disable=attribute-defined-outside-init
) = struct.unpack(">H", data[:2])
data = data[2:]
if self.sub_function_code == ReturnQueryDataRequest.sub_function_code:
self.message = data
else:
word_len = len(data) // 2
if len(data) % 2:
word_len += 1
data += b"0"
data = struct.unpack(">" + "H" * word_len, data)
self.message = data
class DiagnosticStatusSimpleRequest(DiagnosticStatusRequest):
"""Return diagnostic status.
A large majority of the diagnostic functions are simple
status request functions. They work by sending 0x0000
as data and their function code and they are returned
2 bytes of data.
If a function inherits this, they only need to implement
the execute method
"""
def __init__(self, data=0x0000, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a simple diagnostic request.
The data defaults to 0x0000 if not provided as over half
of the functions require it.
:param data: The data to send along with the request
"""
DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message = data
async def execute(self, *args):
"""Raise if not implemented."""
raise NotImplementedException("Diagnostic Message Has No Execute Method")
class DiagnosticStatusSimpleResponse(DiagnosticStatusResponse):
"""Diagnostic status.
A large majority of the diagnostic functions are simple
status request functions. They work by sending 0x0000
as data and their function code and they are returned
2 bytes of data.
"""
def __init__(self, data=0x0000, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Return a simple diagnostic response.
:param data: The resulting data to return to the client
"""
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message = data
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 00
# ---------------------------------------------------------------------------#
class ReturnQueryDataRequest(DiagnosticStatusRequest):
"""Return query data.
The data passed in the request data field is to be returned (looped back)
in the response. The entire response message should be identical to the
request.
"""
sub_function_code = 0x0000
def __init__(self, message=b"\x00\x00", slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance of the request.
:param message: The message to send to loopback
"""
DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if not isinstance(message, bytes):
raise ModbusException(f"message({type(message)}) must be bytes")
self.message = message
async def execute(self, *_args):
"""Execute the loopback request (builds the response).
:returns: The populated loopback response message
"""
return ReturnQueryDataResponse(self.message)
class ReturnQueryDataResponse(DiagnosticStatusResponse):
"""Return query data.
The data passed in the request data field is to be returned (looped back)
in the response. The entire response message should be identical to the
request.
"""
sub_function_code = 0x0000
def __init__(self, message=b"\x00\x00", slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance of the response.
:param message: The message to loopback
"""
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if not isinstance(message, bytes):
raise ModbusException(f"message({type(message)}) must be bytes")
self.message = message
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 01
# ---------------------------------------------------------------------------#
class RestartCommunicationsOptionRequest(DiagnosticStatusRequest):
"""Restart communication.
The remote device serial line port must be initialized and restarted, and
all of its communications event counters are cleared. If the port is
currently in Listen Only Mode, no response is returned. This function is
the only one that brings the port out of Listen Only Mode. If the port is
not currently in Listen Only Mode, a normal response is returned. This
occurs before the restart is executed.
"""
sub_function_code = 0x0001
def __init__(self, toggle=False, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new request.
:param toggle: Set to True to toggle, False otherwise
"""
DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if toggle:
self.message = [ModbusStatus.ON]
else:
self.message = [ModbusStatus.OFF]
async def execute(self, *_args):
"""Clear event log and restart.
:returns: The initialized response message
"""
# if _MCB.ListenOnly:
return RestartCommunicationsOptionResponse(self.message)
class RestartCommunicationsOptionResponse(DiagnosticStatusResponse):
"""Restart Communication.
The remote device serial line port must be initialized and restarted, and
all of its communications event counters are cleared. If the port is
currently in Listen Only Mode, no response is returned. This function is
the only one that brings the port out of Listen Only Mode. If the port is
not currently in Listen Only Mode, a normal response is returned. This
occurs before the restart is executed.
"""
sub_function_code = 0x0001
def __init__(self, toggle=False, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new response.
:param toggle: Set to True if we toggled, False otherwise
"""
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if toggle:
self.message = [ModbusStatus.ON]
else:
self.message = [ModbusStatus.OFF]
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 02
# ---------------------------------------------------------------------------#
class ReturnDiagnosticRegisterRequest(DiagnosticStatusSimpleRequest):
"""The contents of the remote device's 16-bit diagnostic register are returned in the response."""
sub_function_code = 0x0002
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
# if _MCB.isListenOnly():
register = pack_bitstring(_MCB.getDiagnosticRegister())
return ReturnDiagnosticRegisterResponse(register)
class ReturnDiagnosticRegisterResponse(DiagnosticStatusSimpleResponse):
"""Return diagnostic register.
The contents of the remote device's 16-bit diagnostic register are
returned in the response
"""
sub_function_code = 0x0002
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 03
# ---------------------------------------------------------------------------#
class ChangeAsciiInputDelimiterRequest(DiagnosticStatusSimpleRequest):
"""Change ascii input delimiter.
The character "CHAR" passed in the request data field becomes the end of
message delimiter for future messages (replacing the default LF
character). This function is useful in cases of a Line Feed is not
required at the end of ASCII messages.
"""
sub_function_code = 0x0003
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
char = (self.message & 0xFF00) >> 8 # type: ignore[operator]
_MCB.Delimiter = char
return ChangeAsciiInputDelimiterResponse(self.message)
class ChangeAsciiInputDelimiterResponse(DiagnosticStatusSimpleResponse):
"""Change ascii input delimiter.
The character "CHAR" passed in the request data field becomes the end of
message delimiter for future messages (replacing the default LF
character). This function is useful in cases of a Line Feed is not
required at the end of ASCII messages.
"""
sub_function_code = 0x0003
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 04
# ---------------------------------------------------------------------------#
class ForceListenOnlyModeRequest(DiagnosticStatusSimpleRequest):
"""Forces the addressed remote device to its Listen Only Mode for MODBUS communications.
This isolates it from the other devices on the network,
allowing them to continue communicating without interruption from the
addressed remote device. No response is returned.
"""
sub_function_code = 0x0004
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
_MCB.ListenOnly = True
return ForceListenOnlyModeResponse()
class ForceListenOnlyModeResponse(DiagnosticStatusResponse):
"""Forces the addressed remote device to its Listen Only Mode for MODBUS communications.
This isolates it from the other devices on the network,
allowing them to continue communicating without interruption from the
addressed remote device. No response is returned.
This does not send a response
"""
sub_function_code = 0x0004
should_respond = False
def __init__(self, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize to block a return response."""
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message = []
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 10
# ---------------------------------------------------------------------------#
class ClearCountersRequest(DiagnosticStatusSimpleRequest):
"""Clear ll counters and the diagnostic register.
Also, counters are cleared upon power-up
"""
sub_function_code = 0x000A
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
_MCB.reset()
return ClearCountersResponse(self.message)
class ClearCountersResponse(DiagnosticStatusSimpleResponse):
"""Clear ll counters and the diagnostic register.
Also, counters are cleared upon power-up
"""
sub_function_code = 0x000A
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 11
# ---------------------------------------------------------------------------#
class ReturnBusMessageCountRequest(DiagnosticStatusSimpleRequest):
"""Return bus message count.
The response data field returns the quantity of messages that the
remote device has detected on the communications systems since its last
restart, clear counters operation, or power-up
"""
sub_function_code = 0x000B
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.BusMessage
return ReturnBusMessageCountResponse(count)
class ReturnBusMessageCountResponse(DiagnosticStatusSimpleResponse):
"""Return bus message count.
The response data field returns the quantity of messages that the
remote device has detected on the communications systems since its last
restart, clear counters operation, or power-up
"""
sub_function_code = 0x000B
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 12
# ---------------------------------------------------------------------------#
class ReturnBusCommunicationErrorCountRequest(DiagnosticStatusSimpleRequest):
"""Return bus comm. count.
The response data field returns the quantity of CRC errors encountered
by the remote device since its last restart, clear counter operation, or
power-up
"""
sub_function_code = 0x000C
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.BusCommunicationError
return ReturnBusCommunicationErrorCountResponse(count)
class ReturnBusCommunicationErrorCountResponse(DiagnosticStatusSimpleResponse):
"""Return bus comm. error.
The response data field returns the quantity of CRC errors encountered
by the remote device since its last restart, clear counter operation, or
power-up
"""
sub_function_code = 0x000C
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 13
# ---------------------------------------------------------------------------#
class ReturnBusExceptionErrorCountRequest(DiagnosticStatusSimpleRequest):
"""Return bus exception.
The response data field returns the quantity of modbus exception
responses returned by the remote device since its last restart,
clear counters operation, or power-up
"""
sub_function_code = 0x000D
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.BusExceptionError
return ReturnBusExceptionErrorCountResponse(count)
class ReturnBusExceptionErrorCountResponse(DiagnosticStatusSimpleResponse):
"""Return bus exception.
The response data field returns the quantity of modbus exception
responses returned by the remote device since its last restart,
clear counters operation, or power-up
"""
sub_function_code = 0x000D
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 14
# ---------------------------------------------------------------------------#
class ReturnSlaveMessageCountRequest(DiagnosticStatusSimpleRequest):
"""Return slave message count.
The response data field returns the quantity of messages addressed to the
remote device, or broadcast, that the remote device has processed since
its last restart, clear counters operation, or power-up
"""
sub_function_code = 0x000E
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.SlaveMessage
return ReturnSlaveMessageCountResponse(count)
class ReturnSlaveMessageCountResponse(DiagnosticStatusSimpleResponse):
"""Return slave message count.
The response data field returns the quantity of messages addressed to the
remote device, or broadcast, that the remote device has processed since
its last restart, clear counters operation, or power-up
"""
sub_function_code = 0x000E
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 15
# ---------------------------------------------------------------------------#
class ReturnSlaveNoResponseCountRequest(DiagnosticStatusSimpleRequest):
"""Return slave no response.
The response data field returns the quantity of messages addressed to the
remote device, or broadcast, that the remote device has processed since
its last restart, clear counters operation, or power-up
"""
sub_function_code = 0x000F
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.SlaveNoResponse
return ReturnSlaveNoResponseCountResponse(count)
class ReturnSlaveNoResponseCountResponse(DiagnosticStatusSimpleResponse):
"""Return slave no response.
The response data field returns the quantity of messages addressed to the
remote device, or broadcast, that the remote device has processed since
its last restart, clear counters operation, or power-up
"""
sub_function_code = 0x000F
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 16
# ---------------------------------------------------------------------------#
class ReturnSlaveNAKCountRequest(DiagnosticStatusSimpleRequest):
"""Return slave NAK count.
The response data field returns the quantity of messages addressed to the
remote device for which it returned a Negative Acknowledge (NAK) exception
response, since its last restart, clear counters operation, or power-up.
Exception responses are described and listed in section 7 .
"""
sub_function_code = 0x0010
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.SlaveNAK
return ReturnSlaveNAKCountResponse(count)
class ReturnSlaveNAKCountResponse(DiagnosticStatusSimpleResponse):
"""Return slave NAK.
The response data field returns the quantity of messages addressed to the
remote device for which it returned a Negative Acknowledge (NAK) exception
response, since its last restart, clear counters operation, or power-up.
Exception responses are described and listed in section 7.
"""
sub_function_code = 0x0010
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 17
# ---------------------------------------------------------------------------#
class ReturnSlaveBusyCountRequest(DiagnosticStatusSimpleRequest):
"""Return slave busy count.
The response data field returns the quantity of messages addressed to the
remote device for which it returned a Slave Device Busy exception response,
since its last restart, clear counters operation, or power-up.
"""
sub_function_code = 0x0011
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.SlaveBusy
return ReturnSlaveBusyCountResponse(count)
class ReturnSlaveBusyCountResponse(DiagnosticStatusSimpleResponse):
"""Return slave busy count.
The response data field returns the quantity of messages addressed to the
remote device for which it returned a Slave Device Busy exception response,
since its last restart, clear counters operation, or power-up.
"""
sub_function_code = 0x0011
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 18
# ---------------------------------------------------------------------------#
class ReturnSlaveBusCharacterOverrunCountRequest(DiagnosticStatusSimpleRequest):
"""Return slave character overrun.
The response data field returns the quantity of messages addressed to the
remote device that it could not handle due to a character overrun condition,
since its last restart, clear counters operation, or power-up. A character
overrun is caused by data characters arriving at the port faster than they
can be stored, or by the loss of a character due to a hardware malfunction.
"""
sub_function_code = 0x0012
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.BusCharacterOverrun
return ReturnSlaveBusCharacterOverrunCountResponse(count)
class ReturnSlaveBusCharacterOverrunCountResponse(DiagnosticStatusSimpleResponse):
"""Return the quantity of messages addressed to the remote device unhandled due to a character overrun.
Since its last restart, clear counters operation, or power-up. A character
overrun is caused by data characters arriving at the port faster than they
can be stored, or by the loss of a character due to a hardware malfunction.
"""
sub_function_code = 0x0012
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 19
# ---------------------------------------------------------------------------#
class ReturnIopOverrunCountRequest(DiagnosticStatusSimpleRequest):
"""Return IopOverrun.
An IOP overrun is caused by data characters arriving at the port
faster than they can be stored, or by the loss of a character due
to a hardware malfunction. This function is specific to the 884.
"""
sub_function_code = 0x0013
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
count = _MCB.Counter.BusCharacterOverrun
return ReturnIopOverrunCountResponse(count)
class ReturnIopOverrunCountResponse(DiagnosticStatusSimpleResponse):
"""Return Iop overrun count.
The response data field returns the quantity of messages
addressed to the slave that it could not handle due to an 884
IOP overrun condition, since its last restart, clear counters
operation, or power-up.
"""
sub_function_code = 0x0013
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 20
# ---------------------------------------------------------------------------#
class ClearOverrunCountRequest(DiagnosticStatusSimpleRequest):
"""Clear the overrun error counter and reset the error flag.
An error flag should be cleared, but nothing else in the
specification mentions is, so it is ignored.
"""
sub_function_code = 0x0014
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
_MCB.Counter.BusCharacterOverrun = 0x0000
return ClearOverrunCountResponse(self.message)
class ClearOverrunCountResponse(DiagnosticStatusSimpleResponse):
"""Clear the overrun error counter and reset the error flag."""
sub_function_code = 0x0014
# ---------------------------------------------------------------------------#
# Diagnostic Sub Code 21
# ---------------------------------------------------------------------------#
class GetClearModbusPlusRequest(DiagnosticStatusSimpleRequest):
"""Get/Clear modbus plus request.
In addition to the Function code (08) and Subfunction code
(00 15 hex) in the query, a two-byte Operation field is used
to specify either a "Get Statistics" or a "Clear Statistics"
operation. The two operations are exclusive - the "Get"
operation cannot clear the statistics, and the "Clear"
operation does not return statistics prior to clearing
them. Statistics are also cleared on power-up of the slave
device.
"""
sub_function_code = 0x0015
def __init__(self, data=0, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize."""
super().__init__(slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message=data
def get_response_pdu_size(self):
"""Return a series of 54 16-bit words (108 bytes) in the data field of the response.
This function differs from the usual two-byte length of the data field.
The data contains the statistics for the Modbus Plus peer processor in the slave device.
Func_code (1 byte) + Sub function code (2 byte) + Operation (2 byte) + Data (108 bytes)
:return:
"""
if self.message == ModbusPlusOperation.GET_STATISTICS:
data = 2 + 108 # byte count(2) + data (54*2)
else:
data = 0
return 1 + 2 + 2 + 2 + data
async def execute(self, *args):
"""Execute the diagnostic request on the given device.
:returns: The initialized response message
"""
message = None # the clear operation does not return info
if self.message == ModbusPlusOperation.CLEAR_STATISTICS:
_MCB.Plus.reset()
message = self.message
else:
message = [self.message]
message += _MCB.Plus.encode()
return GetClearModbusPlusResponse(message)
def encode(self):
"""Encode a diagnostic response.
we encode the data set in self.message
:returns: The encoded packet
"""
packet = struct.pack(">H", self.sub_function_code)
packet += struct.pack(">H", self.message)
return packet
class GetClearModbusPlusResponse(DiagnosticStatusSimpleResponse):
"""Return a series of 54 16-bit words (108 bytes) in the data field of the response.
This function differs from the usual two-byte length of the data field.
The data contains the statistics for the Modbus Plus peer processor in the slave device.
"""
sub_function_code = 0x0015

View File

@@ -0,0 +1,428 @@
"""File Record Read/Write Messages.
Currently none of these messages are implemented
"""
from __future__ import annotations
# pylint: disable=missing-type-doc
import struct
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
# ---------------------------------------------------------------------------#
# File Record Types
# ---------------------------------------------------------------------------#
class FileRecord: # pylint: disable=eq-without-hash
"""Represents a file record and its relevant data."""
def __init__(self, reference_type=0x06, file_number=0x00, record_number=0x00, record_data="", record_length=None, response_length=None):
"""Initialize a new instance.
:params reference_type: must be 0x06
:params file_number: Indicates which file number we are reading
:params record_number: Indicates which record in the file
:params record_data: The actual data of the record
:params record_length: The length in registers of the record
:params response_length: The length in bytes of the record
"""
self.reference_type = reference_type
self.file_number = file_number
self.record_number = record_number
self.record_data = record_data
self.record_length = record_length if record_length else len(self.record_data) // 2
self.response_length = response_length if response_length else len(self.record_data) + 1
def __eq__(self, relf):
"""Compare the left object to the right."""
return (
self.reference_type == relf.reference_type
and self.file_number == relf.file_number
and self.record_number == relf.record_number
and self.record_length == relf.record_length
and self.record_data == relf.record_data
)
def __ne__(self, relf):
"""Compare the left object to the right."""
return not self.__eq__(relf)
def __repr__(self):
"""Give a representation of the file record."""
params = (self.file_number, self.record_number, self.record_length)
return (
"FileRecord(file=%d, record=%d, length=%d)" # pylint: disable=consider-using-f-string
% params
)
# ---------------------------------------------------------------------------#
# File Requests/Responses
# ---------------------------------------------------------------------------#
class ReadFileRecordRequest(ModbusRequest):
"""Read file record request.
This function code is used to perform a file record read. All request
data lengths are provided in terms of number of bytes and all record
lengths are provided in terms of registers.
A file is an organization of records. Each file contains 10000 records,
addressed 0000 to 9999 decimal or 0x0000 to 0x270f. For example, record
12 is addressed as 12. The function can read multiple groups of
references. The groups can be separating (non-contiguous), but the
references within each group must be sequential. Each group is defined
in a separate "sub-request" field that contains seven bytes::
The reference type: 1 byte (must be 0x06)
The file number: 2 bytes
The starting record number within the file: 2 bytes
The length of the record to be read: 2 bytes
The quantity of registers to be read, combined with all other fields
in the expected response, must not exceed the allowable length of the
MODBUS PDU: 235 bytes.
"""
function_code = 0x14
function_code_name = "read_file_record"
_rtu_byte_count_pos = 2
def __init__(self, records=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param records: The file record requests to be read
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.records = records or []
def encode(self):
"""Encode the request packet.
:returns: The byte encoded packet
"""
packet = struct.pack("B", len(self.records) * 7)
for record in self.records:
packet += struct.pack(
">BHHH",
0x06,
record.file_number,
record.record_number,
record.record_length,
)
return packet
def decode(self, data):
"""Decode the incoming request.
:param data: The data to decode into the address
"""
self.records = []
byte_count = int(data[0])
for count in range(1, byte_count, 7):
decoded = struct.unpack(">BHHH", data[count : count + 7])
record = FileRecord(
file_number=decoded[1],
record_number=decoded[2],
record_length=decoded[3],
)
if decoded[0] == 0x06:
self.records.append(record)
def execute(self, _context):
"""Run a read exception status request against the store.
:returns: The populated response
"""
# TODO do some new context operation here # pylint: disable=fixme
# if file number, record number, or address + length
# is too big, return an error.
files: list[FileRecord] = []
return ReadFileRecordResponse(files)
class ReadFileRecordResponse(ModbusResponse):
"""Read file record response.
The normal response is a series of "sub-responses," one for each
"sub-request." The byte count field is the total combined count of
bytes in all "sub-responses." In addition, each "sub-response"
contains a field that shows its own byte count.
"""
function_code = 0x14
_rtu_byte_count_pos = 2
def __init__(self, records=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param records: The requested file records
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.records = records or []
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
total = sum(record.response_length + 1 for record in self.records)
packet = struct.pack("B", total)
for record in self.records:
packet += struct.pack(">BB", record.record_length, 0x06)
packet += record.record_data
return packet
def decode(self, data):
"""Decode the response.
:param data: The packet data to decode
"""
count, self.records = 1, []
byte_count = int(data[0])
while count < byte_count:
response_length, reference_type = struct.unpack(
">BB", data[count : count + 2]
)
count += response_length + 1 # the count is not included
record = FileRecord(
response_length=response_length,
record_data=data[count - response_length + 1 : count],
)
if reference_type == 0x06:
self.records.append(record)
class WriteFileRecordRequest(ModbusRequest):
"""Write file record request.
This function code is used to perform a file record write. All
request data lengths are provided in terms of number of bytes
and all record lengths are provided in terms of the number of 16
bit words.
"""
function_code = 0x15
function_code_name = "write_file_record"
_rtu_byte_count_pos = 2
def __init__(self, records=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param records: The file record requests to be read
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.records = records or []
def encode(self):
"""Encode the request packet.
:returns: The byte encoded packet
"""
total_length = sum((record.record_length * 2) + 7 for record in self.records)
packet = struct.pack("B", total_length)
for record in self.records:
packet += struct.pack(
">BHHH",
0x06,
record.file_number,
record.record_number,
record.record_length,
)
packet += record.record_data
return packet
def decode(self, data):
"""Decode the incoming request.
:param data: The data to decode into the address
"""
byte_count = int(data[0])
count, self.records = 1, []
while count < byte_count:
decoded = struct.unpack(">BHHH", data[count : count + 7])
response_length = decoded[3] * 2
count += response_length + 7
record = FileRecord(
record_length=decoded[3],
file_number=decoded[1],
record_number=decoded[2],
record_data=data[count - response_length : count],
)
if decoded[0] == 0x06:
self.records.append(record)
def execute(self, _context):
"""Run the write file record request against the context.
:returns: The populated response
"""
# TODO do some new context operation here # pylint: disable=fixme
# if file number, record number, or address + length
# is too big, return an error.
return WriteFileRecordResponse(self.records)
class WriteFileRecordResponse(ModbusResponse):
"""The normal response is an echo of the request."""
function_code = 0x15
_rtu_byte_count_pos = 2
def __init__(self, records=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param records: The file record requests to be read
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.records = records or []
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
total_length = sum((record.record_length * 2) + 7 for record in self.records)
packet = struct.pack("B", total_length)
for record in self.records:
packet += struct.pack(
">BHHH",
0x06,
record.file_number,
record.record_number,
record.record_length,
)
packet += record.record_data
return packet
def decode(self, data):
"""Decode the incoming request.
:param data: The data to decode into the address
"""
count, self.records = 1, []
byte_count = int(data[0])
while count < byte_count:
decoded = struct.unpack(">BHHH", data[count : count + 7])
response_length = decoded[3] * 2
count += response_length + 7
record = FileRecord(
record_length=decoded[3],
file_number=decoded[1],
record_number=decoded[2],
record_data=data[count - response_length : count],
)
if decoded[0] == 0x06:
self.records.append(record)
class ReadFifoQueueRequest(ModbusRequest):
"""Read fifo queue request.
This function code allows to read the contents of a First-In-First-Out
(FIFO) queue of register in a remote device. The function returns a
count of the registers in the queue, followed by the queued data.
Up to 32 registers can be read: the count, plus up to 31 queued data
registers.
The queue count register is returned first, followed by the queued data
registers. The function reads the queue contents, but does not clear
them.
"""
function_code = 0x18
function_code_name = "read_fifo_queue"
_rtu_frame_size = 6
def __init__(self, address=0x0000, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The fifo pointer address (0x0000 to 0xffff)
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
self.values = [] # this should be added to the context
def encode(self):
"""Encode the request packet.
:returns: The byte encoded packet
"""
return struct.pack(">H", self.address)
def decode(self, data):
"""Decode the incoming request.
:param data: The data to decode into the address
"""
self.address = struct.unpack(">H", data)[0]
def execute(self, _context):
"""Run a read exception status request against the store.
:returns: The populated response
"""
if not 0x0000 <= self.address <= 0xFFFF:
return self.doException(merror.IllegalValue)
if len(self.values) > 31:
return self.doException(merror.IllegalValue)
# TODO pull the values from some context # pylint: disable=fixme
return ReadFifoQueueResponse(self.values)
class ReadFifoQueueResponse(ModbusResponse):
"""Read Fifo queue response.
In a normal response, the byte count shows the quantity of bytes to
follow, including the queue count bytes and value register bytes
(but not including the error check field). The queue count is the
quantity of data registers in the queue (not including the count register).
If the queue count exceeds 31, an exception response is returned with an
error code of 03 (Illegal Data Value).
"""
function_code = 0x18
@classmethod
def calculateRtuFrameSize(cls, buffer):
"""Calculate the size of the message.
:param buffer: A buffer containing the data that have been received.
:returns: The number of bytes in the response.
"""
hi_byte = int(buffer[2])
lo_byte = int(buffer[3])
return (hi_byte << 16) + lo_byte + 6
def __init__(self, values=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The list of values of the fifo to return
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.values = values or []
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
length = len(self.values) * 2
packet = struct.pack(">HH", 2 + length, length)
for value in self.values:
packet += struct.pack(">H", value)
return packet
def decode(self, data):
"""Decode a the response.
:param data: The packet data to decode
"""
self.values = []
_, count = struct.unpack(">HH", data[0:4])
for index in range(0, count - 4):
idx = 4 + index * 2
self.values.append(struct.unpack(">H", data[idx : idx + 2])[0])

View File

@@ -0,0 +1,216 @@
"""Encapsulated Interface (MEI) Transport Messages."""
# pylint: disable=missing-type-doc
import struct
from pymodbus.constants import DeviceInformation, MoreData
from pymodbus.device import DeviceInformationFactory, ModbusControlBlock
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
_MCB = ModbusControlBlock()
class _OutOfSpaceException(Exception):
"""Internal out of space exception."""
# This exception exists here as a simple, local way to manage response
# length control for the only MODBUS command which requires it under
# standard, non-error conditions. It and the structures associated with
# it should ideally be refactored and applied to all responses, however,
# since a Client can make requests which result in disallowed conditions,
# such as, for instance, requesting a register read of more registers
# than will fit in a single PDU. As per the specification, the PDU is
# restricted to 253 bytes, irrespective of the transport used.
#
# See Page 5/50 of MODBUS Application Protocol Specification V1.1b3.
def __init__(self, oid):
self.oid = oid
super().__init__()
# ---------------------------------------------------------------------------#
# Read Device Information
# ---------------------------------------------------------------------------#
class ReadDeviceInformationRequest(ModbusRequest):
"""Read device information.
This function code allows reading the identification and additional
information relative to the physical and functional description of a
remote device, only.
The Read Device Identification interface is modeled as an address space
composed of a set of addressable data elements. The data elements are
called objects and an object Id identifies them.
"""
function_code = 0x2B
sub_function_code = 0x0E
function_code_name = "read_device_information"
_rtu_frame_size = 7
def __init__(self, read_code=None, object_id=0x00, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param read_code: The device information read code
:param object_id: The object to read from
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.read_code = read_code or DeviceInformation.BASIC
self.object_id = object_id
def encode(self):
"""Encode the request packet.
:returns: The byte encoded packet
"""
packet = struct.pack(
">BBB", self.sub_function_code, self.read_code, self.object_id
)
return packet
def decode(self, data):
"""Decode data part of the message.
:param data: The incoming data
"""
params = struct.unpack(">BBB", data)
self.sub_function_code, self.read_code, self.object_id = params
async def execute(self, _context):
"""Run a read exception status request against the store.
:returns: The populated response
"""
if not 0x00 <= self.object_id <= 0xFF:
return self.doException(merror.IllegalValue)
if not 0x00 <= self.read_code <= 0x04:
return self.doException(merror.IllegalValue)
information = DeviceInformationFactory.get(_MCB, self.read_code, self.object_id)
return ReadDeviceInformationResponse(self.read_code, information)
def __str__(self):
"""Build a representation of the request.
:returns: The string representation of the request
"""
params = (self.read_code, self.object_id)
return (
"ReadDeviceInformationRequest(%d,%d)" # pylint: disable=consider-using-f-string
% params
)
class ReadDeviceInformationResponse(ModbusResponse):
"""Read device information response."""
function_code = 0x2B
sub_function_code = 0x0E
@classmethod
def calculateRtuFrameSize(cls, buffer):
"""Calculate the size of the message.
:param buffer: A buffer containing the data that have been received.
:returns: The number of bytes in the response.
"""
size = 8 # skip the header information
count = int(buffer[7])
try:
while count > 0:
_, object_length = struct.unpack(">BB", buffer[size : size + 2])
size += object_length + 2
count -= 1
return size + 2
except struct.error as exc:
raise IndexError from exc
def __init__(self, read_code=None, information=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param read_code: The device information read code
:param information: The requested information request
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.read_code = read_code or DeviceInformation.BASIC
self.information = information or {}
self.number_of_objects = 0
self.conformity = 0x83 # I support everything right now
self.next_object_id = 0x00
self.more_follows = MoreData.NOTHING
self.space_left = 253 - 6
def _encode_object(self, object_id, data):
"""Encode object."""
self.space_left -= 2 + len(data)
if self.space_left <= 0:
raise _OutOfSpaceException(object_id)
encoded_obj = struct.pack(">BB", object_id, len(data))
if isinstance(data, bytes):
encoded_obj += data
else:
encoded_obj += data.encode()
self.number_of_objects += 1
return encoded_obj
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
packet = struct.pack(
">BBB", self.sub_function_code, self.read_code, self.conformity
)
objects = b""
try:
for object_id, data in iter(self.information.items()):
if isinstance(data, list):
for item in data:
objects += self._encode_object(object_id, item)
else:
objects += self._encode_object(object_id, data)
except _OutOfSpaceException as exc:
self.next_object_id = exc.oid
self.more_follows = MoreData.KEEP_READING
packet += struct.pack(
">BBB", self.more_follows, self.next_object_id, self.number_of_objects
)
packet += objects
return packet
def decode(self, data):
"""Decode a the response.
:param data: The packet data to decode
"""
params = struct.unpack(">BBBBBB", data[0:6])
self.sub_function_code, self.read_code = params[0:2]
self.conformity, self.more_follows = params[2:4]
self.next_object_id, self.number_of_objects = params[4:6]
self.information, count = {}, 6 # skip the header information
while count < len(data):
object_id, object_length = struct.unpack(">BB", data[count : count + 2])
count += object_length + 2
if object_id not in self.information:
self.information[object_id] = data[count - object_length : count]
elif isinstance(self.information[object_id], list):
self.information[object_id].append(data[count - object_length : count])
else:
self.information[object_id] = [
self.information[object_id],
data[count - object_length : count],
]
def __str__(self):
"""Build a representation of the response.
:returns: The string representation of the response
"""
return f"ReadDeviceInformationResponse({self.read_code})"

View File

@@ -0,0 +1,473 @@
"""Diagnostic record read/write.
Currently not all implemented
"""
# pylint: disable=missing-type-doc
import struct
from pymodbus.constants import ModbusStatus
from pymodbus.device import DeviceInformationFactory, ModbusControlBlock
from pymodbus.pdu import ModbusRequest, ModbusResponse
_MCB = ModbusControlBlock()
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class ReadExceptionStatusRequest(ModbusRequest):
"""This function code is used to read the contents of eight Exception Status outputs in a remote device.
The function provides a simple method for
accessing this information, because the Exception Output references are
known (no output reference is needed in the function).
"""
function_code = 0x07
function_code_name = "read_exception_status"
_rtu_frame_size = 4
def __init__(self, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance."""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
def encode(self):
"""Encode the message."""
return b""
def decode(self, data):
"""Decode data part of the message.
:param data: The incoming data
"""
async def execute(self, _context=None):
"""Run a read exception status request against the store.
:returns: The populated response
"""
status = _MCB.Counter.summary()
return ReadExceptionStatusResponse(status)
def __str__(self):
"""Build a representation of the request.
:returns: The string representation of the request
"""
return f"ReadExceptionStatusRequest({self.function_code})"
class ReadExceptionStatusResponse(ModbusResponse):
"""The normal response contains the status of the eight Exception Status outputs.
The outputs are packed into one data byte, with one bit
per output. The status of the lowest output reference is contained
in the least significant bit of the byte. The contents of the eight
Exception Status outputs are device specific.
"""
function_code = 0x07
_rtu_frame_size = 5
def __init__(self, status=0x00, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param status: The status response to report
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.status = status if status < 256 else 255
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
return struct.pack(">B", self.status)
def decode(self, data):
"""Decode a the response.
:param data: The packet data to decode
"""
self.status = int(data[0])
def __str__(self):
"""Build a representation of the response.
:returns: The string representation of the response
"""
arguments = (self.function_code, self.status)
return (
"ReadExceptionStatusResponse(%d, %s)" # pylint: disable=consider-using-f-string
% arguments
)
# Encapsulate interface transport 43, 14
# CANopen general reference 43, 13
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class GetCommEventCounterRequest(ModbusRequest):
"""This function code is used to get a status word.
And an event count from the remote device's communication event counter.
By fetching the current count before and after a series of messages, a
client can determine whether the messages were handled normally by the
remote device.
The device's event counter is incremented once for each successful
message completion. It is not incremented for exception responses,
poll commands, or fetch event counter commands.
The event counter can be reset by means of the Diagnostics function
(code 08), with a subfunction of Restart Communications Option
(code 00 01) or Clear Counters and Diagnostic Register (code 00 0A).
"""
function_code = 0x0B
function_code_name = "get_event_counter"
_rtu_frame_size = 4
def __init__(self, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance."""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
def encode(self):
"""Encode the message."""
return b""
def decode(self, data):
"""Decode data part of the message.
:param data: The incoming data
"""
async def execute(self, _context=None):
"""Run a read exception status request against the store.
:returns: The populated response
"""
status = _MCB.Counter.Event
return GetCommEventCounterResponse(status)
def __str__(self):
"""Build a representation of the request.
:returns: The string representation of the request
"""
return f"GetCommEventCounterRequest({self.function_code})"
class GetCommEventCounterResponse(ModbusResponse):
"""Get comm event counter response.
The normal response contains a two-byte status word, and a two-byte
event count. The status word will be all ones (FF FF hex) if a
previously-issued program command is still being processed by the
remote device (a busy condition exists). Otherwise, the status word
will be all zeros.
"""
function_code = 0x0B
_rtu_frame_size = 8
def __init__(self, count=0x0000, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param count: The current event counter value
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.count = count
self.status = True # this means we are ready, not waiting
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
if self.status:
ready = ModbusStatus.READY
else:
ready = ModbusStatus.WAITING
return struct.pack(">HH", ready, self.count)
def decode(self, data):
"""Decode a the response.
:param data: The packet data to decode
"""
ready, self.count = struct.unpack(">HH", data)
self.status = ready == ModbusStatus.READY
def __str__(self):
"""Build a representation of the response.
:returns: The string representation of the response
"""
arguments = (self.function_code, self.count, self.status)
return (
"GetCommEventCounterResponse(%d, %d, %d)" # pylint: disable=consider-using-f-string
% arguments
)
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class GetCommEventLogRequest(ModbusRequest):
"""This function code is used to get a status word.
Event count, message count, and a field of event bytes from the remote device.
The status word and event counts are identical to that returned by
the Get Communications Event Counter function (11, 0B hex).
The message counter contains the quantity of messages processed by the
remote device since its last restart, clear counters operation, or
power-up. This count is identical to that returned by the Diagnostic
function (code 08), sub-function Return Bus Message Count (code 11,
0B hex).
The event bytes field contains 0-64 bytes, with each byte corresponding
to the status of one MODBUS send or receive operation for the remote
device. The remote device enters the events into the field in
chronological order. Byte 0 is the most recent event. Each new byte
flushes the oldest byte from the field.
"""
function_code = 0x0C
function_code_name = "get_event_log"
_rtu_frame_size = 4
def __init__(self, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance."""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
def encode(self):
"""Encode the message."""
return b""
def decode(self, data):
"""Decode data part of the message.
:param data: The incoming data
"""
async def execute(self, _context=None):
"""Run a read exception status request against the store.
:returns: The populated response
"""
results = {
"status": True,
"message_count": _MCB.Counter.BusMessage,
"event_count": _MCB.Counter.Event,
"events": _MCB.getEvents(),
}
return GetCommEventLogResponse(**results)
def __str__(self):
"""Build a representation of the request.
:returns: The string representation of the request
"""
return f"GetCommEventLogRequest({self.function_code})"
class GetCommEventLogResponse(ModbusResponse):
"""Get Comm event log response.
The normal response contains a two-byte status word field,
a two-byte event count field, a two-byte message count field,
and a field containing 0-64 bytes of events. A byte count
field defines the total length of the data in these four field
"""
function_code = 0x0C
_rtu_byte_count_pos = 2
def __init__(self, status=True, message_count=0, event_count=0, events=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param status: The status response to report
:param message_count: The current message count
:param event_count: The current event count
:param events: The collection of events to send
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.status = status
self.message_count = message_count
self.event_count = event_count
self.events = events if events else []
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
if self.status:
ready = ModbusStatus.READY
else:
ready = ModbusStatus.WAITING
packet = struct.pack(">B", 6 + len(self.events))
packet += struct.pack(">H", ready)
packet += struct.pack(">HH", self.event_count, self.message_count)
packet += b"".join(struct.pack(">B", e) for e in self.events)
return packet
def decode(self, data):
"""Decode a the response.
:param data: The packet data to decode
"""
length = int(data[0])
status = struct.unpack(">H", data[1:3])[0]
self.status = status == ModbusStatus.READY
self.event_count = struct.unpack(">H", data[3:5])[0]
self.message_count = struct.unpack(">H", data[5:7])[0]
self.events = []
for i in range(7, length + 1):
self.events.append(int(data[i]))
def __str__(self):
"""Build a representation of the response.
:returns: The string representation of the response
"""
arguments = (
self.function_code,
self.status,
self.message_count,
self.event_count,
)
return (
"GetCommEventLogResponse(%d, %d, %d, %d)" # pylint: disable=consider-using-f-string
% arguments
)
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class ReportSlaveIdRequest(ModbusRequest):
"""This function code is used to read the description of the type.
The current status, and other information specific to a remote device.
"""
function_code = 0x11
function_code_name = "report_slave_id"
_rtu_frame_size = 4
def __init__(self, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param slave: Modbus slave slave ID
"""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
def encode(self):
"""Encode the message."""
return b""
def decode(self, data):
"""Decode data part of the message.
:param data: The incoming data
"""
async def execute(self, context=None):
"""Run a report slave id request against the store.
:returns: The populated response
"""
report_slave_id_data = None
if context:
report_slave_id_data = getattr(context, "reportSlaveIdData", None)
if not report_slave_id_data:
information = DeviceInformationFactory.get(_MCB)
# Support identity values as bytes data and regular str data
id_data = []
for v_item in information.values():
if isinstance(v_item, bytes):
id_data.append(v_item)
else:
id_data.append(v_item.encode())
identifier = b"-".join(id_data)
identifier = identifier or b"Pymodbus"
report_slave_id_data = identifier
return ReportSlaveIdResponse(report_slave_id_data)
def __str__(self):
"""Build a representation of the request.
:returns: The string representation of the request
"""
return f"ReportSlaveIdRequest({self.function_code})"
class ReportSlaveIdResponse(ModbusResponse):
"""Show response.
The data contents are specific to each type of device.
"""
function_code = 0x11
_rtu_byte_count_pos = 2
def __init__(self, identifier=b"\x00", status=True, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param identifier: The identifier of the slave
:param status: The status response to report
"""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.identifier = identifier
self.status = status
self.byte_count = None
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
if self.status:
status = ModbusStatus.SLAVE_ON
else:
status = ModbusStatus.SLAVE_OFF
length = len(self.identifier) + 1
packet = struct.pack(">B", length)
packet += self.identifier # we assume it is already encoded
packet += struct.pack(">B", status)
return packet
def decode(self, data):
"""Decode a the response.
Since the identifier is device dependent, we just return the
raw value that a user can decode to whatever it should be.
:param data: The packet data to decode
"""
self.byte_count = int(data[0])
self.identifier = data[1 : self.byte_count + 1]
status = int(data[-1])
self.status = status == ModbusStatus.SLAVE_ON
def __str__(self) -> str:
"""Build a representation of the response.
:returns: The string representation of the response
"""
return f"ReportSlaveIdResponse({self.function_code}, {self.identifier}, {self.status})"

View File

@@ -0,0 +1,255 @@
"""Contains base classes for modbus request/response/error packets."""
# pylint: disable=missing-type-doc
import struct
from pymodbus.exceptions import NotImplementedException
from pymodbus.logging import Log
from pymodbus.utilities import rtuFrameSize
# --------------------------------------------------------------------------- #
# Base PDUs
# --------------------------------------------------------------------------- #
class ModbusPDU:
"""Base class for all Modbus messages.
.. attribute:: transaction_id
This value is used to uniquely identify a request
response pair. It can be implemented as a simple counter
.. attribute:: protocol_id
This is a constant set at 0 to indicate Modbus. It is
put here for ease of expansion.
.. attribute:: slave_id
This is used to route the request to the correct child. In
the TCP modbus, it is used for routing (or not used at all. However,
for the serial versions, it is used to specify which child to perform
the requests against. The value 0x00 represents the broadcast address
(also 0xff).
.. attribute:: check
This is used for LRC/CRC in the serial modbus protocols
.. attribute:: skip_encode
This is used when the message payload has already been encoded.
Generally this will occur when the PayloadBuilder is being used
to create a complicated message. By setting this to True, the
request will pass the currently encoded message through instead
of encoding it again.
"""
def __init__(self, slave, transaction, protocol, skip_encode):
"""Initialize the base data for a modbus request.
:param slave: Modbus slave slave ID
"""
self.transaction_id = transaction
self.protocol_id = protocol
self.slave_id = slave
self.skip_encode = skip_encode
self.check = 0x0000
def encode(self):
"""Encode the message.
:raises: A not implemented exception
"""
raise NotImplementedException()
def decode(self, data):
"""Decode data part of the message.
:param data: is a string object
:raises NotImplementedException:
"""
raise NotImplementedException()
@classmethod
def calculateRtuFrameSize(cls, buffer):
"""Calculate the size of a PDU.
:param buffer: A buffer containing the data that have been received.
:returns: The number of bytes in the PDU.
:raises NotImplementedException:
"""
if hasattr(cls, "_rtu_frame_size"):
return cls._rtu_frame_size
if hasattr(cls, "_rtu_byte_count_pos"):
return rtuFrameSize(buffer, cls._rtu_byte_count_pos)
raise NotImplementedException(
f"Cannot determine RTU frame size for {cls.__name__}"
)
class ModbusRequest(ModbusPDU):
"""Base class for a modbus request PDU."""
function_code = -1
def __init__(self, slave, transaction, protocol, skip_encode):
"""Proxy to the lower level initializer.
:param slave: Modbus slave slave ID
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.fut = None
def doException(self, exception):
"""Build an error response based on the function.
:param exception: The exception to return
:raises: An exception response
"""
exc = ExceptionResponse(self.function_code, exception)
Log.error("Exception response {}", exc)
return exc
class ModbusResponse(ModbusPDU):
"""Base class for a modbus response PDU.
.. attribute:: should_respond
A flag that indicates if this response returns a result back
to the client issuing the request
.. attribute:: _rtu_frame_size
Indicates the size of the modbus rtu response used for
calculating how much to read.
"""
should_respond = True
function_code = 0x00
def __init__(self, slave, transaction, protocol, skip_encode):
"""Proxy the lower level initializer.
:param slave: Modbus slave slave ID
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.bits = []
self.registers = []
self.request = None
def isError(self) -> bool:
"""Check if the error is a success or failure."""
return self.function_code > 0x80
# --------------------------------------------------------------------------- #
# Exception PDUs
# --------------------------------------------------------------------------- #
class ModbusExceptions: # pylint: disable=too-few-public-methods
"""An enumeration of the valid modbus exceptions."""
IllegalFunction = 0x01
IllegalAddress = 0x02
IllegalValue = 0x03
SlaveFailure = 0x04
Acknowledge = 0x05
SlaveBusy = 0x06
NegativeAcknowledge = 0x07
MemoryParityError = 0x08
GatewayPathUnavailable = 0x0A
GatewayNoResponse = 0x0B
@classmethod
def decode(cls, code):
"""Give an error code, translate it to a string error name.
:param code: The code number to translate
"""
values = {
v: k
for k, v in iter(cls.__dict__.items())
if not k.startswith("__") and not callable(v)
}
return values.get(code, None)
class ExceptionResponse(ModbusResponse):
"""Base class for a modbus exception PDU."""
ExceptionOffset = 0x80
_rtu_frame_size = 5
def __init__(self, function_code, exception_code=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize the modbus exception response.
:param function_code: The function to build an exception response for
:param exception_code: The specific modbus exception to return
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.original_code = function_code
self.function_code = function_code | self.ExceptionOffset
self.exception_code = exception_code
def encode(self):
"""Encode a modbus exception response.
:returns: The encoded exception packet
"""
return struct.pack(">B", self.exception_code)
def decode(self, data):
"""Decode a modbus exception response.
:param data: The packet data to decode
"""
self.exception_code = int(data[0])
def __str__(self):
"""Build a representation of an exception response.
:returns: The string representation of an exception response
"""
message = ModbusExceptions.decode(self.exception_code)
parameters = (self.function_code, self.original_code, message)
return (
"Exception Response(%d, %d, %s)" # pylint: disable=consider-using-f-string
% parameters
)
class IllegalFunctionRequest(ModbusRequest):
"""Define the Modbus slave exception type "Illegal Function".
This exception code is returned if the slave::
- does not implement the function code **or**
- is not in a state that allows it to process the function
"""
ErrorCode = 1
def __init__(self, function_code, xslave, xtransaction, xprotocol, xskip_encode):
"""Initialize a IllegalFunctionRequest.
:param function_code: The function we are erroring on
"""
super().__init__(xslave, xtransaction, xprotocol, xskip_encode)
self.function_code = function_code
def decode(self, _data):
"""Decode so this failure will run correctly."""
def encode(self):
"""Decode so this failure will run correctly."""
async def execute(self, _context):
"""Build an illegal function request error response.
:returns: The error response packet
"""
return ExceptionResponse(self.function_code, self.ErrorCode)

View File

@@ -0,0 +1,367 @@
"""Register Reading Request/Response."""
# pylint: disable=missing-type-doc
import struct
from pymodbus.exceptions import ModbusIOException
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
class ReadRegistersRequestBase(ModbusRequest):
"""Base class for reading a modbus register."""
_rtu_frame_size = 8
def __init__(self, address, count, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start the read from
:param count: The number of registers to read
:param slave: Modbus slave slave ID
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
self.count = count
def encode(self):
"""Encode the request packet.
:return: The encoded packet
"""
return struct.pack(">HH", self.address, self.count)
def decode(self, data):
"""Decode a register request packet.
:param data: The request to decode
"""
self.address, self.count = struct.unpack(">HH", data)
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Byte Count(1 byte) + 2 * Quantity of Coils (n Bytes).
"""
return 1 + 1 + 2 * self.count
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"{self.__class__.__name__} ({self.address},{self.count})"
class ReadRegistersResponseBase(ModbusResponse):
"""Base class for responding to a modbus register read.
The requested registers can be found in the .registers list.
"""
_rtu_byte_count_pos = 2
def __init__(self, values, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The values to write to
:param slave: Modbus slave slave ID
"""
super().__init__(slave, transaction, protocol, skip_encode)
#: A list of register values
self.registers = values or []
def encode(self):
"""Encode the response packet.
:returns: The encoded packet
"""
result = struct.pack(">B", len(self.registers) * 2)
for register in self.registers:
result += struct.pack(">H", register)
return result
def decode(self, data):
"""Decode a register response packet.
:param data: The request to decode
"""
byte_count = int(data[0])
if byte_count < 2 or byte_count > 252 or byte_count % 2 == 1 or byte_count != len(data) - 1:
raise ModbusIOException(f"Invalid response {data} has byte count of {byte_count}")
self.registers = []
for i in range(1, byte_count + 1, 2):
self.registers.append(struct.unpack(">H", data[i : i + 2])[0])
def getRegister(self, index):
"""Get the requested register.
:param index: The indexed register to retrieve
:returns: The request register
"""
return self.registers[index]
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"{self.__class__.__name__} ({len(self.registers)})"
class ReadHoldingRegistersRequest(ReadRegistersRequestBase):
"""Read holding registers.
This function code is used to read the contents of a contiguous block
of holding registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore registers numbered
1-16 are addressed as 0-15.
"""
function_code = 3
function_code_name = "read_holding_registers"
def __init__(self, address=None, count=None, slave=1, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance of the request.
:param address: The starting address to read from
:param count: The number of registers to read from address
:param slave: Modbus slave slave ID
"""
super().__init__(address, count, slave, transaction, protocol, skip_encode)
async def execute(self, context):
"""Run a read holding request against a datastore.
:param context: The datastore to request from
:returns: An initialized :py:class:`~pymodbus.register_read_message.ReadHoldingRegistersResponse`
"""
if not (1 <= self.count <= 0x7D):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
values = await context.async_getValues(
self.function_code, self.address, self.count
)
return ReadHoldingRegistersResponse(values)
class ReadHoldingRegistersResponse(ReadRegistersResponseBase):
"""Read holding registers.
This function code is used to read the contents of a contiguous block
of holding registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore registers numbered
1-16 are addressed as 0-15.
The requested registers can be found in the .registers list.
"""
function_code = 3
def __init__(self, values=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new response instance.
:param values: The resulting register values
"""
super().__init__(values, slave, transaction, protocol, skip_encode)
class ReadInputRegistersRequest(ReadRegistersRequestBase):
"""Read input registers.
This function code is used to read from 1 to approx. 125 contiguous
input registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore input registers
numbered 1-16 are addressed as 0-15.
"""
function_code = 4
function_code_name = "read_input_registers"
def __init__(self, address=None, count=None, slave=1, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance of the request.
:param address: The starting address to read from
:param count: The number of registers to read from address
:param slave: Modbus slave slave ID
"""
super().__init__(address, count, slave, transaction, protocol, skip_encode)
async def execute(self, context):
"""Run a read input request against a datastore.
:param context: The datastore to request from
:returns: An initialized :py:class:`~pymodbus.register_read_message.ReadInputRegistersResponse`
"""
if not (1 <= self.count <= 0x7D):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
values = await context.async_getValues(
self.function_code, self.address, self.count
)
return ReadInputRegistersResponse(values)
class ReadInputRegistersResponse(ReadRegistersResponseBase):
"""Read/write input registers.
This function code is used to read from 1 to approx. 125 contiguous
input registers in a remote device. The Request PDU specifies the
starting register address and the number of registers. In the PDU
Registers are addressed starting at zero. Therefore input registers
numbered 1-16 are addressed as 0-15.
The requested registers can be found in the .registers list.
"""
function_code = 4
def __init__(self, values=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new response instance.
:param values: The resulting register values
"""
super().__init__(values, slave, transaction, protocol, skip_encode)
class ReadWriteMultipleRegistersRequest(ModbusRequest):
"""Read/write multiple registers.
This function code performs a combination of one read operation and one
write operation in a single MODBUS transaction. The write
operation is performed before the read.
Holding registers are addressed starting at zero. Therefore holding
registers 1-16 are addressed in the PDU as 0-15.
The request specifies the starting address and number of holding
registers to be read as well as the starting address, number of holding
registers, and the data to be written. The byte count specifies the
number of bytes to follow in the write data field."
"""
function_code = 23
function_code_name = "read_write_multiple_registers"
_rtu_byte_count_pos = 10
def __init__(self, read_address=0x00, read_count=0, write_address=0x00, write_registers=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new request message.
:param read_address: The address to start reading from
:param read_count: The number of registers to read from address
:param write_address: The address to start writing to
:param write_registers: The registers to write to the specified address
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.read_address = read_address
self.read_count = read_count
self.write_address = write_address
self.write_registers = write_registers
if not hasattr(self.write_registers, "__iter__"):
self.write_registers = [self.write_registers]
self.write_count = len(self.write_registers)
self.write_byte_count = self.write_count * 2
def encode(self):
"""Encode the request packet.
:returns: The encoded packet
"""
result = struct.pack(
">HHHHB",
self.read_address,
self.read_count,
self.write_address,
self.write_count,
self.write_byte_count,
)
for register in self.write_registers:
result += struct.pack(">H", register)
return result
def decode(self, data):
"""Decode the register request packet.
:param data: The request to decode
"""
(
self.read_address,
self.read_count,
self.write_address,
self.write_count,
self.write_byte_count,
) = struct.unpack(">HHHHB", data[:9])
self.write_registers = []
for i in range(9, self.write_byte_count + 9, 2):
register = struct.unpack(">H", data[i : i + 2])[0]
self.write_registers.append(register)
async def execute(self, context):
"""Run a write single register request against a datastore.
:param context: The datastore to request from
:returns: An initialized :py:class:`~pymodbus.register_read_message.ReadWriteMultipleRegistersResponse`
"""
if not (1 <= self.read_count <= 0x07D):
return self.doException(merror.IllegalValue)
if not 1 <= self.write_count <= 0x079:
return self.doException(merror.IllegalValue)
if self.write_byte_count != self.write_count * 2:
return self.doException(merror.IllegalValue)
if not context.validate(
self.function_code, self.write_address, self.write_count
):
return self.doException(merror.IllegalAddress)
if not context.validate(self.function_code, self.read_address, self.read_count):
return self.doException(merror.IllegalAddress)
await context.async_setValues(
self.function_code, self.write_address, self.write_registers
)
registers = await context.async_getValues(
self.function_code, self.read_address, self.read_count
)
return ReadWriteMultipleRegistersResponse(registers)
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Byte Count(1 byte) + 2 * Quantity of Coils (n Bytes)
:return:
"""
return 1 + 1 + 2 * self.read_count
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
params = (
self.read_address,
self.read_count,
self.write_address,
self.write_count,
)
return (
"ReadWriteNRegisterRequest R(%d,%d) W(%d,%d)" # pylint: disable=consider-using-f-string
% params
)
class ReadWriteMultipleRegistersResponse(ReadHoldingRegistersResponse):
"""Read/write multiple registers.
The normal response contains the data from the group of registers that
were read. The byte count field specifies the quantity of bytes to
follow in the read data field.
The requested registers can be found in the .registers list.
"""
function_code = 23

View File

@@ -0,0 +1,369 @@
"""Register Writing Request/Response Messages."""
# pylint: disable=missing-type-doc
import struct
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
class WriteSingleRegisterRequest(ModbusRequest):
"""This function code is used to write a single holding register in a remote device.
The Request PDU specifies the address of the register to
be written. Registers are addressed starting at zero. Therefore register
numbered 1 is addressed as 0.
"""
function_code = 6
function_code_name = "write_register"
_rtu_frame_size = 8
def __init__(self, address=None, value=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance.
:param address: The address to start writing add
:param value: The values to write
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
self.value = value
def encode(self):
"""Encode a write single register packet packet request.
:returns: The encoded packet
"""
packet = struct.pack(">H", self.address)
if self.skip_encode:
packet += self.value
else:
packet += struct.pack(">H", self.value)
return packet
def decode(self, data):
"""Decode a write single register packet packet request.
:param data: The request to decode
"""
self.address, self.value = struct.unpack(">HH", data)
async def execute(self, context):
"""Run a write single register request against a datastore.
:param context: The datastore to request from
:returns: An initialized response, exception message otherwise
"""
if not 0 <= self.value <= 0xFFFF:
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, 1):
return self.doException(merror.IllegalAddress)
await context.async_setValues(
self.function_code, self.address, [self.value]
)
values = await context.async_getValues(self.function_code, self.address, 1)
return WriteSingleRegisterResponse(self.address, values[0])
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Register Address(2 byte) + Register Value (2 bytes)
:return:
"""
return 1 + 2 + 2
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
return f"WriteRegisterRequest {self.address}"
class WriteSingleRegisterResponse(ModbusResponse):
"""The normal response is an echo of the request.
Returned after the register contents have been written.
"""
function_code = 6
_rtu_frame_size = 8
def __init__(self, address=None, value=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start writing add
:param value: The values to write
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
self.value = value
def encode(self):
"""Encode a write single register packet packet request.
:returns: The encoded packet
"""
return struct.pack(">HH", self.address, self.value)
def decode(self, data):
"""Decode a write single register packet packet request.
:param data: The request to decode
"""
self.address, self.value = struct.unpack(">HH", data)
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Starting Address (2 byte) + And_mask (2 Bytes) + OrMask (2 Bytes)
:return:
"""
return 1 + 2 + 2 + 2
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
params = (self.address, self.value)
return (
"WriteRegisterResponse %d => %d" # pylint: disable=consider-using-f-string
% params
)
# ---------------------------------------------------------------------------#
# Write Multiple Registers
# ---------------------------------------------------------------------------#
class WriteMultipleRegistersRequest(ModbusRequest):
"""This function code is used to write a block.
Of contiguous registers (1 to approx. 120 registers) in a remote device.
The requested written values are specified in the request data field.
Data is packed as two bytes per register.
"""
function_code = 16
function_code_name = "write_registers"
_rtu_byte_count_pos = 6
_pdu_length = 5 # func + adress1 + adress2 + outputQuant1 + outputQuant2
def __init__(self, address=None, values=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance.
:param address: The address to start writing to
:param values: The values to write
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
if values is None:
values = []
elif not hasattr(values, "__iter__"):
values = [values]
self.values = values
self.count = len(self.values)
self.byte_count = self.count * 2
def encode(self):
"""Encode a write single register packet packet request.
:returns: The encoded packet
"""
packet = struct.pack(">HHB", self.address, self.count, self.byte_count)
if self.skip_encode:
return packet + b"".join(self.values)
for value in self.values:
packet += struct.pack(">H", value)
return packet
def decode(self, data):
"""Decode a write single register packet packet request.
:param data: The request to decode
"""
self.address, self.count, self.byte_count = struct.unpack(">HHB", data[:5])
self.values = [] # reset
for idx in range(5, (self.count * 2) + 5, 2):
self.values.append(struct.unpack(">H", data[idx : idx + 2])[0])
async def execute(self, context):
"""Run a write single register request against a datastore.
:param context: The datastore to request from
:returns: An initialized response, exception message otherwise
"""
if not 1 <= self.count <= 0x07B:
return self.doException(merror.IllegalValue)
if self.byte_count != self.count * 2:
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
return self.doException(merror.IllegalAddress)
await context.async_setValues(
self.function_code, self.address, self.values
)
return WriteMultipleRegistersResponse(self.address, self.count)
def get_response_pdu_size(self):
"""Get response pdu size.
Func_code (1 byte) + Starting Address (2 byte) + Quantity of Registers (2 Bytes)
:return:
"""
return 1 + 2 + 2
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
params = (self.address, self.count)
return (
"WriteMultipleRegisterRequest %d => %d" # pylint: disable=consider-using-f-string
% params
)
class WriteMultipleRegistersResponse(ModbusResponse):
"""The normal response returns the function code.
Starting address, and quantity of registers written.
"""
function_code = 16
_rtu_frame_size = 8
def __init__(self, address=None, count=None, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start writing to
:param count: The number of registers to write to
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
self.count = count
def encode(self):
"""Encode a write single register packet packet request.
:returns: The encoded packet
"""
return struct.pack(">HH", self.address, self.count)
def decode(self, data):
"""Decode a write single register packet packet request.
:param data: The request to decode
"""
self.address, self.count = struct.unpack(">HH", data)
def __str__(self):
"""Return a string representation of the instance.
:returns: A string representation of the instance
"""
params = (self.address, self.count)
return (
"WriteMultipleRegisterResponse (%d,%d)" # pylint: disable=consider-using-f-string
% params
)
class MaskWriteRegisterRequest(ModbusRequest):
"""This function code is used to modify the contents.
Of a specified holding register using a combination of an AND mask,
an OR mask, and the register's current contents.
The function can be used to set or clear individual bits in the register.
"""
function_code = 0x16
function_code_name = "mask_write_register"
_rtu_frame_size = 10
def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The mask pointer address (0x0000 to 0xffff)
:param and_mask: The and bitmask to apply to the register address
:param or_mask: The or bitmask to apply to the register address
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
self.and_mask = and_mask
self.or_mask = or_mask
def encode(self):
"""Encode the request packet.
:returns: The byte encoded packet
"""
return struct.pack(">HHH", self.address, self.and_mask, self.or_mask)
def decode(self, data):
"""Decode the incoming request.
:param data: The data to decode into the address
"""
self.address, self.and_mask, self.or_mask = struct.unpack(">HHH", data)
async def execute(self, context):
"""Run a mask write register request against the store.
:param context: The datastore to request from
:returns: The populated response
"""
if not 0x0000 <= self.and_mask <= 0xFFFF:
return self.doException(merror.IllegalValue)
if not 0x0000 <= self.or_mask <= 0xFFFF:
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, 1):
return self.doException(merror.IllegalAddress)
values = (await context.async_getValues(self.function_code, self.address, 1))[0]
values = (values & self.and_mask) | (self.or_mask & ~self.and_mask)
await context.async_setValues(
self.function_code, self.address, [values]
)
return MaskWriteRegisterResponse(self.address, self.and_mask, self.or_mask)
class MaskWriteRegisterResponse(ModbusResponse):
"""The normal response is an echo of the request.
The response is returned after the register has been written.
"""
function_code = 0x16
_rtu_frame_size = 10
def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, transaction=0, protocol=0, skip_encode=False):
"""Initialize new instance.
:param address: The mask pointer address (0x0000 to 0xffff)
:param and_mask: The and bitmask applied to the register address
:param or_mask: The or bitmask applied to the register address
"""
super().__init__(slave, transaction, protocol, skip_encode)
self.address = address
self.and_mask = and_mask
self.or_mask = or_mask
def encode(self):
"""Encode the response.
:returns: The byte encoded message
"""
return struct.pack(">HHH", self.address, self.and_mask, self.or_mask)
def decode(self, data):
"""Decode a the response.
:param data: The packet data to decode
"""
self.address, self.and_mask, self.or_mask = struct.unpack(">HHH", data)

View File

@@ -0,0 +1,42 @@
"""Server.
import external classes, to make them easier to use:
"""
__all__ = [
"get_simulator_commandline",
"ModbusSerialServer",
"ModbusSimulatorServer",
"ModbusTcpServer",
"ModbusTlsServer",
"ModbusUdpServer",
"ServerAsyncStop",
"ServerStop",
"StartAsyncSerialServer",
"StartAsyncTcpServer",
"StartAsyncTlsServer",
"StartAsyncUdpServer",
"StartSerialServer",
"StartTcpServer",
"StartTlsServer",
"StartUdpServer",
]
from pymodbus.server.async_io import (
ModbusSerialServer,
ModbusTcpServer,
ModbusTlsServer,
ModbusUdpServer,
ServerAsyncStop,
ServerStop,
StartAsyncSerialServer,
StartAsyncTcpServer,
StartAsyncTlsServer,
StartAsyncUdpServer,
StartSerialServer,
StartTcpServer,
StartTlsServer,
StartUdpServer,
)
from pymodbus.server.simulator.http_server import ModbusSimulatorServer
from pymodbus.server.simulator.main import get_commandline as get_simulator_commandline

View File

@@ -0,0 +1,733 @@
"""Implementation of a Threaded Modbus Server."""
# pylint: disable=missing-type-doc
from __future__ import annotations
import asyncio
import os
import traceback
from contextlib import suppress
from pymodbus.datastore import ModbusServerContext
from pymodbus.device import ModbusControlBlock, ModbusDeviceIdentification
from pymodbus.exceptions import NoSuchSlaveException
from pymodbus.factory import ServerDecoder
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerType, ModbusFramer
from pymodbus.logging import Log
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.transport import CommParams, CommType, ModbusProtocol
# --------------------------------------------------------------------------- #
# Protocol Handlers
# --------------------------------------------------------------------------- #
class ModbusServerRequestHandler(ModbusProtocol):
"""Implements modbus slave wire protocol.
This uses the asyncio.Protocol to implement the server protocol.
When a connection is established, a callback is called.
This callback will setup the connection and
create and schedule an asyncio.Task and assign it to running_task.
"""
def __init__(self, owner):
"""Initialize."""
params = CommParams(
comm_name="server",
comm_type=owner.comm_params.comm_type,
reconnect_delay=0.0,
reconnect_delay_max=0.0,
timeout_connect=0.0,
host=owner.comm_params.source_address[0],
port=owner.comm_params.source_address[1],
)
super().__init__(params, False)
self.server = owner
self.running = False
self.receive_queue: asyncio.Queue = asyncio.Queue()
self.handler_task = None # coroutine to be run on asyncio loop
self.framer: ModbusFramer
self.loop = asyncio.get_running_loop()
def _log_exception(self):
"""Show log exception."""
Log.debug(
"Handler for stream [{}] has been canceled", self.comm_params.comm_name
)
def callback_new_connection(self) -> ModbusProtocol:
"""Call when listener receive new connection request."""
Log.debug("callback_new_connection called")
return ModbusServerRequestHandler(self)
def callback_connected(self) -> None:
"""Call when connection is succcesfull."""
try:
self.running = True
self.framer = self.server.framer(
self.server.decoder,
client=None,
)
# schedule the connection handler on the event loop
self.handler_task = asyncio.create_task(self.handle())
self.handler_task.set_name("server connection handler")
except Exception as exc: # pylint: disable=broad-except
Log.error(
"Server callback_connected exception: {}; {}",
exc,
traceback.format_exc(),
)
def callback_disconnected(self, call_exc: Exception | None) -> None:
"""Call when connection is lost."""
try:
if self.handler_task:
self.handler_task.cancel()
if hasattr(self.server, "on_connection_lost"):
self.server.on_connection_lost()
if call_exc is None:
self._log_exception()
else:
Log.debug(
"Client Disconnection {} due to {}",
self.comm_params.comm_name,
call_exc,
)
self.running = False
except Exception as exc: # pylint: disable=broad-except
Log.error(
"Datastore unable to fulfill request: {}; {}",
exc,
traceback.format_exc(),
)
async def inner_handle(self):
"""Handle handler."""
slaves = self.server.context.slaves()
# this is an asyncio.Queue await, it will never fail
data = await self._recv_()
if isinstance(data, tuple):
# addr is populated when talking over UDP
data, *addr = data
else:
addr = [None]
# if broadcast is enabled make sure to
# process requests to address 0
if self.server.broadcast_enable:
if 0 not in slaves:
slaves.append(0)
Log.debug("Handling data: {}", data, ":hex")
single = self.server.context.single
self.framer.processIncomingPacket(
data=data,
callback=lambda x: self.execute(x, *addr),
slave=slaves,
single=single,
)
async def handle(self) -> None:
"""Coroutine which represents a single master <=> slave conversation.
Once the client connection is established, the data chunks will be
fed to this coroutine via the asyncio.Queue object which is fed by
the ModbusServerRequestHandler class's callback Future.
This callback future gets data from either asyncio.BaseProtocol.data_received
or asyncio.DatagramProtocol.datagram_received.
This function will execute without blocking in the while-loop and
yield to the asyncio event loop when the frame is exhausted.
As a result, multiple clients can be interleaved without any
interference between them.
"""
while self.running:
try:
await self.inner_handle()
except asyncio.CancelledError:
# catch and ignore cancellation errors
if self.running:
self._log_exception()
self.running = False
except Exception as exc: # pylint: disable=broad-except
# force TCP socket termination as processIncomingPacket
# should handle application layer errors
Log.error(
'Unknown exception "{}" on stream {} forcing disconnect',
exc,
self.comm_params.comm_name,
)
self.close()
self.callback_disconnected(exc)
def execute(self, request, *addr):
"""Call with the resulting message.
:param request: The decoded request message
:param addr: the address
"""
if self.server.request_tracer:
self.server.request_tracer(request, *addr)
asyncio.run_coroutine_threadsafe(self._async_execute(request, *addr), self.loop)
async def _async_execute(self, request, *addr):
broadcast = False
try:
if self.server.broadcast_enable and not request.slave_id:
broadcast = True
# if broadcasting then execute on all slave contexts,
# note response will be ignored
for slave_id in self.server.context.slaves():
response = await request.execute(self.server.context[slave_id])
else:
context = self.server.context[request.slave_id]
response = await request.execute(context)
except NoSuchSlaveException:
Log.error("requested slave does not exist: {}", request.slave_id)
if self.server.ignore_missing_slaves:
return # the client will simply timeout waiting for a response
response = request.doException(merror.GatewayNoResponse)
except Exception as exc: # pylint: disable=broad-except
Log.error(
"Datastore unable to fulfill request: {}; {}",
exc,
traceback.format_exc(),
)
response = request.doException(merror.SlaveFailure)
# no response when broadcasting
if not broadcast:
response.transaction_id = request.transaction_id
response.slave_id = request.slave_id
skip_encoding = False
if self.server.response_manipulator:
response, skip_encoding = self.server.response_manipulator(response)
self.server_send(response, *addr, skip_encoding=skip_encoding)
def server_send(self, message, addr, **kwargs):
"""Send message."""
if kwargs.get("skip_encoding", False):
self.send(message, addr=addr)
elif message.should_respond:
pdu = self.framer.buildPacket(message)
self.send(pdu, addr=addr)
else:
Log.debug("Skipping sending response!!")
async def _recv_(self):
"""Receive data from the network."""
try:
result = await self.receive_queue.get()
except RuntimeError:
Log.error("Event loop is closed")
result = None
return result
def callback_data(self, data: bytes, addr: tuple | None = ()) -> int:
"""Handle received data."""
if addr != ():
self.receive_queue.put_nowait((data, addr))
else:
self.receive_queue.put_nowait(data)
return len(data)
# --------------------------------------------------------------------------- #
# Server Implementations
# --------------------------------------------------------------------------- #
class ModbusBaseServer(ModbusProtocol):
"""Common functionality for all server classes."""
def __init__(
self,
params: CommParams,
context,
ignore_missing_slaves,
broadcast_enable,
response_manipulator,
request_tracer,
identity,
framer,
) -> None:
"""Initialize base server."""
super().__init__(
params,
True,
)
self.loop = asyncio.get_running_loop()
self.decoder = ServerDecoder()
self.context = context or ModbusServerContext()
self.control = ModbusControlBlock()
self.ignore_missing_slaves = ignore_missing_slaves
self.broadcast_enable = broadcast_enable
self.response_manipulator = response_manipulator
self.request_tracer = request_tracer
self.handle_local_echo = False
if isinstance(identity, ModbusDeviceIdentification):
self.control.Identity.update(identity)
self.framer = FRAMER_NAME_TO_CLASS.get(framer, framer)
self.serving: asyncio.Future = asyncio.Future()
def callback_new_connection(self):
"""Handle incoming connect."""
return ModbusServerRequestHandler(self)
async def shutdown(self):
"""Close server."""
if not self.serving.done():
self.serving.set_result(True)
self.close()
async def serve_forever(self):
"""Start endless loop."""
if self.transport:
raise RuntimeError(
"Can't call serve_forever on an already running server object"
)
await self.listen()
Log.info("Server listening.")
await self.serving
Log.info("Server graceful shutdown.")
def callback_connected(self) -> None:
"""Call when connection is succcesfull."""
def callback_disconnected(self, exc: Exception | None) -> None:
"""Call when connection is lost."""
Log.debug("callback_disconnected called: {}", exc)
def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data."""
Log.debug("callback_data called: {} addr={}", data, ":hex", addr)
return 0
class ModbusTcpServer(ModbusBaseServer):
"""A modbus threaded tcp socket server.
We inherit and overload the socket server so that we
can control the client threads as well as have a single
server context instance.
"""
def __init__(
self,
context,
framer=FramerType.SOCKET,
identity=None,
address=("", 502),
ignore_missing_slaves=False,
broadcast_enable=False,
response_manipulator=None,
request_tracer=None,
):
"""Initialize the socket server.
If the identify structure is not passed in, the ModbusControlBlock
uses its own empty structure.
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure
:param address: An optional (interface, port) to bind to.
:param ignore_missing_slaves: True to not send errors on a request
to a missing slave
:param broadcast_enable: True to treat slave_id 0 as broadcast address,
False to treat 0 as any other slave_id
:param response_manipulator: Callback method for manipulating the
response
:param request_tracer: Callback method for tracing
"""
params = getattr(
self,
"tls_setup",
CommParams(
comm_type=CommType.TCP,
comm_name="server_listener",
reconnect_delay=0.0,
reconnect_delay_max=0.0,
timeout_connect=0.0,
),
)
params.source_address = address
super().__init__(
params,
context,
ignore_missing_slaves,
broadcast_enable,
response_manipulator,
request_tracer,
identity,
framer,
)
class ModbusTlsServer(ModbusTcpServer):
"""A modbus threaded tls socket server.
We inherit and overload the socket server so that we
can control the client threads as well as have a single
server context instance.
"""
def __init__( # pylint: disable=too-many-arguments
self,
context,
framer=FramerType.TLS,
identity=None,
address=("", 502),
sslctx=None,
certfile=None,
keyfile=None,
password=None,
ignore_missing_slaves=False,
broadcast_enable=False,
response_manipulator=None,
request_tracer=None,
):
"""Overloaded initializer for the socket server.
If the identify structure is not passed in, the ModbusControlBlock
uses its own empty structure.
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure
:param address: An optional (interface, port) to bind to.
:param sslctx: The SSLContext to use for TLS (default None and auto
create)
:param certfile: The cert file path for TLS (used if sslctx is None)
:param keyfile: The key file path for TLS (used if sslctx is None)
:param password: The password for for decrypting the private key file
:param ignore_missing_slaves: True to not send errors on a request
to a missing slave
:param broadcast_enable: True to treat slave_id 0 as broadcast address,
False to treat 0 as any other slave_id
:param response_manipulator: Callback method for
manipulating the response
"""
self.tls_setup = CommParams(
comm_type=CommType.TLS,
comm_name="server_listener",
reconnect_delay=0.0,
reconnect_delay_max=0.0,
timeout_connect=0.0,
sslctx=CommParams.generate_ssl(
True, certfile, keyfile, password, sslctx=sslctx
),
)
super().__init__(
context,
framer=framer,
identity=identity,
address=address,
ignore_missing_slaves=ignore_missing_slaves,
broadcast_enable=broadcast_enable,
response_manipulator=response_manipulator,
request_tracer=request_tracer,
)
class ModbusUdpServer(ModbusBaseServer):
"""A modbus threaded udp socket server.
We inherit and overload the socket server so that we
can control the client threads as well as have a single
server context instance.
"""
def __init__(
self,
context,
framer=FramerType.SOCKET,
identity=None,
address=("", 502),
ignore_missing_slaves=False,
broadcast_enable=False,
response_manipulator=None,
request_tracer=None,
):
"""Overloaded initializer for the socket server.
If the identify structure is not passed in, the ModbusControlBlock
uses its own empty structure.
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure
:param address: An optional (interface, port) to bind to.
:param ignore_missing_slaves: True to not send errors on a request
to a missing slave
:param broadcast_enable: True to treat slave_id 0 as broadcast address,
False to treat 0 as any other slave_id
:param response_manipulator: Callback method for
manipulating the response
:param request_tracer: Callback method for tracing
"""
# ----------------
super().__init__(
CommParams(
comm_type=CommType.UDP,
comm_name="server_listener",
source_address=address,
reconnect_delay=0.0,
reconnect_delay_max=0.0,
timeout_connect=0.0,
),
context,
ignore_missing_slaves,
broadcast_enable,
response_manipulator,
request_tracer,
identity,
framer,
)
class ModbusSerialServer(ModbusBaseServer):
"""A modbus threaded serial socket server.
We inherit and overload the socket server so that we
can control the client threads as well as have a single
server context instance.
"""
def __init__(
self, context, framer=FramerType.RTU, identity=None, **kwargs
):
"""Initialize the socket server.
If the identity structure is not passed in, the ModbusControlBlock
uses its own empty structure.
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use, default ModbusRtuFramer
:param identity: An optional identify structure
:param port: The serial port to attach to
:param stopbits: The number of stop bits to use
:param bytesize: The bytesize of the serial messages
:param parity: Which kind of parity to use
:param baudrate: The baud rate to use for the serial device
:param timeout: The timeout to use for the serial device
:param handle_local_echo: (optional) Discard local echo from dongle.
:param ignore_missing_slaves: True to not send errors on a request
to a missing slave
:param broadcast_enable: True to treat slave_id 0 as broadcast address,
False to treat 0 as any other slave_id
:param reconnect_delay: reconnect delay in seconds
:param response_manipulator: Callback method for
manipulating the response
:param request_tracer: Callback method for tracing
"""
super().__init__(
params=CommParams(
comm_type=CommType.SERIAL,
comm_name="server_listener",
reconnect_delay=kwargs.get("reconnect_delay", 2),
reconnect_delay_max=0.0,
timeout_connect=kwargs.get("timeout", 3),
source_address=(kwargs.get("port", 0), 0),
bytesize=kwargs.get("bytesize", 8),
parity=kwargs.get("parity", "N"),
baudrate=kwargs.get("baudrate", 19200),
stopbits=kwargs.get("stopbits", 1),
),
context=context,
ignore_missing_slaves=kwargs.get("ignore_missing_slaves", False),
broadcast_enable=kwargs.get("broadcast_enable", False),
response_manipulator=kwargs.get("response_manipulator", None),
request_tracer=kwargs.get("request_tracer", None),
identity=kwargs.get("identity", None),
framer=framer,
)
self.handle_local_echo = kwargs.get("handle_local_echo", False)
# --------------------------------------------------------------------------- #
# Creation Factories
# --------------------------------------------------------------------------- #
class _serverList:
"""Maintains information about the active server.
:meta private:
"""
active_server: ModbusTcpServer | ModbusUdpServer | ModbusSerialServer
def __init__(self, server):
"""Register new server."""
self.server = server
self.loop = asyncio.get_event_loop()
@classmethod
async def run(cls, server, custom_functions) -> None:
"""Help starting/stopping server."""
for func in custom_functions:
server.decoder.register(func)
cls.active_server = _serverList(server) # type: ignore[assignment]
with suppress(asyncio.exceptions.CancelledError):
await server.serve_forever()
@classmethod
async def async_stop(cls) -> None:
"""Wait for server stop."""
if not cls.active_server:
raise RuntimeError("ServerAsyncStop called without server task active.")
await cls.active_server.server.shutdown() # type: ignore[union-attr]
cls.active_server = None # type: ignore[assignment]
@classmethod
def stop(cls):
"""Wait for server stop."""
if not cls.active_server:
Log.info("ServerStop called without server task active.")
return
if not cls.active_server.loop.is_running():
Log.info("ServerStop called with loop stopped.")
return
future = asyncio.run_coroutine_threadsafe(cls.async_stop(), cls.active_server.loop)
future.result(timeout=10 if os.name == 'nt' else 0.1)
async def StartAsyncTcpServer( # pylint: disable=invalid-name,dangerous-default-value
context=None,
identity=None,
address=None,
custom_functions=[],
**kwargs,
):
"""Start and run a tcp modbus server.
:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param address: An optional (interface, port) to bind to.
:param custom_functions: An optional list of custom function classes
supported by server instance.
:param kwargs: The rest
"""
kwargs.pop("host", None)
server = ModbusTcpServer(
context, kwargs.pop("framer", FramerType.SOCKET), identity, address, **kwargs
)
await _serverList.run(server, custom_functions)
async def StartAsyncTlsServer( # pylint: disable=invalid-name,dangerous-default-value
context=None,
identity=None,
address=None,
sslctx=None,
certfile=None,
keyfile=None,
password=None,
custom_functions=[],
**kwargs,
):
"""Start and run a tls modbus server.
:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param address: An optional (interface, port) to bind to.
:param sslctx: The SSLContext to use for TLS (default None and auto create)
:param certfile: The cert file path for TLS (used if sslctx is None)
:param keyfile: The key file path for TLS (used if sslctx is None)
:param password: The password for for decrypting the private key file
:param custom_functions: An optional list of custom function classes
supported by server instance.
:param kwargs: The rest
"""
kwargs.pop("host", None)
server = ModbusTlsServer(
context,
kwargs.pop("framer", FramerType.TLS),
identity,
address,
sslctx,
certfile,
keyfile,
password,
**kwargs,
)
await _serverList.run(server, custom_functions)
async def StartAsyncUdpServer( # pylint: disable=invalid-name,dangerous-default-value
context=None,
identity=None,
address=None,
custom_functions=[],
**kwargs,
):
"""Start and run a udp modbus server.
:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param address: An optional (interface, port) to bind to.
:param custom_functions: An optional list of custom function classes
supported by server instance.
:param kwargs:
"""
kwargs.pop("host", None)
server = ModbusUdpServer(
context, kwargs.pop("framer", FramerType.SOCKET), identity, address, **kwargs
)
await _serverList.run(server, custom_functions)
async def StartAsyncSerialServer( # pylint: disable=invalid-name,dangerous-default-value
context=None,
identity=None,
custom_functions=[],
**kwargs,
):
"""Start and run a serial modbus server.
:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param custom_functions: An optional list of custom function classes
supported by server instance.
:param kwargs: The rest
"""
server = ModbusSerialServer(
context, kwargs.pop("framer", FramerType.RTU), identity=identity, **kwargs
)
await _serverList.run(server, custom_functions)
def StartSerialServer(**kwargs): # pylint: disable=invalid-name
"""Start and run a serial modbus server."""
return asyncio.run(StartAsyncSerialServer(**kwargs))
def StartTcpServer(**kwargs): # pylint: disable=invalid-name
"""Start and run a serial modbus server."""
return asyncio.run(StartAsyncTcpServer(**kwargs))
def StartTlsServer(**kwargs): # pylint: disable=invalid-name
"""Start and run a serial modbus server."""
return asyncio.run(StartAsyncTlsServer(**kwargs))
def StartUdpServer(**kwargs): # pylint: disable=invalid-name
"""Start and run a serial modbus server."""
return asyncio.run(StartAsyncUdpServer(**kwargs))
async def ServerAsyncStop(): # pylint: disable=invalid-name
"""Terminate server."""
await _serverList.async_stop()
def ServerStop(): # pylint: disable=invalid-name
"""Terminate server."""
_serverList.stop()

View File

@@ -0,0 +1 @@
"""Initialize."""

Some files were not shown because too many files have changed in this diff Show More