99except ImportError :
1010 from pymodbus .client import ModbusSerialClient
1111
12+
1213from .modbus_base import modbus_base
1314from configparser import SectionProxy
1415from defs .common import find_usb_serial_port , get_usb_serial_port_info , strtoint
@@ -54,16 +55,26 @@ def __init__(self, settings : SectionProxy, protocolSettings : protocol_settings
5455 # Get the signature of the __init__ method
5556 init_signature = inspect .signature (ModbusSerialClient .__init__ )
5657
58+ client_str = self .port + "(" + str (self .baudrate )+ ")"
59+
60+ if client_str in modbus_base .clients :
61+ self .client = modbus_base .clients [client_str ]
62+ return
63+
5764 if 'method' in init_signature .parameters :
5865 self .client = ModbusSerialClient (method = 'rtu' , port = self .port ,
5966 baudrate = int (self .baudrate ),
6067 stopbits = 1 , parity = 'N' , bytesize = 8 , timeout = 2
6168 )
6269 else :
63- self .client = ModbusSerialClient (port = self .port ,
70+ self .client = ModbusSerialClient (
71+ port = self .port ,
6472 baudrate = int (self .baudrate ),
6573 stopbits = 1 , parity = 'N' , bytesize = 8 , timeout = 2
6674 )
75+
76+ #add to clients
77+ modbus_base .clients [client_str ] = self .client
6778
6879 def read_registers (self , start , count = 1 , registry_type : Registry_Type = Registry_Type .INPUT , ** kwargs ):
6980
@@ -75,9 +86,9 @@ def read_registers(self, start, count=1, registry_type : Registry_Type = Registr
7586 kwargs ['slave' ] = kwargs .pop ('unit' )
7687
7788 if registry_type == Registry_Type .INPUT :
78- return self .client .read_input_registers (start , count , ** kwargs )
89+ return self .client .read_input_registers (address = start , count = count , ** kwargs )
7990 elif registry_type == Registry_Type .HOLDING :
80- return self .client .read_holding_registers (start , count , ** kwargs )
91+ return self .client .read_holding_registers (address = start , count = count , ** kwargs )
8192
8293 def write_register (self , register : int , value : int , ** kwargs ):
8394 if not self .write_enabled :
0 commit comments