Skip to content

Commit f9ce125

Browse files
committed
Use Unit ID, with deprecation compatibility for old name
1 parent d42ed2d commit f9ce125

File tree

9 files changed

+158
-94
lines changed

9 files changed

+158
-94
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ __pycache__/
88
*.egg-info/
99
build/
1010

11-
dist/
11+
dist/
12+
13+
*.code-workspace

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.testing.pytestArgs": [
3+
"sunspec2/tests"
4+
],
5+
"python.testing.unittestEnabled": false,
6+
"python.testing.pytestEnabled": true
7+
}

README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,20 @@ type.
165165

166166
TCP
167167
^^^
168-
The following is how to open and initialize a TCP Device, where the slave ID is set to 1, the IP address of the TCP
168+
The following is how to open and initialize a TCP Device, where the Unit ID is set to 1, the IP address of the TCP
169169
device is
170170
127.0.0.1, and the port is 8502::
171171

172172
>>> import sunspec2.modbus.client as client
173-
>>> d = client.SunSpecModbusClientDeviceTCP(slave_id=1, ipaddr='127.0.0.1', ipport=8502)
173+
>>> d = client.SunSpecModbusClientDeviceTCP(unit_id=1, ipaddr='127.0.0.1', ipport=8502)
174174

175175
RTU
176176
^^^
177-
The following to open and initialize a RTU Device, where the slave ID is set to 1, and the name of the serial port is
177+
The following to open and initialize a RTU Device, where the Unit ID is set to 1, and the name of the serial port is
178178
COM2::
179179

180180
>>> import sunspec2.modbus.client as client
181-
>>> d = client.SunSpecModbusClientDeviceRTU(slave_id=1, name="COM2")
181+
>>> d = client.SunSpecModbusClientDeviceRTU(unit_id=1, name="COM2")
182182

183183
Device Image
184184
^^^^^^^^^^^^
@@ -209,7 +209,7 @@ model ID. The first key is the model ID as an int, the second key is the model n
209209
that a device may contain more than one model with the same model ID, the dictionary keys refer to a list of model
210210
objects with that ID. Both keys refer to the same model list for a model ID.
211211

212-
>>> d = client.SunSpecModbusClientDeviceTCP(slave_id=1, ipaddr='127.0.0.1', ipport=8502)
212+
>>> d = client.SunSpecModbusClientDeviceTCP(unit_id=1, ipaddr='127.0.0.1', ipport=8502)
213213
>>> d.scan()
214214

215215

@@ -296,7 +296,7 @@ This section will go over the full steps on how to set a volt-var curve.
296296

297297
Initialize device, and run device discovery with scan(): ::
298298

299-
>>> d = client.SunSpecModbusClientDeviceRTU(slave_id=1, name="COM2")
299+
>>> d = client.SunSpecModbusClientDeviceRTU(unit_id=1, name="COM2")
300300
>>> d.scan()
301301

302302
Confirm that model 705 (DERVoltVar) is on the device: ::

scripts/suns.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
-o: output mode for data (text, xml)
1919
-x: export model description (slang, xml)
2020
-t: transport type: tcp or rtu (default: tcp)
21-
-a: modbus slave address (default: 1)
21+
-a: modbus unit identifier (default: 1)
2222
-i: ip address to use for modbus tcp (default: localhost)
2323
-P: port number for modbus tcp (default: 502)
2424
-p: serial port for modbus rtu (default: /dev/ttyUSB0)
@@ -46,7 +46,7 @@
4646
help='transport type: rtu, tcp, file [default: tcp]')
4747
parser.add_option('-a', metavar=' ', type='int',
4848
default=1,
49-
help='modbus slave address [default: 1]')
49+
help='modbus unit identifier [default: 1]')
5050
parser.add_option('-i', metavar=' ',
5151
default='localhost',
5252
help='ip address to use for modbus tcp [default: localhost]')
@@ -72,10 +72,10 @@
7272

7373
try:
7474
if options.t == 'tcp':
75-
sd = client.SunSpecModbusClientDeviceTCP(slave_id=options.a, ipaddr=options.i, ipport=options.P,
75+
sd = client.SunSpecModbusClientDeviceTCP(unit_id=options.a, ipaddr=options.i, ipport=options.P,
7676
timeout=options.T)
7777
elif options.t == 'rtu':
78-
sd = client.SunSpecModbusClientDeviceRTU(slave_id=options.a, name=options.p, baudrate=options.b,
78+
sd = client.SunSpecModbusClientDeviceRTU(unit_id=options.a, name=options.p, baudrate=options.b,
7979
parity=options.R, timeout=options.T)
8080
elif options.t == 'file':
8181
sd = file_client.FileClientDevice(filename=options.m)

sunspec2/docs/pysunspec.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,20 @@ type.
146146

147147
TCP
148148
^^^
149-
The following is how to open and initialize a TCP Device, where the slave ID is set to 1, the IP address of the TCP
149+
The following is how to open and initialize a TCP Device, where the Unit ID is set to 1, the IP address of the TCP
150150
device is
151151
127.0.0.1, and the port is 8502::
152152

153153
>>> import sunspec2.modbus.client as client
154-
>>> d = client.SunSpecModbusClientDeviceTCP(slave_id=1, ipaddr='127.0.0.1', ipport=8502)
154+
>>> d = client.SunSpecModbusClientDeviceTCP(unit_id=1, ipaddr='127.0.0.1', ipport=8502)
155155

156156
RTU
157157
^^^
158-
The following to open and initialize a RTU Device, where the slave ID is set to 1, and the name of the serial port is
158+
The following to open and initialize a RTU Device, where the Unit ID is set to 1, and the name of the serial port is
159159
COM2::
160160

161161
>>> import sunspec2.modbus.client as client
162-
>>> d = client.SunSpecModbusClientDeviceRTU(slave_id=1, name="COM2")
162+
>>> d = client.SunSpecModbusClientDeviceRTU(unit_id=1, name="COM2")
163163

164164
Device Image
165165
^^^^^^^^^^^^
@@ -190,7 +190,7 @@ model ID. The first key is the model ID as an int, the second key is the model n
190190
that a device may contain more than one model with the same model ID, the dictionary keys refer to a list of model
191191
objects with that ID. Both keys refer to the same model list for a model ID.
192192

193-
>>> d = client.SunSpecModbusClientDeviceTCP(slave_id=1, ipaddr='127.0.0.1', ipport=8502)
193+
>>> d = client.SunSpecModbusClientDeviceTCP(unit_id=1, ipaddr='127.0.0.1', ipport=8502)
194194
>>> d.scan()
195195

196196

@@ -277,7 +277,7 @@ This section will go over the full steps on how to set a volt-var curve.
277277

278278
Initialize device, and run device discovery with scan(): ::
279279

280-
>>> d = client.SunSpecModbusClientDeviceRTU(slave_id=1, name="COM2")
280+
>>> d = client.SunSpecModbusClientDeviceRTU(unit_id=1, name="COM2")
281281
>>> d.scan()
282282

283283
Confirm that model 705 (DERVoltVar) is on the device: ::

sunspec2/modbus/client.py

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import time
2424
import uuid
25+
import warnings
2526
from sunspec2 import mdef, device, mb
2627
import sunspec2.modbus.modbus as modbus_client
2728

@@ -308,13 +309,52 @@ def scan(self, progress=None, delay=None, connect=True, full_model_read=True):
308309
if connected:
309310
self.disconnect()
310311

311-
312312
class SunSpecModbusClientDeviceTCP(SunSpecModbusClientDevice):
313-
def __init__(self, slave_id=1, ipaddr='127.0.0.1', ipport=502, timeout=None, ctx=None, trace_func=None,
313+
"""Provides access to a Modbus RTU device.
314+
Parameters:
315+
unit_id :
316+
Modbus Unit Identifier.
317+
ipaddr :
318+
IP address of the Modbus TCP device.
319+
ipport :
320+
Port number for Modbus TCP. Default is 502 if not specified.
321+
timeout :
322+
Modbus request timeout in seconds. Fractional seconds are permitted
323+
such as .5.
324+
ctx :
325+
Context variable to be used by the object creator. Not used by the
326+
modbus module.
327+
trace_func :
328+
Trace function to use for detailed logging. No detailed logging is
329+
perform is a trace function is not supplied.
330+
max_count :
331+
Maximum register count for a single Modbus request.
332+
max_write_count :
333+
Maximum register count for a single Modbus write request.
334+
model_class :
335+
Model class to use for creating models in the device. Default is
336+
:class:`sunspec2.modbus.client.SunSpecModbusClientModel`.
337+
slave_id : [DEPRECATED] Use unit_id instead.
338+
Raises:
339+
SunSpecModbusClientError: Raised for any general modbus client error.
340+
SunSpecModbusClientTimeoutError: Raised for a modbus client request timeout.
341+
SunSpecModbusClientException: Raised for an exception response to a modbus
342+
client request.
343+
"""
344+
345+
def __init__(self, unit_id=1, ipaddr='127.0.0.1', ipport=502, timeout=None, ctx=None, trace_func=None,
314346
max_count=modbus_client.REQ_COUNT_MAX, max_write_count=modbus_client.REQ_WRITE_COUNT_MAX,
315-
model_class=SunSpecModbusClientModel):
347+
model_class=SunSpecModbusClientModel, slave_id=None):
316348
SunSpecModbusClientDevice.__init__(self, model_class=model_class)
317-
self.slave_id = slave_id
349+
if unit_id == 1 and slave_id is not None:
350+
unit_id = slave_id
351+
if slave_id is not None:
352+
warnings.warn(
353+
"The 'slave_id' parameter is deprecated and will be removed in a future version. Use 'unit_id' instead.",
354+
DeprecationWarning,
355+
stacklevel=2
356+
)
357+
self.unit_id = unit_id
318358
self.ipaddr = ipaddr
319359
self.ipport = ipport
320360
self.timeout = timeout
@@ -324,7 +364,7 @@ def __init__(self, slave_id=1, ipaddr='127.0.0.1', ipport=502, timeout=None, ctx
324364
self.max_count = max_count
325365
self.max_write_count = max_write_count
326366

327-
self.client = modbus_client.ModbusClientTCP(slave_id=slave_id, ipaddr=ipaddr, ipport=ipport, timeout=timeout,
367+
self.client = modbus_client.ModbusClientTCP(unit_id=unit_id, ipaddr=ipaddr, ipport=ipport, timeout=timeout,
328368
ctx=ctx, trace_func=trace_func,
329369
max_count=modbus_client.REQ_COUNT_MAX,
330370
max_write_count=modbus_client.REQ_WRITE_COUNT_MAX)
@@ -351,8 +391,8 @@ def write(self, addr, data):
351391
class SunSpecModbusClientDeviceRTU(SunSpecModbusClientDevice):
352392
"""Provides access to a Modbus RTU device.
353393
Parameters:
354-
slave_id :
355-
Modbus slave id.
394+
unit_id :
395+
Modbus Unit Identifier.
356396
name :
357397
Name of the serial port such as 'com4' or '/dev/ttyUSB0'.
358398
baudrate :
@@ -373,19 +413,34 @@ class SunSpecModbusClientDeviceRTU(SunSpecModbusClientDevice):
373413
perform is a trace function is not supplied.
374414
max_count :
375415
Maximum register count for a single Modbus request.
416+
slave_id : [DEPRECATED] Use unit_id instead.
376417
Raises:
377418
SunSpecModbusClientError: Raised for any general modbus client error.
378419
SunSpecModbusClientTimeoutError: Raised for a modbus client request timeout.
379420
SunSpecModbusClientException: Raised for an exception response to a modbus
380421
client request.
381422
"""
382423

383-
def __init__(self, slave_id, name, baudrate=None, parity=None, timeout=None, ctx=None, trace_func=None,
424+
def __init__(self, unit_id=None, name=None, baudrate=None, parity=None, timeout=None, ctx=None, trace_func=None,
384425
max_count=modbus_client.REQ_COUNT_MAX, max_write_count=modbus_client.REQ_WRITE_COUNT_MAX,
385-
model_class=SunSpecModbusClientModel):
426+
model_class=SunSpecModbusClientModel, slave_id=None):
386427
# test if this super class init is needed
387428
SunSpecModbusClientDevice.__init__(self, model_class=model_class)
388-
self.slave_id = slave_id
429+
# Backward compatibility for slave_id
430+
if unit_id is not None:
431+
self.unit_id = unit_id
432+
elif slave_id is not None:
433+
self.unit_id = slave_id
434+
else:
435+
raise ValueError("unit_id must be provided")
436+
if name is None:
437+
raise ValueError("name must be provided")
438+
if slave_id is not None:
439+
warnings.warn(
440+
"The 'slave_id' parameter is deprecated and will be removed in a future version. Use 'unit_id' instead.",
441+
DeprecationWarning,
442+
stacklevel=2
443+
)
389444
self.name = name
390445
self.client = None
391446
self.ctx = ctx
@@ -396,7 +451,7 @@ def __init__(self, slave_id, name, baudrate=None, parity=None, timeout=None, ctx
396451
self.client = modbus_client.modbus_rtu_client(name, baudrate, parity, timeout)
397452
if self.client is None:
398453
raise SunSpecModbusClientError('No modbus rtu client set for device')
399-
self.client.add_device(self.slave_id, self)
454+
self.client.add_device(self.unit_id, self)
400455

401456
def open(self):
402457
self.client.open()
@@ -406,7 +461,7 @@ def close(self):
406461
"""
407462

408463
if self.client:
409-
self.client.remove_device(self.slave_id)
464+
self.client.remove_device(self.unit_id)
410465

411466
def read(self, addr, count, op=modbus_client.FUNC_READ_HOLDING):
412467
"""Read Modbus device registers.
@@ -421,7 +476,7 @@ def read(self, addr, count, op=modbus_client.FUNC_READ_HOLDING):
421476
Byte string containing register contents.
422477
"""
423478

424-
return self.client.read(self.slave_id, addr, count, op=op, max_count=self.max_count)
479+
return self.client.read(self.unit_id, addr, count, op=op, max_count=self.max_count)
425480

426481
def write(self, addr, data):
427482
"""Write Modbus device registers.
@@ -432,4 +487,4 @@ def write(self, addr, data):
432487
Byte string containing register contents.
433488
"""
434489

435-
return self.client.write(self.slave_id, addr, data, max_write_count=self.max_write_count)
490+
return self.client.write(self.unit_id, addr, data, max_write_count=self.max_write_count)

0 commit comments

Comments
 (0)