From d8cc7bbc3824d0bf105faa72025b9280bc81c48a Mon Sep 17 00:00:00 2001 From: jan iversen Date: Tue, 12 Nov 2024 10:35:21 +0100 Subject: [PATCH 1/2] Correct close for server transport. --- pymodbus/transport/transport.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 6c7c00b3f..07e1f8c0c 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -154,6 +154,7 @@ def __init__( self.call_create: Callable[[], Coroutine[Any, Any, Any]] = None # type: ignore[assignment] self.reconnect_task: asyncio.Task | None = None self.listener: ModbusProtocol | None = None + self.is_listener: bool = False self.active_connections: dict[str, ModbusProtocol] = {} self.unique_id: str = str(id(self)) self.reconnect_delay_current = 0.0 @@ -249,6 +250,7 @@ async def listen(self) -> bool: """Handle generic listen and call on to specific transport listen.""" Log.debug("Awaiting connections {}", self.comm_params.comm_name) self.is_closing = False + self.is_listener = True try: self.transport = await self.call_create() if isinstance(self.transport, tuple): @@ -281,7 +283,7 @@ def connection_lost(self, reason: Exception | None) -> None: return Log.debug("Connection lost {} due to {}", self.comm_params.comm_name, reason) self.__close() - if self.is_server: + if self.is_listener: self.reconnect_task = asyncio.create_task(self.do_relisten()) self.reconnect_task.set_name("transport relisten") elif not self.listener and self.comm_params.reconnect_delay: @@ -397,7 +399,7 @@ def __close(self, reconnect: bool = False) -> None: self.transport.close() self.transport = None # type: ignore[assignment] self.recv_buffer = b"" - if self.is_server: + if self.is_listener: for _key, value in self.active_connections.items(): value.listener = None value.callback_disconnected(None) From bde715fa9dda1a6fc5c2cc6e1364998c4a97a2f8 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Tue, 12 Nov 2024 10:40:16 +0100 Subject: [PATCH 2/2] server listen port. --- pymodbus/transport/transport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 07e1f8c0c..6ccc84eee 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -167,7 +167,7 @@ def __init__( # This behaviour isn't quite right. # It listens on any IPv4 address rather than the more natural default of any address (v6 or v4). host = "0.0.0.0" # Any IPv4 host - port = 0 # Server will select an ephemeral port for itself + port = 502 # Server will listen on standard modbus port else: host = self.comm_params.host port = int(self.comm_params.port)