From cf2e0edbc0b2d45b56e3d899dce439cb173c68d2 Mon Sep 17 00:00:00 2001 From: Justin Standring Date: Mon, 4 Mar 2024 17:04:46 +1300 Subject: [PATCH] Fix field encoding of Read File Record Response The Modbus spec V1.1b3 states the following field order: 1. Function Code (0x14) 2. Resp. data Length (in bytes) 3. Sub-Req. x, File Resp. length (in registers) 4. Sub-Req. x, Reference Type: 0x6 (fixed) 5. Sub-Req. x, Record Data 6. Sub-Req. x+1, ... Previously, field 4 was before field 4. --- pymodbus/file_message.py | 2 +- test/test_file_message.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pymodbus/file_message.py b/pymodbus/file_message.py index 82c0ad7e4..e08e8f99d 100644 --- a/pymodbus/file_message.py +++ b/pymodbus/file_message.py @@ -181,7 +181,7 @@ def encode(self): total = sum(record.response_length + 1 for record in self.records) packet = struct.pack("B", total) for record in self.records: - packet += struct.pack(">BB", 0x06, record.record_length) + packet += struct.pack(">BB", record.record_length, 0x06) packet += record.record_data return packet diff --git a/test/test_file_message.py b/test/test_file_message.py index a45c7c1df..ee71ba1ac 100644 --- a/test/test_file_message.py +++ b/test/test_file_message.py @@ -161,10 +161,10 @@ def test_read_file_record_request_execute(self): def test_read_file_record_response_encode(self): """Test basic bit message encoding/decoding.""" - records = [FileRecord(record_data=b"\x00\x01\x02\x03")] + records = [FileRecord(record_data=b"\x00\x01\x02\x03\x04\x05")] handle = ReadFileRecordResponse(records) result = handle.encode() - assert result == b"\x06\x06\x02\x00\x01\x02\x03" + assert result == b"\x08\x03\x06\x00\x01\x02\x03\x04\x05" def test_read_file_record_response_decode(self): """Test basic bit message encoding/decoding."""