2222
2323import time
2424import uuid
25+ import warnings
2526from sunspec2 import mdef , device , mb
2627import 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-
312312class 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):
351391class 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