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

Restrict write_registers etc to list[int]. #2469

Merged
merged 1 commit into from
Nov 24, 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
7 changes: 3 additions & 4 deletions examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ async def async_write_registers_mypy(client: ModbusBaseClient) -> None:
rr = await client.read_holding_registers(1, count=len(regs1), slave=SLAVE)
assert not rr.isError() # test that call was OK

regs2: list[bytes] = [b'\x01\x02', b'\x03\x04']
await client.write_registers(1, regs2, slave=SLAVE)
rr = await client.read_holding_registers(1, count=len(regs2), slave=SLAVE)
assert not rr.isError() # test that call was OK
# regs2: list[bytes] = [b'\x01\x02', b'\x03\x04']
# await client.write_registers(1, regs2, slave=SLAVE)
# NOT ALLOWED


async def async_handle_input_registers(client):
Expand Down
15 changes: 2 additions & 13 deletions pymodbus/client/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import struct
from collections.abc import Sequence
from enum import Enum
from typing import Generic, TypeVar

Expand Down Expand Up @@ -159,7 +158,7 @@ def write_coil(self, address: int, value: bool, *, slave: int = 1, no_response_e
pdu = pdu_bit.WriteSingleCoilRequest(address=address, bits=[value], slave_id=slave)
return self.execute(no_response_expected, pdu)

def write_register(self, address: int, value: bytes | int, *, slave: int = 1, no_response_expected: bool = False) -> T:
def write_register(self, address: int, value: int, *, slave: int = 1, no_response_expected: bool = False) -> T:
"""Write register (code 0x06).

:param address: Address to write to
Expand Down Expand Up @@ -502,7 +501,7 @@ def write_coils(
def write_registers(
self,
address: int,
values: Sequence[int | bytes],
values: list[int],
*,
slave: int = 1,
no_response_expected: bool = False
Expand All @@ -515,11 +514,6 @@ def write_registers(
:param no_response_expected: (optional) The client will not expect a response to the request
:raises ModbusException:

.. tip::
values= parameter:
entries defined as bytes are silently converted to int !
only list[int], list[bytes] or list[bytes | int] are expected (others may work unsupported)

This function is used to write a block of contiguous registers
(1 to approx. 120 registers) in a remote device.
"""
Expand Down Expand Up @@ -629,11 +623,6 @@ def readwrite_registers(
:param no_response_expected: (optional) The client will not expect a response to the request
:raises ModbusException:

.. tip::
values= parameter:
entries defined as bytes are silently converted to int !
only list[int], list[bytes] or list[bytes | int] are expected (others may work unsupported)

This function performs a combination of one read operation and one
write operation in a single MODBUS transaction. The write
operation is performed before the read.
Expand Down
13 changes: 3 additions & 10 deletions pymodbus/pdu/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import asyncio
import struct
from abc import abstractmethod
from collections.abc import Sequence
from typing import cast

from pymodbus.exceptions import NotImplementedException
from pymodbus.logging import Log
Expand All @@ -25,21 +23,16 @@ def __init__(self,
address: int = 0,
count: int = 0,
bits: list[bool] | None = None,
registers: Sequence[bytes | int] | None = None,
registers: list[int] | None = None,
status: int = 1,
) -> None:
"""Initialize the base data for a modbus request."""
self.slave_id: int = slave_id
self.transaction_id: int = transaction_id
self.address: int = address
self.bits: list[bool] = bits or []
if not registers:
registers = []
self.registers: list[int] = cast(list[int], registers)
for i, value in enumerate(registers):
if isinstance(value, bytes):
self.registers[i] = int.from_bytes(value, byteorder="big")
self.count: int = count or len(registers)
self.registers: list[int] = registers or []
self.count: int = count or len(self.registers)
self.status: int = status
self.fut: asyncio.Future

Expand Down
5 changes: 3 additions & 2 deletions test/pdu/test_pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ def test_pdu_instance_extras(self, pdutype, kwargs):
def test_pdu_register_as_byte(self):
"""Test validate functions."""
registers =[b'ab', b'cd']
# NOT ALLOWED, NO conversion.
req = reg_msg.ReadHoldingRegistersRequest(address=117, registers=registers, count=3)
assert len(req.registers) == 2
assert req.registers[0] == 24930
assert req.registers[1] == 25444
assert req.registers[0] != 24930
assert req.registers[1] != 25444

def test_pdu_validate_address(self):
"""Test validate functions."""
Expand Down