|
7 | 7 | # TODO: Once SungrowModbusTcpClient 0.1.7 is released,
|
8 | 8 | # we can remove the "<3.0.0" pymodbus restriction and this
|
9 | 9 | # will make sense again.
|
10 |
| - from pymodbus.client import ModbusTcpClient |
11 |
| - from pymodbus.transaction import ModbusSocketFramer |
| 10 | + from pymodbus.client import ModbusTcpClient, ModbusUdpClient, ModbusTlsClient |
| 11 | + from pymodbus.transaction import ModbusAsciiFramer, ModbusBinaryFramer, ModbusRtuFramer, ModbusSocketFramer |
12 | 12 | except ImportError:
|
13 | 13 | # Pymodbus < 3.0
|
14 |
| - from pymodbus.client.sync import ModbusTcpClient, ModbusSocketFramer |
| 14 | + from pymodbus.client.sync import ModbusTcpClient, ModbusUdpClient, ModbusTlsClient, \ |
| 15 | + ModbusAsciiFramer, ModbusBinaryFramer, ModbusRtuFramer, ModbusSocketFramer |
15 | 16 | from SungrowModbusTcpClient import SungrowModbusTcpClient
|
16 | 17 |
|
17 | 18 | DEFAULT_SCAN_RATE_S = 5
|
@@ -55,17 +56,41 @@ def __init__(self, ip, port=502, update_rate_s=DEFAULT_SCAN_RATE_S, variant=None
|
55 | 56 |
|
56 | 57 | def connect(self):
|
57 | 58 | # Connects to the modbus device
|
58 |
| - if self._variant == 'sungrow': |
59 |
| - # Some later versions of the sungrow inverter firmware encrypts the payloads of |
60 |
| - # the modbus traffic. https://github.com/rpvelloso/Sungrow-Modbus is a drop-in |
61 |
| - # replacement for ModbusTcpClient that manages decrypting the traffic for us. |
62 |
| - self._mb = SungrowModbusTcpClient.SungrowModbusTcpClient(host=self._ip, port=self._port, |
63 |
| - framer=ModbusSocketFramer, timeout=1, |
64 |
| - RetryOnEmpty=True, retries=1) |
| 59 | + clients = { |
| 60 | + "tcp": ModbusTcpClient, |
| 61 | + "tls": ModbusTlsClient, |
| 62 | + "udp": ModbusUdpClient, |
| 63 | + "sungrow": SungrowModbusTcpClient.SungrowModbusTcpClient, |
| 64 | + # if 'serial' modbus is required at some point, the configuration |
| 65 | + # needs to be changed to provide file, baudrate etc. |
| 66 | + # "serial": (ModbusSerialClient, ModbusRtuFramer), |
| 67 | + } |
| 68 | + framers = { |
| 69 | + "ascii": ModbusAsciiFramer, |
| 70 | + "binary": ModbusBinaryFramer, |
| 71 | + "rtu": ModbusRtuFramer, |
| 72 | + "socket": ModbusSocketFramer, |
| 73 | + } |
| 74 | + |
| 75 | + if self._variant is None: |
| 76 | + desired_framer, desired_client = None, 'tcp' |
| 77 | + elif "-over-" in self._variant: |
| 78 | + desired_framer, desired_client = self._variant.split('-over-') |
65 | 79 | else:
|
66 |
| - self._mb = ModbusTcpClient(self._ip, self._port, |
67 |
| - framer=ModbusSocketFramer, timeout=1, |
68 |
| - RetryOnEmpty=True, retries=1) |
| 80 | + desired_framer, desired_client = None, self._variant |
| 81 | + |
| 82 | + if desired_client not in clients: |
| 83 | + raise ValueError("Unknown modbus client: {}".format(desired_client)) |
| 84 | + if desired_framer is not None and desired_framer not in framers: |
| 85 | + raise ValueError("Unknown modbus framer: {}".format(desired_framer)) |
| 86 | + |
| 87 | + client = clients[desired_client] |
| 88 | + if desired_framer is None: |
| 89 | + framer = ModbusSocketFramer |
| 90 | + else: |
| 91 | + framer = framers[desired_framer] |
| 92 | + |
| 93 | + self._mb = client(self._ip, self._port, RetryOnEmpty=True, framer=framer, retries=1, timeout=1) |
69 | 94 |
|
70 | 95 | def add_monitor_register(self, table, addr, type='uint16'):
|
71 | 96 | # Accepts a modbus register and table to monitor
|
|
0 commit comments