Set character timeout to 2 instead of 1.5 chars.
authorBenjamin Braatz <bb@bbraatz.eu>
Fri, 9 Apr 2021 08:57:46 +0000 (10:57 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Fri, 9 Apr 2021 09:07:23 +0000 (11:07 +0200)
controlpi_plugins/modbus.py

index 3ea6c4ae01a2452658a59787cddcd473bc8970fd..75b9482504370b25895d2216fb2c6857ff68d444 100644 (file)
@@ -401,7 +401,7 @@ class ModbusMaster(BasePlugin):
             self._stopbits = serial.STOPBITS_TWO
         # 1.5 char and 3.5 char times for protocol timing
         # (according to Modbus specification):
-        self._t15 = 0.000750
+        self._t15 = 0.001000
         self._t35 = 0.001750
         if self.conf['baudrate'] <= 19200:
             bit_per_char = 9
@@ -409,7 +409,7 @@ class ModbusMaster(BasePlugin):
                 bit_per_char += 1
             bit_per_char += self.conf['stopbits']
             seconds_per_char = bit_per_char / self.conf['baudrate']
-            self._t15 = 1.5 * seconds_per_char
+            self._t15 = 2.0 * seconds_per_char
             self._t35 = 3.5 * seconds_per_char
         # Queue for Modbus messages to be sent:
         self._queue: asyncio.Queue = asyncio.Queue()
@@ -621,7 +621,6 @@ class ModbusMaster(BasePlugin):
                 t35_task: asyncio.Task = asyncio.create_task(
                     asyncio.sleep(self._t35))
                 try:
-                    # First byte is awaited for response timeout:
                     first_byte = await asyncio.wait_for(
                         reader.read(1), self.conf['response timeout'])
                     modbus_response += first_byte
@@ -636,8 +635,6 @@ class ModbusMaster(BasePlugin):
                 while True:
                     t35_task = asyncio.create_task(asyncio.sleep(self._t35))
                     try:
-                        # Subsequent bytes are only awaited for the time
-                        # needed to transmit 1.5 characters:
                         next_byte = await asyncio.wait_for(
                                 reader.read(1), self._t15)
                         modbus_response += next_byte
@@ -713,7 +710,7 @@ class ModbusSlave(BasePlugin):
             self._stopbits = serial.STOPBITS_TWO
         # 1.5 char and 3.5 char times for protocol timing
         # (according to Modbus specification):
-        self._t15 = 0.000750
+        self._t15 = 0.001000
         self._t35 = 0.001750
         if self._baudrate <= 19200:
             bit_per_char = 11
@@ -721,7 +718,7 @@ class ModbusSlave(BasePlugin):
                     self._stopbits == serial.STOPBITS_ONE):
                 bit_per_char = 10
             seconds_per_char = bit_per_char / self._baudrate
-            self._t15 = 1.5 * seconds_per_char
+            self._t15 = 2.0 * seconds_per_char
             self._t35 = 3.5 * seconds_per_char
         # Coils and registers:
         self._coils: Dict[int, bool] = {}