Skip to content

Commit

Permalink
Add typing to pdu base classes. (#2380)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Oct 15, 2024
1 parent 5c6050f commit 956997b
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 189 deletions.
8 changes: 4 additions & 4 deletions examples/client_custom_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from pymodbus import FramerType
from pymodbus.client import AsyncModbusTcpClient as ModbusClient
from pymodbus.pdu import ModbusExceptions, ModbusRequest, ModbusResponse
from pymodbus.pdu import ModbusExceptions, ModbusPDU, ModbusResponse
from pymodbus.pdu.bit_read_message import ReadCoilsRequest


Expand Down Expand Up @@ -62,15 +62,15 @@ def decode(self, data):
self.values.append(struct.unpack(">H", data[i : i + 2])[0])


class CustomModbusRequest(ModbusRequest):
class CustomRequest(ModbusPDU):
"""Custom modbus request."""

function_code = 55
_rtu_frame_size = 8

def __init__(self, address=None, slave=1, transaction=0, skip_encode=False):
"""Initialize."""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
self.count = 16

Expand Down Expand Up @@ -127,7 +127,7 @@ async def main(host="localhost", port=5020):
# new modbus function code.
client.register(CustomModbusResponse)
slave=1
request = CustomModbusRequest(32, slave=slave)
request = CustomRequest(32, slave=slave)
result = await client.execute(request)
print(result)

Expand Down
8 changes: 4 additions & 4 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pymodbus.factory import ClientDecoder
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerBase, FramerType
from pymodbus.logging import Log
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.pdu import ModbusPDU, ModbusResponse
from pymodbus.transaction import SyncModbusTransactionManager
from pymodbus.transport import CommParams
from pymodbus.utilities import ModbusTransactionState
Expand Down Expand Up @@ -82,7 +82,7 @@ def close(self) -> None:
"""Close connection."""
self.ctx.close()

def execute(self, request: ModbusRequest):
def execute(self, request: ModbusPDU):
"""Execute request and get response (call **sync/async**).
:param request: The request to process
Expand Down Expand Up @@ -123,7 +123,7 @@ async def async_execute(self, request) -> ModbusResponse:

return resp

def build_response(self, request: ModbusRequest):
def build_response(self, request: ModbusPDU):
"""Return a deferred response for the current request.
:meta private:
Expand Down Expand Up @@ -219,7 +219,7 @@ def idle_time(self) -> float:
return 0
return self.last_frame_end + self.silent_interval

def execute(self, request: ModbusRequest) -> ModbusResponse:
def execute(self, request: ModbusPDU) -> ModbusResponse:
"""Execute request and get response (call **sync/async**).
:param request: The request to process
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/client/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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
from pymodbus.pdu import ModbusPDU


T = TypeVar("T", covariant=False)
Expand Down Expand Up @@ -49,7 +49,7 @@ class ModbusClientMixin(Generic[T]): # pylint: disable=too-many-public-methods
def __init__(self):
"""Initialize."""

def execute(self, _request: ModbusRequest) -> T:
def execute(self, _request: ModbusPDU) -> T:
"""Execute request (code ???).
:raises ModbusException:
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ def register(self, function):
:param function: Custom function class to register
:raises MessageRegisterException:
"""
if not issubclass(function, pdu.ModbusRequest):
if not issubclass(function, pdu.ModbusPDU):
raise MessageRegisterException(
f'"{function.__class__.__name__}" is Not a valid Modbus Message'
". Class needs to be derived from "
"`pymodbus.pdu.ModbusRequest` "
"`pymodbus.pdu.ModbusPDU` "
)
self.lookup[function.function_code] = function
if hasattr(function, "sub_function_code"):
Expand Down
2 changes: 0 additions & 2 deletions pymodbus/pdu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"IllegalFunctionRequest",
"ModbusExceptions",
"ModbusPDU",
"ModbusRequest",
"ModbusResponse",
]

Expand All @@ -13,6 +12,5 @@
IllegalFunctionRequest,
ModbusExceptions,
ModbusPDU,
ModbusRequest,
ModbusResponse,
)
6 changes: 3 additions & 3 deletions pymodbus/pdu/bit_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import struct

from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.pdu import ModbusPDU, ModbusResponse
from pymodbus.utilities import pack_bitstring, unpack_bitstring


class ReadBitsRequestBase(ModbusRequest):
class ReadBitsRequestBase(ModbusPDU):
"""Base class for Messages Requesting bit values."""

_rtu_frame_size = 8
Expand All @@ -20,7 +20,7 @@ def __init__(self, address, count, slave, transaction, skip_encode):
:param count: The number of bits after "address" to read
:param slave: Modbus slave slave ID
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
self.count = count

Expand Down
10 changes: 5 additions & 5 deletions pymodbus/pdu/bit_write_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pymodbus.constants import ModbusStatus
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.pdu import ModbusPDU, ModbusResponse
from pymodbus.utilities import pack_bitstring, unpack_bitstring


Expand All @@ -22,7 +22,7 @@
_turn_coil_off = struct.pack(">H", ModbusStatus.OFF)


class WriteSingleCoilRequest(ModbusRequest):
class WriteSingleCoilRequest(ModbusPDU):
"""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
Expand All @@ -49,7 +49,7 @@ def __init__(self, address=None, value=None, slave=None, transaction=0, skip_enc
:param address: The variable address to write
:param value: The value to write at address
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
self.value = bool(value)

Expand Down Expand Up @@ -151,7 +151,7 @@ def __str__(self):
return f"WriteCoilResponse({self.address}) => {self.value}"


class WriteMultipleCoilsRequest(ModbusRequest):
class WriteMultipleCoilsRequest(ModbusPDU):
"""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
Expand All @@ -173,7 +173,7 @@ def __init__(self, address=None, values=None, slave=None, transaction=0, skip_en
:param address: The starting request address
:param values: The values to write
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
if values is None:
values = []
Expand Down
6 changes: 3 additions & 3 deletions pymodbus/pdu/diag_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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.pdu import ModbusPDU, ModbusResponse
from pymodbus.utilities import pack_bitstring


Expand All @@ -24,7 +24,7 @@
# ---------------------------------------------------------------------------#
# TODO Make sure all the data is decoded from the response # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class DiagnosticStatusRequest(ModbusRequest):
class DiagnosticStatusRequest(ModbusPDU):
"""This is a base class for all of the diagnostic request functions."""

function_code = 0x08
Expand All @@ -33,7 +33,7 @@ class DiagnosticStatusRequest(ModbusRequest):

def __init__(self, slave=1, transaction=0, skip_encode=False):
"""Initialize a diagnostic request."""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.message = None

def encode(self):
Expand Down
14 changes: 7 additions & 7 deletions pymodbus/pdu/file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import struct

from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.pdu import ModbusPDU, ModbusResponse


# ---------------------------------------------------------------------------#
Expand Down Expand Up @@ -61,7 +61,7 @@ def __repr__(self):
# ---------------------------------------------------------------------------#
# File Requests/Responses
# ---------------------------------------------------------------------------#
class ReadFileRecordRequest(ModbusRequest):
class ReadFileRecordRequest(ModbusPDU):
"""Read file record request.
This function code is used to perform a file record read. All request
Expand Down Expand Up @@ -94,7 +94,7 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):
:param records: The file record requests to be read
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.records = records or []

def encode(self):
Expand Down Expand Up @@ -197,7 +197,7 @@ def decode(self, data):
self.records.append(record)


class WriteFileRecordRequest(ModbusRequest):
class WriteFileRecordRequest(ModbusPDU):
"""Write file record request.
This function code is used to perform a file record write. All
Expand All @@ -215,7 +215,7 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):
:param records: The file record requests to be read
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.records = records or []

def encode(self):
Expand Down Expand Up @@ -321,7 +321,7 @@ def decode(self, data):
self.records.append(record)


class ReadFifoQueueRequest(ModbusRequest):
class ReadFifoQueueRequest(ModbusPDU):
"""Read fifo queue request.
This function code allows to read the contents of a First-In-First-Out
Expand All @@ -344,7 +344,7 @@ def __init__(self, address=0x0000, slave=1, transaction=0, skip_encode=False):
:param address: The fifo pointer address (0x0000 to 0xffff)
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
self.values = [] # this should be added to the context

Expand Down
6 changes: 3 additions & 3 deletions pymodbus/pdu/mei_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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
from pymodbus.pdu import ModbusPDU, ModbusResponse


_MCB = ModbusControlBlock()
Expand Down Expand Up @@ -35,7 +35,7 @@ def __init__(self, oid):
# ---------------------------------------------------------------------------#
# Read Device Information
# ---------------------------------------------------------------------------#
class ReadDeviceInformationRequest(ModbusRequest):
class ReadDeviceInformationRequest(ModbusPDU):
"""Read device information.
This function code allows reading the identification and additional
Expand All @@ -58,7 +58,7 @@ def __init__(self, read_code=None, object_id=0x00, slave=1, transaction=0, skip_
:param read_code: The device information read code
:param object_id: The object to read from
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.read_code = read_code or DeviceInformation.BASIC
self.object_id = object_id

Expand Down
18 changes: 9 additions & 9 deletions pymodbus/pdu/other_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pymodbus.constants import ModbusStatus
from pymodbus.device import DeviceInformationFactory, ModbusControlBlock
from pymodbus.pdu import ModbusRequest, ModbusResponse
from pymodbus.pdu import ModbusPDU, ModbusResponse


_MCB = ModbusControlBlock()
Expand All @@ -18,7 +18,7 @@
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class ReadExceptionStatusRequest(ModbusRequest):
class ReadExceptionStatusRequest(ModbusPDU):
"""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
Expand All @@ -32,7 +32,7 @@ class ReadExceptionStatusRequest(ModbusRequest):

def __init__(self, slave=None, transaction=0, skip_encode=0):
"""Initialize a new instance."""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -113,7 +113,7 @@ def __str__(self):
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class GetCommEventCounterRequest(ModbusRequest):
class GetCommEventCounterRequest(ModbusPDU):
"""This function code is used to get a status word.
And an event count from the remote device's communication event counter.
Expand All @@ -137,7 +137,7 @@ class GetCommEventCounterRequest(ModbusRequest):

def __init__(self, slave=1, transaction=0, skip_encode=False):
"""Initialize a new instance."""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -221,7 +221,7 @@ def __str__(self):
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class GetCommEventLogRequest(ModbusRequest):
class GetCommEventLogRequest(ModbusPDU):
"""This function code is used to get a status word.
Event count, message count, and a field of event bytes from the remote device.
Expand All @@ -248,7 +248,7 @@ class GetCommEventLogRequest(ModbusRequest):

def __init__(self, slave=1, transaction=0, skip_encode=False):
"""Initialize a new instance."""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -357,7 +357,7 @@ def __str__(self):
# ---------------------------------------------------------------------------#
# TODO Make these only work on serial # pylint: disable=fixme
# ---------------------------------------------------------------------------#
class ReportSlaveIdRequest(ModbusRequest):
class ReportSlaveIdRequest(ModbusPDU):
"""This function code is used to read the description of the type.
The current status, and other information specific to a remote device.
Expand All @@ -373,7 +373,7 @@ def __init__(self, slave=1, transaction=0, skip_encode=False):
:param slave: Modbus slave slave ID
"""
ModbusRequest.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)

def encode(self):
"""Encode the message."""
Expand Down
Loading

0 comments on commit 956997b

Please sign in to comment.