Timeout fixed in Master, added Slave.
authorBenjamin Braatz <bb@bbraatz.eu>
Tue, 20 Apr 2021 08:41:04 +0000 (10:41 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Tue, 20 Apr 2021 08:41:04 +0000 (10:41 +0200)
controlpi_plugins/modbus.py

index faaa3d9c17e094ed798687750bf817f8e1263c53..042e7ebad5f5b2e16a382cf0043f10c82bdeb59f 100644 (file)
@@ -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: