try to invert dtr and reconnect if serial connection fails

This commit is contained in:
Florent
2026-09-15 08:46:28 -04:00
parent ec0582df22
commit 1145c08bcf
2 changed files with 20 additions and 4 deletions
+5 -2
View File
@@ -11,10 +11,13 @@ logger = logging.getLogger("meshcore")
class DeviceCommands(CommandHandlerBase): class DeviceCommands(CommandHandlerBase):
async def send_appstart(self) -> Event: async def send_appstart(self, timeout=None) -> Event:
logger.debug("Sending appstart command") logger.debug("Sending appstart command")
b1 = bytearray(b"\x01\x03 mccli") 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: async def send_device_query(self) -> Event:
logger.debug("Sending device query command") logger.debug("Sending device query command")
+15 -2
View File
@@ -133,6 +133,19 @@ class MeshCore:
max_reconnect_attempts=max_reconnect_attempts, max_reconnect_attempts=max_reconnect_attempts,
) )
res = await mc.connect() 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: if res is None:
logger.error("No response from meshcore node, disconnecting") logger.error("No response from meshcore node, disconnecting")
logger.error("Are you sure your node is a serial companion ?") 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 to initialize the session. MeshCore.connect() does this on the initial
connection; this callback ensures it also happens on reconnects (F02). 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): async def connect(self):
await self.dispatcher.start() await self.dispatcher.start()
@@ -198,7 +211,7 @@ class MeshCore:
if result is None: if result is None:
await self.dispatcher.stop() await self.dispatcher.stop()
raise ConnectionError("Failed to connect to device") 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: if res is None or res.type == EventType.ERROR:
return None return None
return res return res