Skip to content

Commit

Permalink
remove kwargs PDU messagees. (#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Jul 19, 2024
1 parent 18c839b commit 552c56e
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 98 deletions.
2 changes: 1 addition & 1 deletion examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async def async_handle_holding_registers(client):
assert not rr.isError() # test that call was OK
assert rr.registers == [10] * 8

_logger.info("### write read holding registers, using **kwargs")
_logger.info("### write read holding registers")
arguments = {
"read_address": 1,
"read_count": 8,
Expand Down
2 changes: 1 addition & 1 deletion examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def handle_holding_registers(client):
assert not rr.isError() # test that call was OK
assert rr.registers == [10] * 8

_logger.info("### write read holding registers, using **kwargs")
_logger.info("### write read holding registers")
arguments = {
"read_address": 1,
"read_count": 8,
Expand Down
13 changes: 7 additions & 6 deletions examples/client_custom_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CustomModbusResponse(ModbusResponse):
function_code = 55
_rtu_byte_count_pos = 2

def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize."""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.values = values or []
Expand Down Expand Up @@ -68,7 +68,7 @@ class CustomModbusRequest(ModbusRequest):
function_code = 55
_rtu_frame_size = 8

def __init__(self, address=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, address=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize."""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.address = address
Expand Down Expand Up @@ -100,12 +100,12 @@ def execute(self, context):
class Read16CoilsRequest(ReadCoilsRequest):
"""Read 16 coils in one request."""

def __init__(self, address, **kwargs):
def __init__(self, address, count=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start reading from
"""
ReadCoilsRequest.__init__(self, address, 16, **kwargs)
ReadCoilsRequest.__init__(self, address, count=16, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)


# --------------------------------------------------------------------------- #
Expand All @@ -126,12 +126,13 @@ async def main(host="localhost", port=5020):

# new modbus function code.
client.register(CustomModbusResponse)
request = CustomModbusRequest(32, slave=1)
slave=1
request = CustomModbusRequest(32, slave=slave)
result = await client.execute(request)
print(result)

# inherited request
request = Read16CoilsRequest(32, slave=1)
request = Read16CoilsRequest(32, slave)
result = await client.execute(request)
print(result)

Expand Down
21 changes: 8 additions & 13 deletions pymodbus/client/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,48 +64,42 @@ def execute(self, _request: ModbusRequest) -> T:
)

def read_coils(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> T:
self, address: int, count: int = 1, slave: int = 0) -> T:
"""Read coils (code 0x01).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:param kwargs: (optional) Experimental parameters.
:raises ModbusException:
"""
return self.execute(
pdu_bit_read.ReadCoilsRequest(address, count, slave, **kwargs)
pdu_bit_read.ReadCoilsRequest(address, count, slave)
)

def read_discrete_inputs(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> T:
self, address: int, count: int = 1, slave: int = 0) -> T:
"""Read discrete inputs (code 0x02).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:param kwargs: (optional) Experimental parameters.
:raises ModbusException:
"""
return self.execute(
pdu_bit_read.ReadDiscreteInputsRequest(address, count, slave, **kwargs)
pdu_bit_read.ReadDiscreteInputsRequest(address, count, slave)
)

def read_holding_registers(
self, address: int, count: int = 1, slave: int = 0, **kwargs: Any
) -> T:
self, address: int, count: int = 1, slave: int = 0) -> T:
"""Read holding registers (code 0x03).
:param address: Start address to read from
:param count: (optional) Number of coils to read
:param slave: (optional) Modbus slave ID
:param kwargs: (optional) Experimental parameters.
:raises ModbusException:
"""
return self.execute(
pdu_reg_read.ReadHoldingRegistersRequest(address, count, slave, **kwargs)
pdu_reg_read.ReadHoldingRegistersRequest(address, count, slave)
)

def read_input_registers(
Expand Down Expand Up @@ -457,14 +451,15 @@ def readwrite_registers(
:param kwargs:
:raises ModbusException:
"""
read_address = kwargs.pop("address", read_address)
return self.execute(
pdu_reg_read.ReadWriteMultipleRegistersRequest(
read_address=read_address,
read_count=read_count,
write_address=write_address,
write_registers=values,
slave=slave,
**kwargs,
**kwargs
)
)

Expand Down
12 changes: 6 additions & 6 deletions pymodbus/pdu/bit_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ReadBitsRequestBase(ModbusRequest):

_rtu_frame_size = 8

def __init__(self, address, count, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, address, count, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize the read request data.
:param address: The start address to read from
Expand Down Expand Up @@ -75,7 +75,7 @@ class ReadBitsResponseBase(ModbusResponse):

_rtu_byte_count_pos = 2

def __init__(self, values, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, values, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The requested values to be returned
Expand Down Expand Up @@ -146,7 +146,7 @@ class ReadCoilsRequest(ReadBitsRequestBase):
function_code = 1
function_code_name = "read_coils"

def __init__(self, address=None, count=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, address=None, count=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start reading from
Expand Down Expand Up @@ -193,7 +193,7 @@ class ReadCoilsResponse(ReadBitsResponseBase):

function_code = 1

def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The request values to respond with
Expand All @@ -214,7 +214,7 @@ class ReadDiscreteInputsRequest(ReadBitsRequestBase):
function_code = 2
function_code_name = "read_discrete_input"

def __init__(self, address=None, count=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, address=None, count=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The address to start reading from
Expand Down Expand Up @@ -261,7 +261,7 @@ class ReadDiscreteInputsResponse(ReadBitsResponseBase):

function_code = 2

def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, values=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param values: The request values to respond with
Expand Down
8 changes: 4 additions & 4 deletions pymodbus/pdu/bit_write_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class WriteSingleCoilRequest(ModbusRequest):

_rtu_frame_size = 8

def __init__(self, address=None, value=None, slave=None, transaction=0, protocol=0, skip_encode=0, **_kwargs):
def __init__(self, address=None, value=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance.
:param address: The variable address to write
Expand Down Expand Up @@ -119,7 +119,7 @@ class WriteSingleCoilResponse(ModbusResponse):
function_code = 5
_rtu_frame_size = 8

def __init__(self, address=None, value=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, address=None, value=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The variable address written to
Expand Down Expand Up @@ -173,7 +173,7 @@ class WriteMultipleCoilsRequest(ModbusRequest):
function_code_name = "write_coils"
_rtu_byte_count_pos = 6

def __init__(self, address=None, values=None, slave=None, transaction=0, protocol=0, skip_encode=0, **_kwargs):
def __init__(self, address=None, values=None, slave=None, transaction=0, protocol=0, skip_encode=0):
"""Initialize a new instance.
:param address: The starting request address
Expand Down Expand Up @@ -256,7 +256,7 @@ class WriteMultipleCoilsResponse(ModbusResponse):
function_code = 15
_rtu_frame_size = 8

def __init__(self, address=None, count=None, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, address=None, count=None, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance.
:param address: The starting variable address written to
Expand Down
37 changes: 19 additions & 18 deletions pymodbus/pdu/diag_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DiagnosticStatusRequest(ModbusRequest):
function_code_name = "diagnostic_status"
_rtu_frame_size = 8

def __init__(self, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a diagnostic request."""
ModbusRequest.__init__(self, slave, transaction, protocol, skip_encode)
self.message = None
Expand Down Expand Up @@ -131,7 +131,7 @@ class DiagnosticStatusResponse(ModbusResponse):
function_code = 0x08
_rtu_frame_size = 8

def __init__(self, slave=0, transaction=0, protocol=0, skip_encode=False, **_kwargs):
def __init__(self, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a diagnostic response."""
ModbusResponse.__init__(self, slave, transaction, protocol, skip_encode)
self.message = None
Expand Down Expand Up @@ -188,15 +188,15 @@ class DiagnosticStatusSimpleRequest(DiagnosticStatusRequest):
the execute method
"""

def __init__(self, data=0x0000, **kwargs):
def __init__(self, data=0x0000, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a simple diagnostic request.
The data defaults to 0x0000 if not provided as over half
of the functions require it.
:param data: The data to send along with the request
"""
DiagnosticStatusRequest.__init__(self, **kwargs)
DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message = data

async def execute(self, *args):
Expand All @@ -213,12 +213,12 @@ class DiagnosticStatusSimpleResponse(DiagnosticStatusResponse):
2 bytes of data.
"""

def __init__(self, data=0x0000, **kwargs):
def __init__(self, data=0x0000, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Return a simple diagnostic response.
:param data: The resulting data to return to the client
"""
DiagnosticStatusResponse.__init__(self, **kwargs)
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message = data


Expand All @@ -235,12 +235,12 @@ class ReturnQueryDataRequest(DiagnosticStatusRequest):

sub_function_code = 0x0000

def __init__(self, message=b"\x00\x00", slave=None, **kwargs):
def __init__(self, message=b"\x00\x00", slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance of the request.
:param message: The message to send to loopback
"""
DiagnosticStatusRequest.__init__(self, slave=slave, **kwargs)
DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if not isinstance(message, bytes):
raise ModbusException(f"message({type(message)}) must be bytes")
self.message = message
Expand All @@ -263,12 +263,12 @@ class ReturnQueryDataResponse(DiagnosticStatusResponse):

sub_function_code = 0x0000

def __init__(self, message=b"\x00\x00", **kwargs):
def __init__(self, message=b"\x00\x00", slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new instance of the response.
:param message: The message to loopback
"""
DiagnosticStatusResponse.__init__(self, **kwargs)
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if not isinstance(message, bytes):
raise ModbusException(f"message({type(message)}) must be bytes")
self.message = message
Expand All @@ -290,12 +290,12 @@ class RestartCommunicationsOptionRequest(DiagnosticStatusRequest):

sub_function_code = 0x0001

def __init__(self, toggle=False, slave=None, **kwargs):
def __init__(self, toggle=False, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new request.
:param toggle: Set to True to toggle, False otherwise
"""
DiagnosticStatusRequest.__init__(self, slave=slave, **kwargs)
DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if toggle:
self.message = [ModbusStatus.ON]
else:
Expand Down Expand Up @@ -323,12 +323,12 @@ class RestartCommunicationsOptionResponse(DiagnosticStatusResponse):

sub_function_code = 0x0001

def __init__(self, toggle=False, **kwargs):
def __init__(self, toggle=False, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize a new response.
:param toggle: Set to True if we toggled, False otherwise
"""
DiagnosticStatusResponse.__init__(self, **kwargs)
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
if toggle:
self.message = [ModbusStatus.ON]
else:
Expand Down Expand Up @@ -434,9 +434,9 @@ class ForceListenOnlyModeResponse(DiagnosticStatusResponse):
sub_function_code = 0x0004
should_respond = False

def __init__(self, **kwargs):
def __init__(self, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize to block a return response."""
DiagnosticStatusResponse.__init__(self, **kwargs)
DiagnosticStatusResponse.__init__(self, slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message = []


Expand Down Expand Up @@ -816,9 +816,10 @@ class GetClearModbusPlusRequest(DiagnosticStatusSimpleRequest):

sub_function_code = 0x0015

def __init__(self, slave=None, **kwargs):
def __init__(self, data=0, slave=0, transaction=0, protocol=0, skip_encode=False):
"""Initialize."""
super().__init__(slave=slave, **kwargs)
super().__init__(slave=slave, transaction=transaction, protocol=protocol, skip_encode=skip_encode)
self.message=data

def get_response_pdu_size(self):
"""Return a series of 54 16-bit words (108 bytes) in the data field of the response.
Expand Down
Loading

0 comments on commit 552c56e

Please sign in to comment.