Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean PDU init. #2399

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/client_custom_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class CustomModbusPDU(ModbusPDU):

def __init__(self, values=None, slave=1, transaction=0, skip_encode=False):
"""Initialize."""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.values = values or []

def encode(self):
Expand Down Expand Up @@ -70,7 +71,8 @@ class CustomRequest(ModbusPDU):

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

Expand Down
6 changes: 4 additions & 2 deletions pymodbus/pdu/bit_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ 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
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
self.count = count

Expand Down Expand Up @@ -70,7 +71,8 @@ def __init__(self, values, slave, transaction, skip_encode):
:param values: The requested values to be returned
:param slave: Modbus slave slave ID
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)

#: A list of booleans representing bit values
self.bits = values or []
Expand Down
12 changes: 8 additions & 4 deletions pymodbus/pdu/bit_write_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ 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
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
self.value = bool(value)

Expand Down Expand Up @@ -116,7 +117,8 @@ def __init__(self, address=None, value=None, slave=1, transaction=0, skip_encode
:param address: The variable address written to
:param value: The value written at address
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
self.value = value

Expand Down Expand Up @@ -170,7 +172,8 @@ def __init__(self, address=0, values=None, slave=None, transaction=0, skip_encod
:param address: The starting request address
:param values: The values to write
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
if values is None: # pragma: no cover
values = []
Expand Down Expand Up @@ -246,7 +249,8 @@ def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode
:param address: The starting variable address written to
:param count: The number of values written
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
self.count = count

Expand Down
3 changes: 2 additions & 1 deletion pymodbus/pdu/decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def decode(self, frame: bytes) -> base.ModbusPDU | None:
if not (pdu_type := self.lookup.get(function_code, None)):
Log.debug("decode PDU failed for function code {}", function_code)
raise ModbusException(f"Unknown response {function_code}")
pdu = pdu_type(0, 0, False)
pdu = pdu_type()
pdu.setData(0, 0, False)
Log.debug("decode PDU for {}", function_code)
pdu.decode(frame[1:])

Expand Down
6 changes: 4 additions & 2 deletions pymodbus/pdu/diag_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class DiagnosticStatusRequest(ModbusPDU):

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

def encode(self):
Expand Down Expand Up @@ -95,7 +96,8 @@ class DiagnosticStatusResponse(ModbusPDU):

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

def encode(self):
Expand Down
18 changes: 12 additions & 6 deletions pymodbus/pdu/file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):

:param records: The file record requests to be read
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.records = records or []

def encode(self):
Expand Down Expand Up @@ -159,7 +160,8 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):

:param records: The requested file records
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.records = records or []

def encode(self):
Expand Down Expand Up @@ -215,7 +217,8 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):

:param records: The file record requests to be read
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.records = records or []

def encode(self):
Expand Down Expand Up @@ -279,7 +282,8 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):

:param records: The file record requests to be read
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.records = records or []

def encode(self):
Expand Down Expand Up @@ -344,7 +348,8 @@ def __init__(self, address=0x0000, slave=1, transaction=0, skip_encode=False):

:param address: The fifo pointer address (0x0000 to 0xffff)
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
self.values = [] # this should be added to the context

Expand Down Expand Up @@ -405,7 +410,8 @@ def __init__(self, values=None, slave=1, transaction=0, skip_encode=False):

:param values: The list of values of the fifo to return
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.values = values or []

def encode(self):
Expand Down
6 changes: 4 additions & 2 deletions pymodbus/pdu/mei_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ 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
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.read_code = read_code or DeviceInformation.BASIC
self.object_id = object_id

Expand Down Expand Up @@ -136,7 +137,8 @@ def __init__(self, read_code=None, information=None, slave=1, transaction=0, ski
:param read_code: The device information read code
:param information: The requested information request
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.read_code = read_code or DeviceInformation.BASIC
self.information = information or {}
self.number_of_objects = 0
Expand Down
24 changes: 16 additions & 8 deletions pymodbus/pdu/other_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class ReadExceptionStatusRequest(ModbusPDU):

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

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -74,7 +75,8 @@ def __init__(self, status=0x00, slave=1, transaction=0, skip_encode=False):

:param status: The status response to report
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.status = status if status < 256 else 255

def encode(self):
Expand Down Expand Up @@ -131,7 +133,8 @@ class GetCommEventCounterRequest(ModbusPDU):

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

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -174,7 +177,8 @@ def __init__(self, count=0x0000, slave=1, transaction=0, skip_encode=False):

:param count: The current event counter value
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.count = count
self.status = True # this means we are ready, not waiting

Expand Down Expand Up @@ -236,7 +240,8 @@ class GetCommEventLogRequest(ModbusPDU):

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

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -289,7 +294,8 @@ def __init__(self, status=True, message_count=0, event_count=0, events=None, sla
:param event_count: The current event count
:param events: The collection of events to send
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.status = status
self.message_count = message_count
self.event_count = event_count
Expand Down Expand Up @@ -361,7 +367,8 @@ def __init__(self, slave=1, transaction=0, skip_encode=False):
:param slave: Modbus slave slave ID

"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)

def encode(self):
"""Encode the message."""
Expand Down Expand Up @@ -420,7 +427,8 @@ def __init__(self, identifier=b"\x00", status=True, slave=1, transaction=0, skip
:param identifier: The identifier of the slave
:param status: The status response to report
"""
ModbusPDU.__init__(self, slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.identifier = identifier
self.status = status
self.byte_count = None
Expand Down
34 changes: 20 additions & 14 deletions pymodbus/pdu/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ class ModbusPDU:
_rtu_frame_size: int = 0
_rtu_byte_count_pos: int = 0

def __init__(self, slave: int, transaction: int, skip_encode: bool) -> None:
def __init__(self) -> None:
"""Initialize the base data for a modbus request."""
self.transaction_id: int
self.slave_id: int
self.skip_encode: bool
self.bits: list[bool]
self.registers: list[int]
self.fut: asyncio.Future

def setData(self, slave: int, transaction: int, skip_encode: bool) -> None:
"""Set data common for all PDU."""
self.transaction_id = transaction
self.slave_id = slave
self.skip_encode = skip_encode
self.bits: list[bool] = []
self.registers: list[int] = []
self.fut: asyncio.Future | None = None

@abstractmethod
def encode(self) -> bytes:
"""Encode the message."""

@abstractmethod
def decode(self, data: bytes) -> None:
"""Decode data part of the message."""

def doException(self, exception: int) -> ExceptionResponse:
"""Build an error response based on the function."""
Expand All @@ -48,6 +46,14 @@ def get_response_pdu_size(self) -> int:
"""Calculate response pdu size."""
return 0

@abstractmethod
def encode(self) -> bytes:
"""Encode the message."""

@abstractmethod
def decode(self, data: bytes) -> None:
"""Decode data part of the message."""


@classmethod
def calculateRtuFrameSize(cls, data: bytes) -> int:
Expand All @@ -63,7 +69,6 @@ def calculateRtuFrameSize(cls, data: bytes) -> int:
)



class ModbusExceptions: # pylint: disable=too-few-public-methods
"""An enumeration of the valid modbus exceptions."""

Expand Down Expand Up @@ -102,7 +107,8 @@ def __init__(
transaction: int = 0,
skip_encode: bool = False) -> None:
"""Initialize the modbus exception response."""
super().__init__(slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.function_code = function_code | 0x80
self.exception_code = exception_code

Expand Down
9 changes: 6 additions & 3 deletions pymodbus/pdu/register_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def __init__(self, address, count, slave=1, transaction=0, skip_encode=False):
:param count: The number of registers to read
:param slave: Modbus slave slave ID
"""
super().__init__(slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.address = address
self.count = count

Expand Down Expand Up @@ -68,7 +69,8 @@ def __init__(self, values, slave=1, transaction=0, skip_encode=False):
:param values: The values to write to
:param slave: Modbus slave slave ID
"""
super().__init__(slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)

#: A list of register values
self.registers = values or []
Expand Down Expand Up @@ -263,7 +265,8 @@ def __init__(self, read_address=0x00, read_count=0, write_address=0x00, write_re
:param write_address: The address to start writing to
:param write_registers: The registers to write to the specified address
"""
super().__init__(slave, transaction, skip_encode)
super().__init__()
super().setData(slave, transaction, skip_encode)
self.read_address = read_address
self.read_count = read_count
self.write_address = write_address
Expand Down
Loading