Skip to content

Commit

Permalink
Fix field encoding of Read File Record Response
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
justinsg committed Mar 4, 2024
1 parent cba37e2 commit cf2e0ed
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pymodbus/file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/test_file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit cf2e0ed

Please sign in to comment.