Skip to content

Commit 34acafd

Browse files
pkubanekjaniversen
andauthored
Add unit to arguments (#1041)
* Fix #673 Co-authored-by: jan iversen <[email protected]>
1 parent 8ee3915 commit 34acafd

File tree

7 files changed

+117
-73
lines changed

7 files changed

+117
-73
lines changed

pymodbus/bit_read_message.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=missing-type-doc
33
import struct
44

5+
from pymodbus.constants import Defaults
56
from pymodbus.pdu import ModbusExceptions as merror, ModbusRequest, ModbusResponse
67
from pymodbus.utilities import pack_bitstring, unpack_bitstring
78

@@ -11,13 +12,14 @@ class ReadBitsRequestBase(ModbusRequest):
1112

1213
_rtu_frame_size = 8
1314

14-
def __init__(self, address, count, **kwargs):
15+
def __init__(self, address, count, unit=Defaults.UnitId, **kwargs):
1516
"""Initialize the read request data.
1617
1718
:param address: The start address to read from
1819
:param count: The number of bits after "address" to read
20+
:param unit: Modbus slave unit ID
1921
"""
20-
ModbusRequest.__init__(self, **kwargs)
22+
ModbusRequest.__init__(self, unit, **kwargs)
2123
self.address = address
2224
self.count = count
2325

@@ -64,12 +66,13 @@ class ReadBitsResponseBase(ModbusResponse):
6466

6567
_rtu_byte_count_pos = 2
6668

67-
def __init__(self, values, **kwargs):
69+
def __init__(self, values, unit=Defaults.UnitId, **kwargs):
6870
"""Initialize a new instance.
6971
7072
:param values: The requested values to be returned
73+
:param unit: Modbus slave unit ID
7174
"""
72-
ModbusResponse.__init__(self, **kwargs)
75+
ModbusResponse.__init__(self, unit, **kwargs)
7376

7477
#: A list of booleans representing bit values
7578
self.bits = values or []
@@ -133,13 +136,14 @@ class ReadCoilsRequest(ReadBitsRequestBase):
133136

134137
function_code = 1
135138

136-
def __init__(self, address=None, count=None, **kwargs):
139+
def __init__(self, address=None, count=None, unit=Defaults.UnitId, **kwargs):
137140
"""Initialize a new instance.
138141
139142
:param address: The address to start reading from
140143
:param count: The number of bits to read
144+
:param unit: Modbus slave unit ID
141145
"""
142-
ReadBitsRequestBase.__init__(self, address, count, **kwargs)
146+
ReadBitsRequestBase.__init__(self, address, count, unit, **kwargs)
143147

144148
def execute(self, context):
145149
"""Run a read coils request against a datastore.
@@ -177,12 +181,13 @@ class ReadCoilsResponse(ReadBitsResponseBase):
177181

178182
function_code = 1
179183

180-
def __init__(self, values=None, **kwargs):
184+
def __init__(self, values=None, unit=Defaults.UnitId, **kwargs):
181185
"""Initialize a new instance.
182186
183187
:param values: The request values to respond with
188+
:param unit: Modbus slave unit ID
184189
"""
185-
ReadBitsResponseBase.__init__(self, values, **kwargs)
190+
ReadBitsResponseBase.__init__(self, values, unit, **kwargs)
186191

187192

188193
class ReadDiscreteInputsRequest(ReadBitsRequestBase):
@@ -196,13 +201,14 @@ class ReadDiscreteInputsRequest(ReadBitsRequestBase):
196201

197202
function_code = 2
198203

199-
def __init__(self, address=None, count=None, **kwargs):
204+
def __init__(self, address=None, count=None, unit=Defaults.UnitId, **kwargs):
200205
"""Initialize a new instance.
201206
202207
:param address: The address to start reading from
203208
:param count: The number of bits to read
209+
:param unit: Modbus slave unit ID
204210
"""
205-
ReadBitsRequestBase.__init__(self, address, count, **kwargs)
211+
ReadBitsRequestBase.__init__(self, address, count, unit, **kwargs)
206212

207213
def execute(self, context):
208214
"""Run a read discrete input request against a datastore.
@@ -240,12 +246,13 @@ class ReadDiscreteInputsResponse(ReadBitsResponseBase):
240246

241247
function_code = 2
242248

243-
def __init__(self, values=None, **kwargs):
249+
def __init__(self, values=None, unit=Defaults.UnitId, **kwargs):
244250
"""Initialize a new instance.
245251
246252
:param values: The request values to respond with
253+
:param unit: Modbus slave unit ID
247254
"""
248-
ReadBitsResponseBase.__init__(self, values, **kwargs)
255+
ReadBitsResponseBase.__init__(self, values, unit, **kwargs)
249256

250257

251258
# ---------------------------------------------------------------------------#

pymodbus/client/mixin.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
from pymodbus.bit_read_message import ReadCoilsRequest, ReadDiscreteInputsRequest
5858
from pymodbus.bit_write_message import WriteMultipleCoilsRequest, WriteSingleCoilRequest
59+
from pymodbus.constants import Defaults
5960
from pymodbus.register_read_message import (
6061
ReadHoldingRegistersRequest,
6162
ReadInputRegistersRequest,
@@ -91,15 +92,16 @@ class ModbusClientMixin:
9192
last_frame_end = 0
9293
silent_interval = 0
9394

94-
def read_coils(self, address, count=1, **kwargs):
95+
def read_coils(self, address, count=1, unit=Defaults.UnitId, **kwargs):
9596
"""Read coils.
9697
9798
:param address: The starting address to read from
9899
:param count: The number of coils to read
100+
:param unit: Modbus slave unit ID
99101
:param kwargs:
100102
:returns: A deferred response handle
101103
"""
102-
request = ReadCoilsRequest(address, count, **kwargs)
104+
request = ReadCoilsRequest(address, count, unit, **kwargs)
103105
return self.execute(request) # pylint: disable=no-member
104106

105107
def read_discrete_inputs(self, address, count=1, **kwargs):
@@ -157,26 +159,28 @@ def write_registers(self, address, values, **kwargs):
157159
request = WriteMultipleRegistersRequest(address, values, **kwargs)
158160
return self.execute(request) # pylint: disable=no-member
159161

160-
def read_holding_registers(self, address, count=1, **kwargs):
162+
def read_holding_registers(self, address, count=1, unit=Defaults.UnitId, **kwargs):
161163
"""Read holding registers.
162164
163165
:param address: The starting address to read from
164166
:param count: The number of registers to read
167+
:param unit: Modbus slave unit ID
165168
:param kwargs:
166169
:returns: A deferred response handle
167170
"""
168-
request = ReadHoldingRegistersRequest(address, count, **kwargs)
171+
request = ReadHoldingRegistersRequest(address, count, unit, **kwargs)
169172
return self.execute(request) # pylint: disable=no-member
170173

171-
def read_input_registers(self, address, count=1, **kwargs):
174+
def read_input_registers(self, address, count=1, unit=Defaults.UnitId, **kwargs):
172175
"""Read input registers.
173176
174177
:param address: The starting address to read from
175178
:param count: The number of registers to read
179+
:param unit: Modbus slave unit ID
176180
:param kwargs:
177181
:returns: A deferred response handle
178182
"""
179-
request = ReadInputRegistersRequest(address, count, **kwargs)
183+
request = ReadInputRegistersRequest(address, count, unit, **kwargs)
180184
return self.execute(request) # pylint: disable=no-member
181185

182186
def readwrite_registers(self, *args, **kwargs):

pymodbus/other_message.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# pylint: disable=missing-type-doc
66
import struct
77

8-
from pymodbus.constants import ModbusStatus
8+
from pymodbus.constants import ModbusStatus, Defaults
99
from pymodbus.device import DeviceInformationFactory, ModbusControlBlock
1010
from pymodbus.pdu import ModbusRequest, ModbusResponse
1111

@@ -360,9 +360,13 @@ class ReportSlaveIdRequest(ModbusRequest):
360360
function_code = 0x11
361361
_rtu_frame_size = 4
362362

363-
def __init__(self, **kwargs):
364-
"""Initialize a new instance."""
365-
ModbusRequest.__init__(self, **kwargs)
363+
def __init__(self, unit=Defaults.UnitId, **kwargs):
364+
"""Initialize a new instance.
365+
366+
:param unit: Modbus slave unit ID
367+
368+
"""
369+
ModbusRequest.__init__(self, unit, **kwargs)
366370

367371
def encode(self):
368372
"""Encode the message."""

pymodbus/pdu.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ModbusPDU:
3030
This is a constant set at 0 to indicate Modbus. It is
3131
put here for ease of expansion.
3232
33-
.. attribute:: unit_id
33+
.. attribute:: unit
3434
3535
This is used to route the request to the correct child. In
3636
the TCP modbus, it is used for routing (or not used at all. However,
@@ -51,11 +51,15 @@ class ModbusPDU:
5151
of encoding it again.
5252
"""
5353

54-
def __init__(self, **kwargs):
55-
"""Initialize the base data for a modbus request."""
54+
def __init__(self, unit=Defaults.UnitId, **kwargs):
55+
"""Initialize the base data for a modbus request.
56+
57+
:param unit: Modbus slave unit ID
58+
59+
"""
5660
self.transaction_id = kwargs.get("transaction", Defaults.TransactionId)
5761
self.protocol_id = kwargs.get("protocol", Defaults.ProtocolId)
58-
self.unit_id = kwargs.get("unit", Defaults.UnitId)
62+
self.unit_id = unit
5963
self.skip_encode = kwargs.get("skip_encode", False)
6064
self.check = 0x0000
6165

@@ -94,9 +98,12 @@ def calculateRtuFrameSize(cls, buffer): # pylint: disable=invalid-name
9498
class ModbusRequest(ModbusPDU):
9599
"""Base class for a modbus request PDU."""
96100

97-
def __init__(self, **kwargs):
98-
"""Proxy to the lower level initializer."""
99-
ModbusPDU.__init__(self, **kwargs)
101+
def __init__(self, unit=Defaults.UnitId, **kwargs):
102+
"""Proxy to the lower level initializer.
103+
104+
:param unit: Modbus slave unit ID
105+
"""
106+
super().__init__(unit, **kwargs)
100107

101108
def doException(self, exception): # pylint: disable=invalid-name
102109
"""Build an error response based on the function.
@@ -127,9 +134,13 @@ class ModbusResponse(ModbusPDU):
127134

128135
should_respond = True
129136

130-
def __init__(self, **kwargs):
131-
"""Proxy the lower level initializer."""
132-
ModbusPDU.__init__(self, **kwargs)
137+
def __init__(self, unit=Defaults.UnitId, **kwargs):
138+
"""Proxy the lower level initializer.
139+
140+
:param unit: Modbus slave unit ID
141+
142+
"""
143+
super().__init__(unit, **kwargs)
133144

134145
def isError(self): # pylint: disable=invalid-name
135146
"""Check if the error is a success or failure."""
@@ -178,7 +189,7 @@ def __init__(self, function_code, exception_code=None, **kwargs):
178189
:param function_code: The function to build an exception response for
179190
:param exception_code: The specific modbus exception to return
180191
"""
181-
ModbusResponse.__init__(self, **kwargs)
192+
super().__init__(**kwargs)
182193
self.original_code = function_code
183194
self.function_code = function_code | self.ExceptionOffset
184195
self.exception_code = exception_code
@@ -226,7 +237,7 @@ def __init__(self, function_code, **kwargs):
226237
227238
:param function_code: The function we are erroring on
228239
"""
229-
ModbusRequest.__init__(self, **kwargs)
240+
super().__init__(**kwargs)
230241
self.function_code = function_code
231242

232243
def decode(self, data):

pymodbus/register_read_message.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=missing-type-doc
33
import struct
44

5+
from pymodbus.constants import Defaults
56
from pymodbus.pdu import ModbusExceptions as merror, ModbusRequest, ModbusResponse
67

78

@@ -10,13 +11,14 @@ class ReadRegistersRequestBase(ModbusRequest):
1011

1112
_rtu_frame_size = 8
1213

13-
def __init__(self, address, count, **kwargs):
14+
def __init__(self, address, count, unit=Defaults.UnitId, **kwargs):
1415
"""Initialize a new instance.
1516
1617
:param address: The address to start the read from
1718
:param count: The number of registers to read
19+
:param unit: Modbus slave unit ID
1820
"""
19-
ModbusRequest.__init__(self, **kwargs)
21+
super().__init__(unit, **kwargs)
2022
self.address = address
2123
self.count = count
2224

@@ -57,12 +59,13 @@ class ReadRegistersResponseBase(ModbusResponse):
5759

5860
_rtu_byte_count_pos = 2
5961

60-
def __init__(self, values, **kwargs):
62+
def __init__(self, values, unit=Defaults.UnitId, **kwargs):
6163
"""Initialize a new instance.
6264
6365
:param values: The values to write to
66+
:param unit: Modbus slave unit ID
6467
"""
65-
ModbusResponse.__init__(self, **kwargs)
68+
super().__init__(unit, **kwargs)
6669

6770
#: A list of register values
6871
self.registers = values or []
@@ -115,13 +118,14 @@ class ReadHoldingRegistersRequest(ReadRegistersRequestBase):
115118

116119
function_code = 3
117120

118-
def __init__(self, address=None, count=None, **kwargs):
121+
def __init__(self, address=None, count=None, unit=Defaults.UnitId, **kwargs):
119122
"""Initialize a new instance of the request.
120123
121124
:param address: The starting address to read from
122125
:param count: The number of registers to read from address
126+
:param unit: Modbus slave unit ID
123127
"""
124-
ReadRegistersRequestBase.__init__(self, address, count, **kwargs)
128+
super().__init__(address, count, unit, **kwargs)
125129

126130
def execute(self, context):
127131
"""Run a read holding request against a datastore.
@@ -156,7 +160,7 @@ def __init__(self, values=None, **kwargs):
156160
157161
:param values: The resulting register values
158162
"""
159-
ReadRegistersResponseBase.__init__(self, values, **kwargs)
163+
super().__init__(values, **kwargs)
160164

161165

162166
class ReadInputRegistersRequest(ReadRegistersRequestBase):
@@ -171,13 +175,14 @@ class ReadInputRegistersRequest(ReadRegistersRequestBase):
171175

172176
function_code = 4
173177

174-
def __init__(self, address=None, count=None, **kwargs):
178+
def __init__(self, address=None, count=None, unit=Defaults.UnitId, **kwargs):
175179
"""Initialize a new instance of the request.
176180
177181
:param address: The starting address to read from
178182
:param count: The number of registers to read from address
183+
:param unit: Modbus slave unit ID
179184
"""
180-
ReadRegistersRequestBase.__init__(self, address, count, **kwargs)
185+
super().__init__(address, count, unit, **kwargs)
181186

182187
def execute(self, context):
183188
"""Run a read input request against a datastore.
@@ -212,7 +217,7 @@ def __init__(self, values=None, **kwargs):
212217
213218
:param values: The resulting register values
214219
"""
215-
ReadRegistersResponseBase.__init__(self, values, **kwargs)
220+
super().__init__(values, **kwargs)
216221

217222

218223
class ReadWriteMultipleRegistersRequest(ModbusRequest):
@@ -242,7 +247,7 @@ def __init__(self, **kwargs):
242247
:param write_address: The address to start writing to
243248
:param write_registers: The registers to write to the specified address
244249
"""
245-
ModbusRequest.__init__(self, **kwargs)
250+
super().__init__(**kwargs)
246251
self.read_address = kwargs.get("read_address", 0x00)
247252
self.read_count = kwargs.get("read_count", 0)
248253
self.write_address = kwargs.get("write_address", 0x00)
@@ -353,7 +358,7 @@ def __init__(self, values=None, **kwargs):
353358
354359
:param values: The register values to write
355360
"""
356-
ModbusResponse.__init__(self, **kwargs)
361+
super().__init__(**kwargs)
357362
self.registers = values or []
358363

359364
def encode(self):

0 commit comments

Comments
 (0)