From 0ec7ca0d73983f80370329942a3b89b6eec3a30b Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Tue, 20 Apr 2021 10:41:04 +0200 Subject: [PATCH] Timeout fixed in Master, added Slave. --- controlpi_plugins/modbus.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/controlpi_plugins/modbus.py b/controlpi_plugins/modbus.py index faaa3d9..042e7eb 100644 --- a/controlpi_plugins/modbus.py +++ b/controlpi_plugins/modbus.py @@ -368,11 +368,7 @@ class ModbusMaster(BasePlugin): 'maximum': 2, 'default': 1}, 'retries': {'type': 'integer', - 'default': 2}, - 'response timeout': {'type': 'number', - 'default': 0.2}, - 'turnaround delay': {'type': 'number', - 'default': 0.1}}, + 'default': 2}}, 'required': ['device']} def process_conf(self) -> None: @@ -386,10 +382,6 @@ class ModbusMaster(BasePlugin): self.conf['stopbits'] = 1 if 'retries' not in self.conf: self.conf['retries'] = 2 - if 'response timeout' not in self.conf: - self.conf['response timeout'] = 0.2 - if 'turnaround delay' not in self.conf: - self.conf['turnaround delay'] = 0.1 # Constants from serial for parity and stop bits: self._parity = serial.PARITY_EVEN if self.conf['parity'] == 'odd': @@ -618,7 +610,7 @@ class ModbusMaster(BasePlugin): writer.write(request) if slave == 0: # Broadcast => just wait for delay and finish: - await asyncio.sleep(self.conf['turnaround delay']) + await asyncio.sleep(0.1) break # Read response: response = b'' @@ -628,11 +620,11 @@ class ModbusMaster(BasePlugin): while True: try: response += await asyncio.wait_for( - reader.read(1), self.conf['response timeout']) + reader.read(1), 0.2) crc.update(response[length]) + length += 1 except asyncio.TimeoutError: break - length += 1 # Update expected length based on function: if (length == 2 and (response[1] == 0x05 or response[1] == 0x06 or @@ -798,9 +790,13 @@ class ModbusSlave(BasePlugin): expected_length = 8 length = 0 while True: - request += await reader.read(1) - crc.update(request[length]) - length += 1 + try: + request += await asyncio.wait_for( + reader.read(1), 0.2) + crc.update(request[length]) + length += 1 + except asyncio.TimeoutError: + break # Update expected length based on function: if length == 2: if request[1] == 0x0F or request[1] == 0x10: -- 2.34.1