Skip to content
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

Call on_reconnect_callback. #1959

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ def __init__( # pylint: disable=too-many-arguments
parity=kwargs.get("parity", None),
stopbits=kwargs.get("stopbits", None),
handle_local_echo=kwargs.get("handle_local_echo", False),
on_reconnect_callback = on_reconnect_callback,
),
False,
)
Expand All @@ -355,7 +356,6 @@ def __init__( # pylint: disable=too-many-arguments
self.params.close_comm_on_error = bool(close_comm_on_error)
self.params.strict = bool(strict)
self.params.broadcast_enable = bool(broadcast_enable)
self.on_reconnect_callback = on_reconnect_callback
self.retry_on_empty: int = 0
self.no_resend_on_retry = no_resend_on_retry
self.slaves: list[int] = []
Expand Down
3 changes: 3 additions & 0 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CommParams:
port: int = 0
source_address: tuple[str, int] | None = None
handle_local_echo: bool = False
on_reconnect_callback: Callable[[], None] | None = None

# tls
sslctx: ssl.SSLContext | None = None
Expand Down Expand Up @@ -467,6 +468,8 @@ async def do_reconnect(self) -> None:
self.reconnect_delay_current * 1000,
)
await asyncio.sleep(self.reconnect_delay_current)
if self.comm_params.on_reconnect_callback:
self.comm_params.on_reconnect_callback()
if await self.transport_connect():
break
self.reconnect_delay_current = min(
Expand Down