From debd1effc3bfbbc0e144ea1343cb8805f1cdb20a Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sat, 26 Oct 2024 20:42:43 +0200 Subject: [PATCH] skip_encode. --- doc/source/roadmap.rst | 7 ++--- examples/client_custom_msg.py | 12 +++---- examples/client_payload.py | 3 -- pymodbus/client/mixin.py | 6 ++-- pymodbus/pdu/bit_message.py | 36 ++++++++++----------- pymodbus/pdu/decoders.py | 1 - pymodbus/pdu/diag_message.py | 40 ++++++++++++------------ pymodbus/pdu/file_message.py | 24 +++++++------- pymodbus/pdu/mei_message.py | 8 ++--- pymodbus/pdu/other_message.py | 32 +++++++++---------- pymodbus/pdu/pdu.py | 17 +++++----- pymodbus/pdu/register_read_message.py | 28 ++++++++--------- pymodbus/pdu/register_write_message.py | 33 ++++++++----------- test/framer/test_framer.py | 1 - test/pdu/test_bit_pdu.py | 30 +++++++++--------- test/pdu/test_pdu.py | 5 ++- test/pdu/test_register_write_messages.py | 6 ---- test/sub_client/test_client.py | 15 ++++----- test/sub_current/test_transaction.py | 4 +-- 19 files changed, 140 insertions(+), 168 deletions(-) diff --git a/doc/source/roadmap.rst b/doc/source/roadmap.rst index 0d7fa3276b..1c68232c92 100644 --- a/doc/source/roadmap.rst +++ b/doc/source/roadmap.rst @@ -15,13 +15,12 @@ It is the community that decides how pymodbus evolves NOT the maintainers ! The following bullet points are what the maintainers focus on: -- 3.7.5, bug fix release, hopefully with: +- 3.7.X, bug fix release, hopefully with: + - Not planned +- 3.8.0, with: - Simplify PDU classes - Simplify transaction manager (central control point) - Remove ModbusControlBlock -- 3.7.6, bug fix release, with: - - Not planned -- 3.8.0, with: - new transaction handling - transaction 100% coverage - skip_encode, zero_mode parameters removed diff --git a/examples/client_custom_msg.py b/examples/client_custom_msg.py index 9c241ef3c0..8b2f817427 100755 --- a/examples/client_custom_msg.py +++ b/examples/client_custom_msg.py @@ -36,10 +36,10 @@ class CustomModbusPDU(ModbusPDU): function_code = 55 _rtu_byte_count_pos = 2 - def __init__(self, values=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, values=None, slave=1, transaction=0): """Initialize.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.values = values or [] def encode(self): @@ -69,10 +69,10 @@ class CustomRequest(ModbusPDU): function_code = 55 _rtu_frame_size = 8 - def __init__(self, address=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=None, slave=1, transaction=0): """Initialize.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.count = 16 @@ -102,13 +102,13 @@ def execute(self, context): class Read16CoilsRequest(ReadCoilsRequest): """Read 16 coils in one request.""" - def __init__(self, address, slave=1, transaction=0, skip_encode=False): + def __init__(self, address, slave=1, transaction=0): """Initialize a new instance. :param address: The address to start reading from """ super().__init__() - self.setData(address, 16, slave, transaction, skip_encode) + self.setData(address, 16, slave, transaction) # --------------------------------------------------------------------------- # diff --git a/examples/client_payload.py b/examples/client_payload.py index 70eb53ac62..ec98b7628d 100755 --- a/examples/client_payload.py +++ b/examples/client_payload.py @@ -90,9 +90,6 @@ async def run_payload_calls(client): # We can write registers rr = await client.write_registers(address, registers, slave=slave) assert not rr.isError() - # Or we can write an encoded binary string - rr = await client.write_registers(address, payload, skip_encode=True, slave=1) - assert not rr.isError() # ----------------------------------------------------------------------- # # If you need to decode a collection of registers in a weird layout, the diff --git a/pymodbus/client/mixin.py b/pymodbus/client/mixin.py index 1e9f90fa3f..37854f2176 100644 --- a/pymodbus/client/mixin.py +++ b/pymodbus/client/mixin.py @@ -75,7 +75,7 @@ def read_coils(self, address: int, count: int = 1, slave: int = 1, no_response_e Coils are addressed starting at zero. Therefore devices that numbers coils 1-16 are addressed as 0-15. """ pdu = pdu_bit.ReadCoilsRequest() - pdu.setData(address, count, slave, 0, False) + pdu.setData(address, count, slave, 0) return self.execute(no_response_expected, pdu) def read_discrete_inputs(self, @@ -349,7 +349,6 @@ def write_registers( address: int, values: Sequence[bytes | int], slave: int = 1, - skip_encode: bool = False, no_response_expected: bool = False ) -> T: """Write registers (code 0x10). @@ -357,11 +356,10 @@ def write_registers( :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 :param no_response_expected: (optional) The client will not expect a response to the request :raises ModbusException: """ - return self.execute(no_response_expected, pdu_req_write.WriteMultipleRegistersRequest(address, values,slave=slave,skip_encode=skip_encode)) + return self.execute(no_response_expected, pdu_req_write.WriteMultipleRegistersRequest(address, values,slave=slave)) def report_slave_id(self, slave: int = 1, no_response_expected: bool = False) -> T: """Report slave ID (code 0x11). diff --git a/pymodbus/pdu/bit_message.py b/pymodbus/pdu/bit_message.py index 3a1fe214e8..6f5d829418 100644 --- a/pymodbus/pdu/bit_message.py +++ b/pymodbus/pdu/bit_message.py @@ -25,9 +25,9 @@ def __init__(self) -> None: self.address: int self.count: int - def setData(self, address: int, count: int, slave_id: int, transaction_id: int, skip_encode: bool) -> None: + def setData(self, address: int, count: int, slave_id: int, transaction_id: int) -> None: """Set data.""" - super().setBaseData(slave_id, transaction_id, skip_encode) + super().setBaseData(slave_id, transaction_id) self.address = address self.count = count @@ -77,7 +77,7 @@ class ReadDiscreteInputsRequest(ReadCoilsRequest): function_code = 2 - def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=None, count=None, slave=1, transaction=0): """Initialize a new instance. :param address: The address to start reading from @@ -85,7 +85,7 @@ def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode :param slave: Modbus slave slave ID """ super().__init__() - self.setData(address, count, slave, transaction, skip_encode) + self.setData(address, count, slave, transaction) async def update_datastore(self, context): """Run a read discrete input request against a datastore. @@ -119,14 +119,14 @@ class ReadBitsResponseBase(ModbusPDU): _rtu_byte_count_pos = 2 - def __init__(self, values, slave, transaction, skip_encode): + def __init__(self, values, slave, transaction): """Initialize a new instance. :param values: The requested values to be returned :param slave: Modbus slave slave ID """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) #: A list of booleans representing bit values self.bits = values or [] @@ -172,13 +172,13 @@ class ReadCoilsResponse(ReadBitsResponseBase): function_code = 1 - def __init__(self, values=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, values=None, slave=1, transaction=0): """Initialize a new instance. :param values: The request values to respond with :param slave: Modbus slave slave ID """ - ReadBitsResponseBase.__init__(self, values, slave, transaction, skip_encode) + super().__init__(values, slave, transaction) class ReadDiscreteInputsResponse(ReadBitsResponseBase): @@ -199,13 +199,13 @@ class ReadDiscreteInputsResponse(ReadBitsResponseBase): function_code = 2 - def __init__(self, values=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, values=None, slave=1, transaction=0): """Initialize a new instance. :param values: The request values to respond with :param slave: Modbus slave slave ID """ - ReadBitsResponseBase.__init__(self, values, slave, transaction, skip_encode) + super().__init__(values, slave, transaction) class WriteSingleCoilRequest(ModbusPDU): @@ -228,14 +228,14 @@ class WriteSingleCoilRequest(ModbusPDU): _rtu_frame_size = 8 - def __init__(self, address=None, value=None, slave=None, transaction=0, skip_encode=0): + def __init__(self, address=None, value=None, slave=None, transaction=0): """Initialize a new instance. :param address: The variable address to write :param value: The value to write at address """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.value = bool(value) @@ -296,14 +296,14 @@ class WriteSingleCoilResponse(ModbusPDU): function_code = 5 _rtu_frame_size = 8 - def __init__(self, address=None, value=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=None, value=None, slave=1, transaction=0): """Initialize a new instance. :param address: The variable address written to :param value: The value written at address """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.value = value @@ -350,14 +350,14 @@ class WriteMultipleCoilsRequest(ModbusPDU): function_code = 15 _rtu_byte_count_pos = 6 - def __init__(self, address=0, values=None, slave=None, transaction=0, skip_encode=0): + def __init__(self, address=0, values=None, slave=None, transaction=0): """Initialize a new instance. :param address: The starting request address :param values: The values to write """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address if values is None: values = [] @@ -427,14 +427,14 @@ class WriteMultipleCoilsResponse(ModbusPDU): function_code = 15 _rtu_frame_size = 8 - def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=None, count=None, slave=1, transaction=0): """Initialize a new instance. :param address: The starting variable address written to :param count: The number of values written """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.count = count diff --git a/pymodbus/pdu/decoders.py b/pymodbus/pdu/decoders.py index 34fa2c767a..18c588c516 100644 --- a/pymodbus/pdu/decoders.py +++ b/pymodbus/pdu/decoders.py @@ -110,7 +110,6 @@ def decode(self, frame: bytes) -> base.ModbusPDU | None: Log.debug("decode PDU failed for function code {}", function_code) raise ModbusException(f"Unknown response {function_code}") pdu = pdu_type() - pdu.setBaseData(0, 0, False) pdu.decode(frame[1:]) Log.debug("decoded PDU function_code({} sub {}) -> {} ", pdu.function_code, pdu.sub_function_code, str(pdu)) diff --git a/pymodbus/pdu/diag_message.py b/pymodbus/pdu/diag_message.py index d37537ce09..e32177b463 100644 --- a/pymodbus/pdu/diag_message.py +++ b/pymodbus/pdu/diag_message.py @@ -31,10 +31,10 @@ class DiagnosticStatusRequest(ModbusPDU): sub_function_code = 9999 _rtu_frame_size = 8 - def __init__(self, slave=1, transaction=0, skip_encode=False): + def __init__(self, slave=1, transaction=0): """Initialize a diagnostic request.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.message = None def encode(self): @@ -97,10 +97,10 @@ class DiagnosticStatusResponse(ModbusPDU): sub_function_code = 9999 _rtu_frame_size = 8 - def __init__(self, slave=1, transaction=0, skip_encode=False): + def __init__(self, slave=1, transaction=0): """Initialize a diagnostic response.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.message = None def encode(self): @@ -153,7 +153,7 @@ class DiagnosticStatusSimpleRequest(DiagnosticStatusRequest): the update_datastore method """ - def __init__(self, data=0x0000, slave=1, transaction=0, skip_encode=False): + def __init__(self, data=0x0000, slave=1, transaction=0): """Initialize a simple diagnostic request. The data defaults to 0x0000 if not provided as over half @@ -161,7 +161,7 @@ def __init__(self, data=0x0000, slave=1, transaction=0, skip_encode=False): :param data: The data to send along with the request """ - DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction) self.message = data @@ -174,12 +174,12 @@ class DiagnosticStatusSimpleResponse(DiagnosticStatusResponse): 2 bytes of data. """ - def __init__(self, data=0x0000, slave=1, transaction=0, skip_encode=False): + def __init__(self, data=0x0000, slave=1, transaction=0): """Return a simple diagnostic response. :param data: The resulting data to return to the client """ - DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction) self.message = data @@ -196,12 +196,12 @@ class ReturnQueryDataRequest(DiagnosticStatusRequest): sub_function_code = 0x0000 - def __init__(self, message=b"\x00\x00", slave=1, transaction=0, skip_encode=False): + def __init__(self, message=b"\x00\x00", slave=1, transaction=0): """Initialize a new instance of the request. :param message: The message to send to loopback """ - DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction) if not isinstance(message, bytes): raise ModbusException(f"message({type(message)}) must be bytes") self.message = message @@ -224,12 +224,12 @@ class ReturnQueryDataResponse(DiagnosticStatusResponse): sub_function_code = 0x0000 - def __init__(self, message=b"\x00\x00", slave=1, transaction=0, skip_encode=False): + def __init__(self, message=b"\x00\x00", slave=1, transaction=0): """Initialize a new instance of the response. :param message: The message to loopback """ - DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction) if not isinstance(message, bytes): raise ModbusException(f"message({type(message)}) must be bytes") self.message = message @@ -251,12 +251,12 @@ class RestartCommunicationsOptionRequest(DiagnosticStatusRequest): sub_function_code = 0x0001 - def __init__(self, toggle=False, slave=1, transaction=0, skip_encode=False): + def __init__(self, toggle=False, slave=1, transaction=0): """Initialize a new request. :param toggle: Set to True to toggle, False otherwise """ - DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction) if toggle: self.message = [ModbusStatus.ON] else: @@ -284,12 +284,12 @@ class RestartCommunicationsOptionResponse(DiagnosticStatusResponse): sub_function_code = 0x0001 - def __init__(self, toggle=False, slave=1, transaction=0, skip_encode=False): + def __init__(self, toggle=False, slave=1, transaction=0): """Initialize a new response. :param toggle: Set to True if we toggled, False otherwise """ - DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction) if toggle: self.message = [ModbusStatus.ON] else: @@ -394,9 +394,9 @@ class ForceListenOnlyModeResponse(DiagnosticStatusResponse): sub_function_code = 0x0004 - def __init__(self, slave=1, transaction=0, skip_encode=False): + def __init__(self, slave=1, transaction=0): """Initialize to block a return response.""" - DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) + DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction) self.message = [] @@ -776,9 +776,9 @@ class GetClearModbusPlusRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0015 - def __init__(self, data=0, slave=1, transaction=0, skip_encode=False): + def __init__(self, data=0, slave=1, transaction=0): """Initialize.""" - super().__init__(slave=slave, transaction=transaction, skip_encode=skip_encode) + super().__init__(slave=slave, transaction=transaction) self.message=data def get_response_pdu_size(self): diff --git a/pymodbus/pdu/file_message.py b/pymodbus/pdu/file_message.py index 30676a8b66..b6db0c7513 100644 --- a/pymodbus/pdu/file_message.py +++ b/pymodbus/pdu/file_message.py @@ -90,13 +90,13 @@ class ReadFileRecordRequest(ModbusPDU): function_code = 0x14 _rtu_byte_count_pos = 2 - def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0, skip_encode=False): # pylint: disable=dangerous-default-value + def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0): # pylint: disable=dangerous-default-value """Initialize a new instance. :param records: The file record requests to be read """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.records = records def encode(self): @@ -158,13 +158,13 @@ class ReadFileRecordResponse(ModbusPDU): function_code = 0x14 _rtu_byte_count_pos = 2 - def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0, skip_encode=False): # pylint: disable=dangerous-default-value + def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0): # pylint: disable=dangerous-default-value """Initialize a new instance. :param records: The requested file records """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.records = records def encode(self): @@ -212,13 +212,13 @@ class WriteFileRecordRequest(ModbusPDU): function_code = 0x15 _rtu_byte_count_pos = 2 - def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0, skip_encode=False): # pylint: disable=dangerous-default-value + def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0): # pylint: disable=dangerous-default-value """Initialize a new instance. :param records: The file record requests to be read """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.records = records def encode(self): @@ -277,13 +277,13 @@ class WriteFileRecordResponse(ModbusPDU): function_code = 0x15 _rtu_byte_count_pos = 2 - def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0, skip_encode=False): # pylint: disable=dangerous-default-value + def __init__(self, records: list[FileRecord] = [], slave=1, transaction=0): # pylint: disable=dangerous-default-value """Initialize a new instance. :param records: The file record requests to be read """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.records = records def encode(self): @@ -341,13 +341,13 @@ class ReadFifoQueueRequest(ModbusPDU): function_code = 0x18 _rtu_frame_size = 6 - def __init__(self, address=0x0000, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=0x0000, slave=1, transaction=0): """Initialize a new instance. :param address: The fifo pointer address (0x0000 to 0xffff) """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.values = [] # this should be added to the context @@ -407,13 +407,13 @@ def calculateRtuFrameSize(cls, buffer): lo_byte = int(buffer[3]) return (hi_byte << 16) + lo_byte + 6 - def __init__(self, values=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, values=None, slave=1, transaction=0): """Initialize a new instance. :param values: The list of values of the fifo to return """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.values = values or [] def encode(self): diff --git a/pymodbus/pdu/mei_message.py b/pymodbus/pdu/mei_message.py index 719bc1286c..c83b2ab672 100644 --- a/pymodbus/pdu/mei_message.py +++ b/pymodbus/pdu/mei_message.py @@ -51,14 +51,14 @@ class ReadDeviceInformationRequest(ModbusPDU): sub_function_code = 0x0E _rtu_frame_size = 7 - def __init__(self, read_code=None, object_id=0x00, slave=1, transaction=0, skip_encode=False): + def __init__(self, read_code=None, object_id=0x00, slave=1, transaction=0): """Initialize a new instance. :param read_code: The device information read code :param object_id: The object to read from """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.read_code = read_code or DeviceInformation.BASIC self.object_id = object_id @@ -130,14 +130,14 @@ def calculateRtuFrameSize(cls, buffer): except struct.error as exc: raise IndexError from exc - def __init__(self, read_code=None, information=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, read_code=None, information=None, slave=1, transaction=0): """Initialize a new instance. :param read_code: The device information read code :param information: The requested information request """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.read_code = read_code or DeviceInformation.BASIC self.information = information or {} self.number_of_objects = 0 diff --git a/pymodbus/pdu/other_message.py b/pymodbus/pdu/other_message.py index 283656e186..7086d6b5eb 100644 --- a/pymodbus/pdu/other_message.py +++ b/pymodbus/pdu/other_message.py @@ -29,10 +29,10 @@ class ReadExceptionStatusRequest(ModbusPDU): function_code = 0x07 _rtu_frame_size = 4 - def __init__(self, slave=None, transaction=0, skip_encode=0): + def __init__(self, slave=None, transaction=0): """Initialize a new instance.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) def encode(self): """Encode the message.""" @@ -69,13 +69,13 @@ class ReadExceptionStatusResponse(ModbusPDU): function_code = 0x07 _rtu_frame_size = 5 - def __init__(self, status=0x00, slave=1, transaction=0, skip_encode=False): + def __init__(self, status=0x00, slave=1, transaction=0): """Initialize a new instance. :param status: The status response to report """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.status = status if status < 256 else 255 def encode(self): @@ -129,10 +129,10 @@ class GetCommEventCounterRequest(ModbusPDU): function_code = 0x0B _rtu_frame_size = 4 - def __init__(self, slave=1, transaction=0, skip_encode=False): + def __init__(self, slave=1, transaction=0): """Initialize a new instance.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) def encode(self): """Encode the message.""" @@ -170,13 +170,13 @@ class GetCommEventCounterResponse(ModbusPDU): function_code = 0x0B _rtu_frame_size = 8 - def __init__(self, count=0x0000, slave=1, transaction=0, skip_encode=False): + def __init__(self, count=0x0000, slave=1, transaction=0): """Initialize a new instance. :param count: The current event counter value """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.count = count self.status = True # this means we are ready, not waiting @@ -235,10 +235,10 @@ class GetCommEventLogRequest(ModbusPDU): function_code = 0x0C _rtu_frame_size = 4 - def __init__(self, slave=1, transaction=0, skip_encode=False): + def __init__(self, slave=1, transaction=0): """Initialize a new instance.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) def encode(self): """Encode the message.""" @@ -283,7 +283,7 @@ class GetCommEventLogResponse(ModbusPDU): 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, skip_encode=False): + def __init__(self, status=True, message_count=0, event_count=0, events=None, slave=1, transaction=0): """Initialize a new instance. :param status: The status response to report @@ -292,7 +292,7 @@ def __init__(self, status=True, message_count=0, event_count=0, events=None, sla :param events: The collection of events to send """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.status = status self.message_count = message_count self.event_count = event_count @@ -357,14 +357,14 @@ class ReportSlaveIdRequest(ModbusPDU): function_code = 0x11 _rtu_frame_size = 4 - def __init__(self, slave=1, transaction=0, skip_encode=False): + def __init__(self, slave=1, transaction=0): """Initialize a new instance. :param slave: Modbus slave slave ID """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) def encode(self): """Encode the message.""" @@ -417,14 +417,14 @@ class ReportSlaveIdResponse(ModbusPDU): function_code = 0x11 _rtu_byte_count_pos = 2 - def __init__(self, identifier=b"\x00", status=True, slave=1, transaction=0, skip_encode=False): + def __init__(self, identifier=b"\x00", status=True, slave=1, transaction=0): """Initialize a new instance. :param identifier: The identifier of the slave :param status: The status response to report """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.identifier = identifier self.status = status self.byte_count = None diff --git a/pymodbus/pdu/pdu.py b/pymodbus/pdu/pdu.py index 72e2015652..f3d35b487f 100644 --- a/pymodbus/pdu/pdu.py +++ b/pymodbus/pdu/pdu.py @@ -19,18 +19,16 @@ class ModbusPDU: 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.transaction_id: int = 0 + self.slave_id: int = 0 + self.bits: list[bool] = [] + self.registers: list[int] = [] self.fut: asyncio.Future - def setBaseData(self, slave_id: int, transaction_id: int, skip_encode: bool) -> None: + def setBaseData(self, slave_id: int, transaction_id: int) -> None: """Set data common for all PDU.""" self.transaction_id = transaction_id self.slave_id = slave_id - self.skip_encode = skip_encode def doException(self, exception: int) -> ExceptionResponse: """Build an error response based on the function.""" @@ -104,11 +102,10 @@ def __init__( function_code: int, exception_code: int = 0, slave: int = 1, - transaction: int = 0, - skip_encode: bool = False) -> None: + transaction: int = 0) -> None: """Initialize the modbus exception response.""" super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.function_code = function_code | 0x80 self.exception_code = exception_code diff --git a/pymodbus/pdu/register_read_message.py b/pymodbus/pdu/register_read_message.py index 887be09793..bf87f40fd6 100644 --- a/pymodbus/pdu/register_read_message.py +++ b/pymodbus/pdu/register_read_message.py @@ -14,7 +14,7 @@ class ReadRegistersRequestBase(ModbusPDU): _rtu_frame_size = 8 - def __init__(self, address, count, slave=1, transaction=0, skip_encode=False): + def __init__(self, address, count, slave=1, transaction=0): """Initialize a new instance. :param address: The address to start the read from @@ -22,7 +22,7 @@ def __init__(self, address, count, slave=1, transaction=0, skip_encode=False): :param slave: Modbus slave slave ID """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.count = count @@ -63,14 +63,14 @@ class ReadRegistersResponseBase(ModbusPDU): _rtu_byte_count_pos = 2 - def __init__(self, values, slave=1, transaction=0, skip_encode=False): + def __init__(self, values, slave=1, transaction=0): """Initialize a new instance. :param values: The values to write to :param slave: Modbus slave slave ID """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) #: A list of register values self.registers = values or [] @@ -125,14 +125,14 @@ class ReadHoldingRegistersRequest(ReadRegistersRequestBase): function_code = 3 - def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode=0): + def __init__(self, address=None, count=None, slave=1, transaction=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, skip_encode) + super().__init__(address, count, slave, transaction) async def update_datastore(self, context): """Run a read holding request against a datastore. @@ -166,12 +166,12 @@ class ReadHoldingRegistersResponse(ReadRegistersResponseBase): function_code = 3 - def __init__(self, values=None, slave=None, transaction=0, skip_encode=0): + def __init__(self, values=None, slave=None, transaction=0): """Initialize a new response instance. :param values: The resulting register values """ - super().__init__(values, slave, transaction, skip_encode) + super().__init__(values, slave, transaction) class ReadInputRegistersRequest(ReadRegistersRequestBase): @@ -186,14 +186,14 @@ class ReadInputRegistersRequest(ReadRegistersRequestBase): function_code = 4 - def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode=0): + def __init__(self, address=None, count=None, slave=1, transaction=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, skip_encode) + super().__init__(address, count, slave, transaction) async def update_datastore(self, context): """Run a read input request against a datastore. @@ -227,12 +227,12 @@ class ReadInputRegistersResponse(ReadRegistersResponseBase): function_code = 4 - def __init__(self, values=None, slave=None, transaction=0, skip_encode=0): + def __init__(self, values=None, slave=None, transaction=0): """Initialize a new response instance. :param values: The resulting register values """ - super().__init__(values, slave, transaction, skip_encode) + super().__init__(values, slave, transaction) class ReadWriteMultipleRegistersRequest(ModbusPDU): @@ -254,7 +254,7 @@ class ReadWriteMultipleRegistersRequest(ModbusPDU): function_code = 23 _rtu_byte_count_pos = 10 - def __init__(self, read_address=0x00, read_count=0, write_address=0x00, write_registers=None, slave=1, transaction=0, skip_encode=False): + def __init__(self, read_address=0x00, read_count=0, write_address=0x00, write_registers=None, slave=1, transaction=0): """Initialize a new request message. :param read_address: The address to start reading from @@ -263,7 +263,7 @@ def __init__(self, read_address=0x00, read_count=0, write_address=0x00, write_re :param write_registers: The registers to write to the specified address """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.read_address = read_address self.read_count = read_count self.write_address = write_address diff --git a/pymodbus/pdu/register_write_message.py b/pymodbus/pdu/register_write_message.py index 69d65709fc..f81423bb24 100644 --- a/pymodbus/pdu/register_write_message.py +++ b/pymodbus/pdu/register_write_message.py @@ -19,14 +19,14 @@ class WriteSingleRegisterRequest(ModbusPDU): function_code = 6 _rtu_frame_size = 8 - def __init__(self, address=None, value=None, slave=None, transaction=0, skip_encode=0): + def __init__(self, address=None, value=None, slave=None, transaction=0): """Initialize a new instance. :param address: The address to start writing add :param value: The values to write """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.value = value @@ -35,11 +35,7 @@ def encode(self): :returns: The encoded packet """ - packet = struct.pack(">H", self.address) - if self.skip_encode or isinstance(self.value, bytes): - packet += self.value - else: - packet += struct.pack(">H", self.value) + packet = struct.pack(">HH", self.address, self.value) return packet def decode(self, data): @@ -91,14 +87,14 @@ class WriteSingleRegisterResponse(ModbusPDU): function_code = 6 _rtu_frame_size = 8 - def __init__(self, address=0, value=0, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=0, value=0, slave=1, transaction=0): """Initialize a new instance. :param address: The address to start writing add :param value: The values to write """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.value = value @@ -144,14 +140,14 @@ class WriteMultipleRegistersRequest(ModbusPDU): _rtu_byte_count_pos = 6 _pdu_length = 5 # func + adress1 + adress2 + outputQuant1 + outputQuant2 - def __init__(self, address=0, values=None, slave=None, transaction=0, skip_encode=0): + def __init__(self, address=0, values=None, slave=None, transaction=0): """Initialize a new instance. :param address: The address to start writing to :param values: The values to write """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address if values is None: values = [] @@ -167,9 +163,6 @@ def encode(self): :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: if isinstance(value, bytes): packet += value @@ -235,14 +228,14 @@ class WriteMultipleRegistersResponse(ModbusPDU): function_code = 16 _rtu_frame_size = 8 - def __init__(self, address=0, count=0, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=0, count=0, slave=1, transaction=0): """Initialize a new instance. :param address: The address to start writing to :param count: The number of registers to write to """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.count = count @@ -283,7 +276,7 @@ class MaskWriteRegisterRequest(ModbusPDU): function_code = 0x16 _rtu_frame_size = 10 - def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, transaction=0): """Initialize a new instance. :param address: The mask pointer address (0x0000 to 0xffff) @@ -291,7 +284,7 @@ def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, tra :param or_mask: The or bitmask to apply to the register address """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.and_mask = and_mask self.or_mask = or_mask @@ -339,7 +332,7 @@ class MaskWriteRegisterResponse(ModbusPDU): function_code = 0x16 _rtu_frame_size = 10 - def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, transaction=0, skip_encode=False): + def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, transaction=0): """Initialize new instance. :param address: The mask pointer address (0x0000 to 0xffff) @@ -347,7 +340,7 @@ def __init__(self, address=0x0000, and_mask=0xFFFF, or_mask=0x0000, slave=1, tra :param or_mask: The or bitmask applied to the register address """ super().__init__() - super().setBaseData(slave, transaction, skip_encode) + super().setBaseData(slave, transaction) self.address = address self.and_mask = and_mask self.or_mask = or_mask diff --git a/test/framer/test_framer.py b/test/framer/test_framer.py index 02882666f8..91164b29a7 100644 --- a/test/framer/test_framer.py +++ b/test/framer/test_framer.py @@ -431,7 +431,6 @@ def test_framer_encode(self, test_framer, msg): """Test a tcp frame transaction.""" with mock.patch.object(ModbusPDU, "encode") as mock_encode: message = ModbusPDU() - message.setBaseData(0, 0, False) message.transaction_id = 0x0001 message.slave_id = 0xFF message.function_code = 0x01 diff --git a/test/pdu/test_bit_pdu.py b/test/pdu/test_bit_pdu.py index 4e4e0f84c3..232885df0c 100644 --- a/test/pdu/test_bit_pdu.py +++ b/test/pdu/test_bit_pdu.py @@ -24,7 +24,7 @@ def test_bit_read_base_response_encoding(self): """Test basic bit message encoding/decoding.""" for i in range(20): data = [True] * i - handle = bit_msg.ReadBitsResponseBase(data, 0, 0, False) + handle = bit_msg.ReadBitsResponseBase(data, 0, 0) result = handle.encode() handle.decode(result) assert handle.bits[:i] == data @@ -33,12 +33,12 @@ def test_bit_read_base_requests(self): """Test bit read request encoding.""" messages = { bit_msg.ReadCoilsRequest(): b"\x00\x0c\x00\x0e", - bit_msg.ReadBitsResponseBase([1, 0, 1, 1, 0], 0, 0, False): b"\x01\x0d", + bit_msg.ReadBitsResponseBase([1, 0, 1, 1, 0], 0, 0): b"\x01\x0d", } first = True for request, expected in iter(messages.items()): if first: - request.setData(12, 14, 0, 0, False) + request.setData(12, 14, 0, 0) first = False assert request.encode() == expected @@ -46,10 +46,10 @@ async def test_bit_read_update_datastore_value_errors(self): """Test bit read request encoding.""" context = MockContext() pdu1 = bit_msg.ReadCoilsRequest() - pdu1.setData(1, 0x800, 0, 0, False) + pdu1.setData(1, 0x800, 0, 0) requests = [ pdu1, - bit_msg.ReadDiscreteInputsRequest(1, 0x800, 0, 0, False), + bit_msg.ReadDiscreteInputsRequest(1, 0x800, 0, 0), ] for request in requests: result = await request.update_datastore(context) @@ -59,10 +59,10 @@ async def test_bit_read_update_datastore_address_errors(self): """Test bit read request encoding.""" context = MockContext() pdu1 = bit_msg.ReadCoilsRequest() - pdu1.setData(1, 5, 0, 0, False) + pdu1.setData(1, 5, 0, 0) requests = [ pdu1, - bit_msg.ReadDiscreteInputsRequest(1, 5, 0, 0, False), + bit_msg.ReadDiscreteInputsRequest(1, 5, 0, 0), ] for request in requests: result = await request.update_datastore(context) @@ -73,10 +73,10 @@ async def test_bit_read_update_datastore_success(self): context = MockContext() context.validate = lambda a, b, c: True pdu1 = bit_msg.ReadCoilsRequest() - pdu1.setData(1, 5, 0, 0, False) + pdu1.setData(1, 5, 0, 0) requests = [ pdu1, - bit_msg.ReadDiscreteInputsRequest(1, 5, 0, False), + bit_msg.ReadDiscreteInputsRequest(1, 5, 0), ] for request in requests: result = await request.update_datastore(context) @@ -85,18 +85,18 @@ async def test_bit_read_update_datastore_success(self): def test_bit_read_get_response_pdu(self): """Test bit read message get response pdu.""" pdu1 = bit_msg.ReadCoilsRequest() - pdu1.setData(1, 5, 0, 0, False) + pdu1.setData(1, 5, 0, 0) pdu2 = bit_msg.ReadCoilsRequest() - pdu2.setData(1, 8, 0, 0, False) + pdu2.setData(1, 8, 0, 0) pdu3 = bit_msg.ReadCoilsRequest() - pdu3.setData(0, 16, 0, 0, False) + pdu3.setData(0, 16, 0, 0) requests = { pdu1: 3, pdu2: 3, pdu3: 4, - bit_msg.ReadDiscreteInputsRequest(1, 21, 0, 0, False): 5, - bit_msg.ReadDiscreteInputsRequest(1, 24, 0, 0, False): 5, - bit_msg.ReadDiscreteInputsRequest(1, 1900, 0, 0, False): 240, + bit_msg.ReadDiscreteInputsRequest(1, 21, 0, 0): 5, + bit_msg.ReadDiscreteInputsRequest(1, 24, 0, 0): 5, + bit_msg.ReadDiscreteInputsRequest(1, 1900, 0, 0): 240, } for request, expected in iter(requests.items()): pdu_len = request.get_response_pdu_size() diff --git a/test/pdu/test_pdu.py b/test/pdu/test_pdu.py index f7fd45d69f..fc020858d4 100644 --- a/test/pdu/test_pdu.py +++ b/test/pdu/test_pdu.py @@ -21,7 +21,7 @@ class TestPdu: """Test modbus PDU.""" - exception = ExceptionResponse(1, 1, 0, 0, False) + exception = ExceptionResponse(1, 1, 0, 0) async def test_error_methods(self): """Test all error methods.""" @@ -41,7 +41,6 @@ async def test_is_error(self): def test_request_exception(self): """Test request exception.""" request = ModbusPDU() - request.setBaseData(0, 0, False) request.function_code = 1 errors = {ModbusExceptions.decode(c): c for c in range(1, 20)} for error, code in iter(errors.items()): @@ -83,7 +82,7 @@ def test_calculate_rtu_frame_size(self): # -------------------------- requests = [ - (bit_msg.ReadCoilsRequest, (117, 3, 0, 0, False), {}, b'\x01\x00\x75\x00\x03'), + (bit_msg.ReadCoilsRequest, (117, 3, 0, 0), {}, b'\x01\x00\x75\x00\x03'), (bit_msg.ReadDiscreteInputsRequest, (), {"address": 117, "count": 3}, b'\x02\x00\x75\x00\x03'), (bit_msg.WriteSingleCoilRequest, (), {"address": 117, "value": True}, b'\x05\x00\x75\xff\x00'), (bit_msg.WriteMultipleCoilsRequest, (), {"address": 117, "values": [True, False, True]}, b'\x0f\x00\x75\x00\x03\x01\x05'), diff --git a/test/pdu/test_register_write_messages.py b/test/pdu/test_register_write_messages.py index 40d64ab3dc..e8d4ef50a7 100644 --- a/test/pdu/test_register_write_messages.py +++ b/test/pdu/test_register_write_messages.py @@ -49,12 +49,6 @@ def setup_method(self): ): b"\x00\x01\x00\x03\x06\x00\x0a\x00\x0b\x00\x0c", WriteMultipleRegistersRequest(1, 0xD): b"\x00\x01\x00\x01\x02\x00\x0D", WriteMultipleRegistersResponse(1, 5): b"\x00\x01\x00\x05", - WriteSingleRegisterRequest( - 1, self.payload[0], skip_encode=True - ): b"\x00\x01\x12\x34", - WriteMultipleRegistersRequest( - 1, self.payload, skip_encode=True - ): b"\x00\x01\x00\x01\x02\x12\x34", } def test_register_write_requests_encode(self): diff --git a/test/sub_client/test_client.py b/test/sub_client/test_client.py index 860d57bf43..7c0861a56c 100755 --- a/test/sub_client/test_client.py +++ b/test/sub_client/test_client.py @@ -240,7 +240,6 @@ async def test_client_instanciate( client.connect = lambda: False client.transport = None pdu = ModbusPDU() - pdu.setBaseData(0, 0, False) with pytest.raises(ConnectionException): client.execute(False, pdu) @@ -363,7 +362,7 @@ async def test_client_protocol_handler(): transport = mock.MagicMock() base.ctx.connection_made(transport=transport) reply = pdu_bit.ReadCoilsRequest() - reply.setData(1, 1, 0, 0, False) + reply.setData(1, 1, 0, 0) reply.transaction_id = 0x00 base.ctx._handle_response(None) # pylint: disable=protected-access base.ctx._handle_response(reply) # pylint: disable=protected-access @@ -417,7 +416,7 @@ async def test_client_protocol_execute(): ), ) request = pdu_bit.ReadCoilsRequest() - request.setData(1, 1, 0, 0, False) + request.setData(1, 1, 0, 0) transport = MockTransport(base, request) base.ctx.connection_made(transport=transport) @@ -436,7 +435,7 @@ async def test_client_execute_broadcast(): ), ) request = pdu_bit.ReadCoilsRequest() - request.setData(1, 1, 0, 0, False) + request.setData(1, 1, 0, 0) transport = MockTransport(base, request) base.ctx.connection_made(transport=transport) assert await base.async_execute(False, request) @@ -453,7 +452,7 @@ async def test_client_execute_broadcast_no(): ), ) request = pdu_bit.ReadCoilsRequest() - request.setData(1, 1, 0, 0, False) + request.setData(1, 1, 0, 0) transport = MockTransport(base, request) base.ctx.connection_made(transport=transport) assert not await base.async_execute(True, request) @@ -470,7 +469,7 @@ async def test_client_protocol_retry(): ), ) request = pdu_bit.ReadCoilsRequest() - request.setData(1, 1, 0, 0, False) + request.setData(1, 1, 0, 0) transport = MockTransport(base, request, retries=2) base.ctx.connection_made(transport=transport) @@ -494,7 +493,7 @@ async def test_client_protocol_timeout(): # Avoid creating do_reconnect() task base.ctx.connection_lost = mock.MagicMock() request = pdu_bit.ReadCoilsRequest() - request.setData(1, 1, 0, 0, False) + request.setData(1, 1, 0, 0) transport = MockTransport(base, request, retries=4) base.ctx.connection_made(transport=transport) @@ -698,7 +697,6 @@ async def test_client_build_response(): comm_params=CommParams(), ) pdu = ModbusPDU() - pdu.setBaseData(0, 0, False) with pytest.raises(ConnectionException): await client.build_response(pdu) @@ -707,7 +705,6 @@ async def test_client_mixin_execute(): """Test dummy execute for both sync and async.""" client = ModbusClientMixin() pdu = ModbusPDU() - pdu.setBaseData(0, 0, False) with pytest.raises(NotImplementedError): client.execute(False, pdu) with pytest.raises(NotImplementedError): diff --git a/test/sub_current/test_transaction.py b/test/sub_current/test_transaction.py index b0af32860b..67f70483d5 100755 --- a/test/sub_current/test_transaction.py +++ b/test/sub_current/test_transaction.py @@ -166,7 +166,7 @@ def test_get_transaction_manager_transaction(self): """Test the getting a transaction from the transaction manager.""" self._manager.reset() handle = ModbusPDU() - handle.setBaseData(0, self._manager.getNextTID(), False) + handle.setBaseData(0, self._manager.getNextTID()) self._manager.addTransaction(handle) result = self._manager.getTransaction(handle.transaction_id) assert handle is result @@ -175,7 +175,7 @@ def test_delete_transaction_manager_transaction(self): """Test deleting a transaction from the dict transaction manager.""" self._manager.reset() handle = ModbusPDU() - handle.setBaseData(0, self._manager.getNextTID(), False) + handle.setBaseData(0, self._manager.getNextTID()) self._manager.addTransaction(handle) self._manager.delTransaction(handle.transaction_id) assert not self._manager.getTransaction(handle.transaction_id)