From 1145c08bcfa8e2246081ac0c006f67ba6b3218d8 Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 15 Sep 2026 08:46:28 -0400 Subject: [PATCH] try to invert dtr and reconnect if serial connection fails --- src/meshcore/commands/device.py | 7 +++++-- src/meshcore/meshcore.py | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/meshcore/commands/device.py b/src/meshcore/commands/device.py index 973d660..c44cc5f 100644 --- a/src/meshcore/commands/device.py +++ b/src/meshcore/commands/device.py @@ -11,10 +11,13 @@ logger = logging.getLogger("meshcore") class DeviceCommands(CommandHandlerBase): - async def send_appstart(self) -> Event: + async def send_appstart(self, timeout=None) -> Event: logger.debug("Sending appstart command") b1 = bytearray(b"\x01\x03 mccli") - return await self.send(b1, [EventType.SELF_INFO, EventType.ERROR]) + if timeout is None: + return await self.send(b1, [EventType.SELF_INFO, EventType.ERROR]) + else: + return await self.send(b1, [EventType.SELF_INFO, EventType.ERROR], timeout=timeout) async def send_device_query(self) -> Event: logger.debug("Sending device query command") diff --git a/src/meshcore/meshcore.py b/src/meshcore/meshcore.py index f965af3..fc9b36c 100644 --- a/src/meshcore/meshcore.py +++ b/src/meshcore/meshcore.py @@ -133,6 +133,19 @@ class MeshCore: max_reconnect_attempts=max_reconnect_attempts, ) res = await mc.connect() + if res is None: + logger.info("No response from meshcore node, trying to invert dtr") + await mc.disconnect() + connection = SerialConnection(port, baudrate, cx_dly=cx_dly, rts=rts, dtr=not dtr) + mc = cls( + connection, + debug=debug, + only_error=only_error, + default_timeout=default_timeout, + auto_reconnect=auto_reconnect, + max_reconnect_attempts=max_reconnect_attempts, + ) + res = await mc.connect() if res is None: logger.error("No response from meshcore node, disconnecting") logger.error("Are you sure your node is a serial companion ?") @@ -190,7 +203,7 @@ class MeshCore: to initialize the session. MeshCore.connect() does this on the initial connection; this callback ensures it also happens on reconnects (F02). """ - await self.commands.send_appstart() + await self.commands.send_appstart(timeout=2) async def connect(self): await self.dispatcher.start() @@ -198,7 +211,7 @@ class MeshCore: if result is None: await self.dispatcher.stop() raise ConnectionError("Failed to connect to device") - res = await self.commands.send_appstart() + res = await self.commands.send_appstart(timeout=2) if res is None or res.type == EventType.ERROR: return None return res