Skip to content

Commit 44d2c88

Browse files
committed
fix multi transport support on one devceice
1 parent 920b33d commit 44d2c88

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

classes/transports/modbus_base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@
1212
from typing import TYPE_CHECKING
1313
if TYPE_CHECKING:
1414
from configparser import SectionProxy
15+
try:
16+
from pymodbus.client.sync import BaseModbusClient
17+
except ImportError:
18+
from pymodbus.client import BaseModbusClient
19+
1520

1621
class modbus_base(transport_base):
1722

23+
24+
#this is specifically static
25+
clients : dict[str, BaseModbusClient] = []
26+
''' str is identifier, dict of clients when multiple transports use the same ports '''
27+
28+
#non-static here for reference, type hinting, python bs ect...
1829
modbus_delay_increament : float = 0.05
1930
''' delay adjustment every error. todo: add a setting for this '''
2031

classes/transports/modbus_rtu.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def __init__(self, settings : SectionProxy, protocolSettings : protocol_settings
5454
# Get the signature of the __init__ method
5555
init_signature = inspect.signature(ModbusSerialClient.__init__)
5656

57+
client_str = self.port+"("+self.baudrate+")"
58+
59+
if client_str in modbus_base.clients:
60+
self.client = modbus_base.clients[client_str]
61+
return
62+
5763
if 'method' in init_signature.parameters:
5864
self.client = ModbusSerialClient(method='rtu', port=self.port,
5965
baudrate=int(self.baudrate),
@@ -64,6 +70,9 @@ def __init__(self, settings : SectionProxy, protocolSettings : protocol_settings
6470
baudrate=int(self.baudrate),
6571
stopbits=1, parity='N', bytesize=8, timeout=2
6672
)
73+
74+
#add to clients
75+
self.clients[client_str] = self.client
6776

6877
def read_registers(self, start, count=1, registry_type : Registry_Type = Registry_Type.INPUT, **kwargs):
6978

classes/transports/modbus_tcp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ def __init__(self, settings : SectionProxy, protocolSettings : protocol_settings
3333
if 'slave' in inspect.signature(ModbusTcpClient.read_holding_registers).parameters:
3434
self.pymodbus_slave_arg = 'slave'
3535

36+
client_str = self.host+"("+self.port+")"
37+
#check if client is already initialied
38+
if client_str in modbus_base.clients:
39+
self.client = modbus_base.clients[client_str]
40+
return
41+
3642
self.client = ModbusTcpClient(host=self.host, port=self.port, timeout=7, retries=3)
43+
44+
#add to clients
45+
self.clients[client_str] = self.client
46+
3747
super().__init__(settings, protocolSettings=protocolSettings)
3848

3949
def read_registers(self, start, count=1, registry_type : Registry_Type = Registry_Type.INPUT, **kwargs):

0 commit comments

Comments
 (0)