|
13 | 13 |
|
14 | 14 | from tcp_modbus_aio.exceptions import (
|
15 | 15 | ModbusCommunicationFailureError,
|
16 |
| - ModbusCommunicationTimeoutError, |
| 16 | + ModbusConcurrencyError, |
17 | 17 | ModbusNotConnectedError,
|
18 | 18 | )
|
19 | 19 | from tcp_modbus_aio.ping import ping_ip
|
@@ -57,7 +57,6 @@ class TCPModbusClient:
|
57 | 57 | KEEPALIVE_MAX_FAILS: ClassVar = 5
|
58 | 58 |
|
59 | 59 | PING_LOOP_PERIOD: ClassVar = 1
|
60 |
| - CONSECUTIVE_TIMEOUTS_TO_RECONNECT: ClassVar = 5 |
61 | 60 | COOLDOWN_BEFORE_RECONNECTING_SEC: ClassVar = 0.01
|
62 | 61 |
|
63 | 62 | def __init__(
|
@@ -90,9 +89,6 @@ def __init__(
|
90 | 89 | self._reader: asyncio.StreamReader | None = None
|
91 | 90 | self._writer: asyncio.StreamWriter | None = None
|
92 | 91 |
|
93 |
| - # Number of current consecutive modbus calls that resulted in a timeout |
94 |
| - self._consecutive_timeouts = 0 |
95 |
| - |
96 | 92 | # Last ping time in seconds from ping loop, or None if the last ping failed
|
97 | 93 | self._last_ping: float | None = None
|
98 | 94 |
|
@@ -192,7 +188,7 @@ async def _get_tcp_connection(
|
192 | 188 | )
|
193 | 189 | if self.logger is not None:
|
194 | 190 | self.logger.warning(f"[{self}][_get_tcp_connection] {msg}")
|
195 |
| - raise ModbusCommunicationTimeoutError(msg) |
| 191 | + raise ModbusCommunicationFailureError(msg) |
196 | 192 | except OSError:
|
197 | 193 | msg = f"Cannot connect to TCP modbus device at {self.host}:{self.port}"
|
198 | 194 | if self.logger is not None:
|
@@ -447,7 +443,7 @@ async def send_modbus_message(
|
447 | 443 | self._comms_lock.acquire(), time_budget_remaining
|
448 | 444 | )
|
449 | 445 | except asyncio.TimeoutError:
|
450 |
| - raise ModbusCommunicationTimeoutError( |
| 446 | + raise ModbusConcurrencyError( |
451 | 447 | f"Failed to acquire lock to send request {msg_str} to modbus device {self.host}"
|
452 | 448 | )
|
453 | 449 | time_budget_remaining -= lock_t()
|
@@ -515,20 +511,7 @@ async def send_modbus_message(
|
515 | 511 | return None
|
516 | 512 |
|
517 | 513 | raise
|
518 |
| - except asyncio.TimeoutError as e: |
519 |
| - self._consecutive_timeouts += 1 |
520 |
| - if self._consecutive_timeouts >= self.CONSECUTIVE_TIMEOUTS_TO_RECONNECT: |
521 |
| - if self.logger is not None: |
522 |
| - self.logger.warning( |
523 |
| - f"[{self}][send_modbus_message] {self._consecutive_timeouts} consecutive timeouts, " |
524 |
| - "clearing connection" |
525 |
| - ) |
526 |
| - self.clear_tcp_connection() |
527 |
| - |
528 |
| - raise ModbusCommunicationTimeoutError( |
529 |
| - f"Request {msg_str} timed out to {self.host}:{self.port}" |
530 |
| - ) from e |
531 |
| - except (OSError, EOFError) as e: |
| 514 | + except (OSError, EOFError, asyncio.TimeoutError) as e: |
532 | 515 | if self.logger is not None:
|
533 | 516 | self.logger.warning(
|
534 | 517 | f"[{self}][send_modbus_message] {type(e).__name__}({e}) while sending request {msg_str}, "
|
|
0 commit comments