Skip to content
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
2 changes: 1 addition & 1 deletion pymodbus/pdu/register_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def encode(self) -> bytes:
self.verifyAddress(address=self.read_address)
self.verifyAddress(address=self.write_address)
self.verifyCount(self.MAX_READ_COUNT, count=self.read_count)
self.verifyCount(self.MAX_READ_COUNT, count=self.write_count)
self.verifyCount(self.MAX_WRITE_COUNT, count=self.write_count)
result = struct.pack(
">HHHHB",
self.read_address,
Expand Down
29 changes: 29 additions & 0 deletions test/pdu/test_register_read_messages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test register read messages."""

import struct
from unittest import mock

import pytest
Expand Down Expand Up @@ -84,6 +85,34 @@ def test_register_read_response_decode_error(self):
reg.decode(b"\x14\x00\x03\x00\x11")
assert exc_info.value.fcode == reg.function_code

def test_readwrite_encode_write_count_limit(self):
"""Encoding must reject a write count above the FC23 maximum of 121.

121 write registers is a 252 byte PDU, 122 is 254 and does not fit in
the 253 byte MODBUS PDU.
"""

def build(count):
return ReadWriteMultipleRegistersRequest(
read_address=1,
read_count=1,
write_address=1,
write_registers=[0] * count,
)

assert len(build(121).encode()) + 1 <= 253
with pytest.raises(ValueError): # noqa: PT011
build(122).encode()

def test_readwrite_decode_write_count_above_limit(self):
"""Decoding must accept 122..125 so the server can answer ILLEGAL_VALUE.

Raising here would make the server drop the frame without a response.
"""
request = ReadWriteMultipleRegistersRequest()
request.decode(struct.pack(">HHHHB", 1, 1, 1, 122, 244) + b"\x00\x00" * 122)
assert request.write_count == 122

async def test_register_read_requests_count_errors(self, mock_server_context):
"""This tests that the register request messages.

Expand Down