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

remove ModbusResponse. #2383

Merged
merged 4 commits into from
Oct 15, 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
12 changes: 6 additions & 6 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, ModbusPDU, ModbusResponse
from pymodbus.pdu import ModbusExceptions, ModbusPDU
from pymodbus.pdu.bit_read_message import ReadCoilsRequest


Expand All @@ -30,15 +30,15 @@
# --------------------------------------------------------------------------- #


class CustomModbusResponse(ModbusResponse):
class CustomModbusPDU(ModbusPDU):
"""Custom modbus response."""

function_code = 55
_rtu_byte_count_pos = 2

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

def encode(self):
Expand Down Expand Up @@ -89,7 +89,7 @@ def execute(self, context):
if not context.validate(self.function_code, self.address, self.count):
return self.doException(ModbusExceptions.IllegalAddress)
values = context.getValues(self.function_code, self.address, self.count)
return CustomModbusResponse(values)
return CustomModbusPDU(values)


# --------------------------------------------------------------------------- #
Expand Down Expand Up @@ -122,10 +122,10 @@ async def main(host="localhost", port=5020):
await client.connect()

# create a response object to control it works
CustomModbusResponse()
CustomModbusPDU()

# new modbus function code.
client.register(CustomModbusResponse)
client.register(CustomModbusPDU)
slave=1
request = CustomRequest(32, slave=slave)
result = await client.execute(request)
Expand Down
1 change: 0 additions & 1 deletion examples/server_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def server_response_manipulator(self, response):
self.message_count = 3
else:
print("---> RESPONSE: NONE")
response.should_respond = False
self.message_count -= 1
return response, False

Expand Down
14 changes: 7 additions & 7 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
from pymodbus.factory import ClientDecoder
from pymodbus.framer import FRAMER_NAME_TO_CLASS, FramerBase, FramerType
from pymodbus.logging import Log
from pymodbus.pdu import ModbusPDU, ModbusResponse
from pymodbus.pdu import ModbusPDU
from pymodbus.transaction import SyncModbusTransactionManager
from pymodbus.transport import CommParams
from pymodbus.utilities import ModbusTransactionState


class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]]):
class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusPDU]]):
"""**ModbusBaseClient**.

:mod:`ModbusBaseClient` is normally not referenced outside :mod:`pymodbus`.
Expand Down Expand Up @@ -67,7 +67,7 @@ async def connect(self) -> bool:
)
return await self.ctx.connect()

def register(self, custom_response_class: ModbusResponse) -> None:
def register(self, custom_response_class: ModbusPDU) -> None:
"""Register a custom response class with the decoder (call **sync**).

:param custom_response_class: (optional) Modbus response class.
Expand Down Expand Up @@ -95,7 +95,7 @@ def execute(self, request: ModbusPDU):
raise ConnectionException(f"Not connected[{self!s}]")
return self.async_execute(request)

async def async_execute(self, request) -> ModbusResponse:
async def async_execute(self, request) -> ModbusPDU:
"""Execute requests asynchronously.

:meta private:
Expand Down Expand Up @@ -160,7 +160,7 @@ def __str__(self):
)


class ModbusBaseSyncClient(ModbusClientMixin[ModbusResponse]):
class ModbusBaseSyncClient(ModbusClientMixin[ModbusPDU]):
"""**ModbusBaseClient**.

:mod:`ModbusBaseClient` is normally not referenced outside :mod:`pymodbus`.
Expand Down Expand Up @@ -198,7 +198,7 @@ def __init__(
# ----------------------------------------------------------------------- #
# Client external interface
# ----------------------------------------------------------------------- #
def register(self, custom_response_class: ModbusResponse) -> None:
def register(self, custom_response_class: ModbusPDU) -> None:
"""Register a custom response class with the decoder.

:param custom_response_class: (optional) Modbus response class.
Expand All @@ -219,7 +219,7 @@ def idle_time(self) -> float:
return 0
return self.last_frame_end + self.silent_interval

def execute(self, request: ModbusPDU) -> ModbusResponse:
def execute(self, request: ModbusPDU) -> ModbusPDU:
"""Execute request and get response (call **sync/async**).

:param request: The request to process
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ def _helper(self, data: str):

def register(self, function):
"""Register a function and sub function class with the decoder."""
if function and not issubclass(function, pdu.ModbusResponse):
if function and 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.ModbusResponse` "
"`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,13 +4,11 @@
"IllegalFunctionRequest",
"ModbusExceptions",
"ModbusPDU",
"ModbusResponse",
]

from pymodbus.pdu.pdu import (
ExceptionResponse,
IllegalFunctionRequest,
ModbusExceptions,
ModbusPDU,
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,7 +4,7 @@
import struct

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


Expand Down Expand Up @@ -59,7 +59,7 @@ def __str__(self):
return f"ReadBitRequest({self.address},{self.count})"


class ReadBitsResponseBase(ModbusResponse):
class ReadBitsResponseBase(ModbusPDU):
"""Base class for Messages responding to bit-reading values.

The requested bits can be found in the .bits list.
Expand All @@ -73,7 +73,7 @@ def __init__(self, values, slave, transaction, skip_encode):
:param values: The requested values to be returned
:param slave: Modbus slave slave ID
"""
ModbusResponse.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)

#: A list of booleans representing bit values
self.bits = values or []
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 ModbusPDU, ModbusResponse
from pymodbus.pdu import ModbusPDU
from pymodbus.utilities import pack_bitstring, unpack_bitstring


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


class WriteSingleCoilResponse(ModbusResponse):
class WriteSingleCoilResponse(ModbusPDU):
"""The normal response is an echo of the request.

Returned after the coil state has been written.
Expand All @@ -119,7 +119,7 @@ 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
"""
ModbusResponse.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
self.value = value

Expand Down Expand Up @@ -241,7 +241,7 @@ def get_response_pdu_size(self):
return 1 + 2 + 2


class WriteMultipleCoilsResponse(ModbusResponse):
class WriteMultipleCoilsResponse(ModbusPDU):
"""The normal response returns the function code.

Starting address, and quantity of coils forced.
Expand All @@ -256,7 +256,7 @@ 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
"""
ModbusResponse.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.address = address
self.count = count

Expand Down
7 changes: 3 additions & 4 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 ModbusPDU, ModbusResponse
from pymodbus.pdu import ModbusPDU
from pymodbus.utilities import pack_bitstring


Expand Down Expand Up @@ -80,7 +80,7 @@ def get_response_pdu_size(self):
return 1 + 2 + 2 * len(self.message)


class DiagnosticStatusResponse(ModbusResponse):
class DiagnosticStatusResponse(ModbusPDU):
"""Diagnostic status.

This is a base class for all of the diagnostic response functions
Expand All @@ -95,7 +95,7 @@ class DiagnosticStatusResponse(ModbusResponse):

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

def encode(self):
Expand Down Expand Up @@ -394,7 +394,6 @@ class ForceListenOnlyModeResponse(DiagnosticStatusResponse):
"""

sub_function_code = 0x0004
should_respond = False

def __init__(self, slave=1, transaction=0, skip_encode=False):
"""Initialize to block a return response."""
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 ModbusPDU, ModbusResponse
from pymodbus.pdu import ModbusPDU


# ---------------------------------------------------------------------------#
Expand Down Expand Up @@ -142,7 +142,7 @@ def execute(self, _context):
return ReadFileRecordResponse(files)


class ReadFileRecordResponse(ModbusResponse):
class ReadFileRecordResponse(ModbusPDU):
"""Read file record response.

The normal response is a series of "sub-responses," one for each
Expand All @@ -159,7 +159,7 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):

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

def encode(self):
Expand Down Expand Up @@ -268,7 +268,7 @@ def execute(self, _context):
return WriteFileRecordResponse(self.records)


class WriteFileRecordResponse(ModbusResponse):
class WriteFileRecordResponse(ModbusPDU):
"""The normal response is an echo of the request."""

function_code = 0x15
Expand All @@ -279,7 +279,7 @@ def __init__(self, records=None, slave=1, transaction=0, skip_encode=False):

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

def encode(self):
Expand Down Expand Up @@ -375,7 +375,7 @@ def execute(self, _context):
return ReadFifoQueueResponse(self.values)


class ReadFifoQueueResponse(ModbusResponse):
class ReadFifoQueueResponse(ModbusPDU):
"""Read Fifo queue response.

In a normal response, the byte count shows the quantity of bytes to
Expand Down Expand Up @@ -405,7 +405,7 @@ def __init__(self, values=None, slave=1, transaction=0, skip_encode=False):

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

def encode(self):
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 ModbusPDU, ModbusResponse
from pymodbus.pdu import ModbusPDU


_MCB = ModbusControlBlock()
Expand Down Expand Up @@ -105,7 +105,7 @@ def __str__(self):
)


class ReadDeviceInformationResponse(ModbusResponse):
class ReadDeviceInformationResponse(ModbusPDU):
"""Read device information response."""

function_code = 0x2B
Expand Down Expand Up @@ -136,7 +136,7 @@ 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
"""
ModbusResponse.__init__(self, slave, transaction, skip_encode)
ModbusPDU.__init__(self, slave, transaction, skip_encode)
self.read_code = read_code or DeviceInformation.BASIC
self.information = information or {}
self.number_of_objects = 0
Expand Down
Loading