From d4278b8afa87bc34f0731dcc6d831406089e1b73 Mon Sep 17 00:00:00 2001 From: Florent Date: Thu, 10 Jul 2025 17:24:01 +0200 Subject: [PATCH] deal with serial_cx issues --- examples/pubsub_example.py | 4 +++- examples/serial_channel_manager.py | 2 +- examples/serial_infos.py | 5 ++--- src/meshcore/meshcore.py | 5 ++--- src/meshcore/serial_cx.py | 6 ++++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/pubsub_example.py b/examples/pubsub_example.py index dde5860..15d97a8 100644 --- a/examples/pubsub_example.py +++ b/examples/pubsub_example.py @@ -44,6 +44,8 @@ async def main(): print("Connected to MeshCore device") + res = await meshcore.commands.send_device_query() + # Get contacts result = await meshcore.commands.get_contacts() if result.type == EventType.ERROR: @@ -118,4 +120,4 @@ if __name__ == "__main__": # This prevents the KeyboardInterrupt traceback from being shown print("\nExited cleanly") except Exception as e: - print(f"Error: {e}") \ No newline at end of file + print(f"Error: {e}") diff --git a/examples/serial_channel_manager.py b/examples/serial_channel_manager.py index f00726f..09f9423 100755 --- a/examples/serial_channel_manager.py +++ b/examples/serial_channel_manager.py @@ -113,4 +113,4 @@ async def set_channel_config(mc): print() if __name__ == "__main__": - asyncio.run(main()) \ No newline at end of file + asyncio.run(main()) diff --git a/examples/serial_infos.py b/examples/serial_infos.py index 7e8000d..a6bb684 100755 --- a/examples/serial_infos.py +++ b/examples/serial_infos.py @@ -1,15 +1,14 @@ #!/usr/bin/python import asyncio - -from meshcore import MeshCore +from meshcore import MeshCore, EventType PORT = "/dev/tty.usbserial-583A0069501" BAUDRATE = 115200 async def main(): mc = await MeshCore.create_serial(PORT, BAUDRATE) - + print(mc.self_info) await mc.disconnect() diff --git a/src/meshcore/meshcore.py b/src/meshcore/meshcore.py index ad37b48..e7e88d6 100644 --- a/src/meshcore/meshcore.py +++ b/src/meshcore/meshcore.py @@ -67,10 +67,9 @@ class MeshCore: @classmethod async def create_serial(cls, port: str, baudrate: int = 115200, debug: bool = False, default_timeout=None, - auto_reconnect: bool = False, max_reconnect_attempts: int = 3) -> 'MeshCore': + auto_reconnect: bool = False, max_reconnect_attempts: int = 3, cx_dly:float = 0.1) -> 'MeshCore': """Create and connect a MeshCore instance using serial connection""" - connection = SerialConnection(port, baudrate) - await asyncio.sleep(0.2) # Time for transport to establish + connection = SerialConnection(port, baudrate, cx_dly=cx_dly) mc = cls(connection, debug=debug, default_timeout=default_timeout, auto_reconnect=auto_reconnect, max_reconnect_attempts=max_reconnect_attempts) diff --git a/src/meshcore/serial_cx.py b/src/meshcore/serial_cx.py index 05419bd..8bf9430 100644 --- a/src/meshcore/serial_cx.py +++ b/src/meshcore/serial_cx.py @@ -9,7 +9,7 @@ import serial_asyncio logger = logging.getLogger("meshcore") class SerialConnection: - def __init__(self, port, baudrate): + def __init__(self, port, baudrate, cx_dly=0.2): self.port = port self.baudrate = baudrate self.frame_started = False @@ -18,6 +18,7 @@ class SerialConnection: self.header = b"" self.inframe = b"" self._disconnect_callback = None + self.cx_dly = cx_dly class MCSerialClientProtocol(asyncio.Protocol): def __init__(self, cx): @@ -52,6 +53,7 @@ class SerialConnection: loop, lambda: self.MCSerialClientProtocol(self), self.port, baudrate=self.baudrate) + await asyncio.sleep(self.cx_dly) # wait for cx to establish logger.info("Serial Connection started") return self.port @@ -100,4 +102,4 @@ class SerialConnection: def set_disconnect_callback(self, callback): """Set callback to handle disconnections.""" - self._disconnect_callback = callback \ No newline at end of file + self._disconnect_callback = callback