From 499521f43202252c6a9d59f7a4fd90fc650aafef Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 23 Jul 2024 17:31:32 -0500 Subject: [PATCH 1/3] Inherit from ReadHoldingRegistersResponse --- pymodbus/pdu/register_read_message.py | 37 +-------------------------- 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/pymodbus/pdu/register_read_message.py b/pymodbus/pdu/register_read_message.py index 8fd335b32..9bcd113da 100644 --- a/pymodbus/pdu/register_read_message.py +++ b/pymodbus/pdu/register_read_message.py @@ -351,7 +351,7 @@ def __str__(self): ) -class ReadWriteMultipleRegistersResponse(ModbusResponse): +class ReadWriteMultipleRegistersResponse(ReadHoldingRegistersResponse): """Read/write multiple registers. The normal response contains the data from the group of registers that @@ -362,38 +362,3 @@ class ReadWriteMultipleRegistersResponse(ModbusResponse): """ function_code = 23 - _rtu_byte_count_pos = 2 - - def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False): - """Initialize a new instance. - - :param values: The register values to write - """ - super().__init__(slave, transaction, protocol, skip_encode) - 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 the register response packet. - - :param data: The response to decode - """ - bytecount = int(data[0]) - for i in range(1, bytecount, 2): - self.registers.append(struct.unpack(">H", data[i : i + 2])[0]) - - def __str__(self): - """Return a string representation of the instance. - - :returns: A string representation of the instance - """ - return f"ReadWriteNRegisterResponse ({len(self.registers)})" From db9cf712187ed19d9cbfff65d75f4054b8489ef2 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 23 Jul 2024 18:53:28 -0500 Subject: [PATCH 2/3] fix test --- test/sub_function_codes/test_register_read_messages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sub_function_codes/test_register_read_messages.py b/test/sub_function_codes/test_register_read_messages.py index 9b8bafef8..72f0452f2 100644 --- a/test/sub_function_codes/test_register_read_messages.py +++ b/test/sub_function_codes/test_register_read_messages.py @@ -88,7 +88,7 @@ def test_register_read_response_decode(self): [0x0A, 0x0B, 0x0C], [0x0A, 0x0B, 0x0C], [0x0A, 0x0B, 0x0C], - [0x0A, 0x0B, 0x0C, 0x0A, 0x0B, 0x0C], + [0x0A, 0x0B, 0x0C], ] values = sorted( self.response_read.items(), From 037678a4ec7e492000b5a760c54f7fcfe0fb9df7 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 23 Jul 2024 19:02:12 -0500 Subject: [PATCH 3/3] simplify test code --- .../test_register_read_messages.py | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/test/sub_function_codes/test_register_read_messages.py b/test/sub_function_codes/test_register_read_messages.py index 72f0452f2..ce5504e65 100644 --- a/test/sub_function_codes/test_register_read_messages.py +++ b/test/sub_function_codes/test_register_read_messages.py @@ -30,10 +30,9 @@ class TestReadRegisterMessages: * Read Holding Registers """ - value = None - values = None - request_read = None - response_read = None + values: list + request_read: dict + response_read: dict def setup_method(self): """Initialize the test environment and builds request/result encoding pairs.""" @@ -42,7 +41,6 @@ def setup_method(self): "read_count": 5, "write_address": 1, } - self.value = 0xABCD self.values = [0xA, 0xB, 0xC] self.request_read = { ReadRegistersRequestBase(1, 5): b"\x00\x01\x00\x05", @@ -84,20 +82,9 @@ def test_register_read_responses(self): def test_register_read_response_decode(self): """Test register read response.""" - registers = [ - [0x0A, 0x0B, 0x0C], - [0x0A, 0x0B, 0x0C], - [0x0A, 0x0B, 0x0C], - [0x0A, 0x0B, 0x0C], - ] - values = sorted( - self.response_read.items(), - key=lambda x: str(x), # pylint: disable=unnecessary-lambda - ) - for packet, register in zip(values, registers): - request, response = packet - request.decode(response) - assert request.registers == register + for response, packet in self.response_read.items(): + response.decode(packet) + assert response.registers == self.values async def test_register_read_requests_count_errors(self): """This tests that the register request messages.