Skip to content

Commit 63ed627

Browse files
committed
pymodbus update compatability
1 parent bc3e9d9 commit 63ed627

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

classes/transports/modbus_rtu.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class modbus_rtu(modbus_base):
1919
baudrate : int = 9600
2020
client : ModbusSerialClient
2121

22+
pymodbus_address_arg = 'unit'
23+
2224
def __init__(self, settings : SectionProxy, protocolSettings : protocol_settings = None):
2325
#logger = logging.getLogger(__name__)
2426
#logging.basicConfig(level=logging.DEBUG)
@@ -41,6 +43,11 @@ def __init__(self, settings : SectionProxy, protocolSettings : protocol_settings
4143
address : int = settings.getint("address", 0)
4244
self.addresses = [address]
4345

46+
# pymodbus compatability; unit was renamed to address
47+
if 'address' in inspect.signature(ModbusSerialClient.read_holding_registers).parameters:
48+
self.pymodbus_address_arg = 'address'
49+
50+
4451
# Get the signature of the __init__ method
4552
init_signature = inspect.signature(ModbusSerialClient.__init__)
4653

@@ -60,6 +67,10 @@ def read_registers(self, start, count=1, registry_type : Registry_Type = Registr
6067
if 'unit' not in kwargs:
6168
kwargs = {'unit': int(self.addresses[0]), **kwargs}
6269

70+
#compatability
71+
if self.pymodbus_address_arg != 'unit':
72+
kwargs['address'] = kwargs.pop('unit')
73+
6374
if registry_type == Registry_Type.INPUT:
6475
return self.client.read_input_registers(start, count, **kwargs)
6576
elif registry_type == Registry_Type.HOLDING:
@@ -72,6 +83,10 @@ def write_register(self, register : int, value : int, **kwargs):
7283
if 'unit' not in kwargs:
7384
kwargs = {'unit': self.addresses[0], **kwargs}
7485

86+
#compatability
87+
if self.pymodbus_address_arg != 'unit':
88+
kwargs['address'] = kwargs.pop('unit')
89+
7590
self.client.write_register(register, value, **kwargs) #function code 0x06 writes to holding register
7691

7792
def connect(self):

0 commit comments

Comments
 (0)