-
Notifications
You must be signed in to change notification settings - Fork 50
Fix Modbus connection recovery for both master and slave plugins #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,11 +2,11 @@ | |||||||||
|
|
||||||||||
| import time | ||||||||||
| from typing import Optional | ||||||||||
|
|
||||||||||
| from pymodbus.client import ModbusTcpClient | ||||||||||
| from pymodbus.exceptions import ConnectionException | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class ModbusConnectionManager: | ||||||||||
| class ModbusConnectionManager: # pylint: disable=too-many-instance-attributes | ||||||||||
|
||||||||||
| """Manages Modbus TCP connections with retry logic.""" | ||||||||||
|
|
||||||||||
| def __init__(self, host: str, port: int, timeout_ms: int): | ||||||||||
|
|
@@ -15,11 +15,12 @@ def __init__(self, host: str, port: int, timeout_ms: int): | |||||||||
| self.timeout = timeout_ms / 1000.0 # Convert to seconds | ||||||||||
|
|
||||||||||
| # Retry configuration | ||||||||||
| self.retry_delay_base = 2.0 # initial delay between attempts (seconds) | ||||||||||
| self.retry_delay_max = 30.0 # maximum delay between attempts (seconds) | ||||||||||
| self.retry_delay_base = 2.0 # initial delay between attempts (seconds) | ||||||||||
| self.retry_delay_max = 30.0 # maximum delay between attempts (seconds) | ||||||||||
| self.retry_delay_current = self.retry_delay_base | ||||||||||
|
|
||||||||||
| # Connection state | ||||||||||
| # Connection state - is_connected is the authoritative flag for connection health | ||||||||||
| # It is set to False when any error occurs, forcing reconnection on next cycle | ||||||||||
| self.client: Optional[ModbusTcpClient] = None | ||||||||||
| self.is_connected = False | ||||||||||
|
|
||||||||||
|
|
@@ -42,17 +43,17 @@ def connect_with_retry(self, stop_event=None) -> bool: | |||||||||
| if self.client: | ||||||||||
| try: | ||||||||||
| self.client.close() | ||||||||||
| except: | ||||||||||
| except Exception: | ||||||||||
| pass | ||||||||||
| self.client = ModbusTcpClient( | ||||||||||
| host=self.host, | ||||||||||
| port=self.port, | ||||||||||
| timeout=self.timeout | ||||||||||
| host=self.host, port=self.port, timeout=self.timeout | ||||||||||
|
||||||||||
| host=self.host, port=self.port, timeout=self.timeout | |
| host=self.host, | |
| port=self.port, | |
| timeout=self.timeout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Unnecessary blank line added after imports. The import section already has proper spacing.