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

Fix encoding & decoding of ReadFileRecordResponse #2319

Merged
merged 2 commits into from
Sep 14, 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
9 changes: 6 additions & 3 deletions pymodbus/pdu/file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,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", record.record_length, 0x06)
packet += struct.pack(">BB", record.response_length, 0x06)
packet += record.record_data
return packet

Expand All @@ -185,11 +185,14 @@ def decode(self, data):
response_length, reference_type = struct.unpack(
">BB", data[count : count + 2]
)
count += response_length + 1 # the count is not included
count += 2

record_length = response_length - 1 # response length includes the type byte
record = FileRecord(
response_length=response_length,
record_data=data[count - response_length + 1 : count],
record_data=data[count : count + record_length],
)
count += record_length
if reference_type == 0x06:
self.records.append(record)

Expand Down
15 changes: 10 additions & 5 deletions test/test_file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,22 @@ def test_read_file_record_response_encode(self):
records = [FileRecord(record_data=b"\x00\x01\x02\x03\x04\x05")]
handle = ReadFileRecordResponse(records)
result = handle.encode()
assert result == b"\x08\x03\x06\x00\x01\x02\x03\x04\x05"
assert result == b"\x08\x07\x06\x00\x01\x02\x03\x04\x05"

def test_read_file_record_response_decode(self):
"""Test basic bit message encoding/decoding."""
record = FileRecord(
record1 = FileRecord(
file_number=0x00, record_number=0x00, record_data=b"\x0d\xfe\x00\x20"
)
request = b"\x0c\x05\x06\x0d\xfe\x00\x20\x05\x05\x06\x33\xcd\x00\x40"
record2 = FileRecord(
file_number=0x00, record_number=0x00, record_data=b"\x33\xcd\x00\x40"
)
response = b"\x0c\x05\x06\x0d\xfe\x00\x20\x05\x06\x33\xcd\x00\x40"
handle = ReadFileRecordResponse()
handle.decode(request)
assert handle.records[0] == record
handle.decode(response)

assert handle.records[0] == record1
assert handle.records[1] == record2

def test_read_file_record_response_rtu_frame_size(self):
"""Test basic bit message encoding/decoding."""
Expand Down