diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 6c7c00b3f..6ccc84eee 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 @@ -166,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) @@ -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)