deal with serial_cx issues

This commit is contained in:
Florent 2025-07-10 17:24:01 +02:00
parent 190fde8a45
commit d4278b8afa
5 changed files with 12 additions and 10 deletions

View file

@ -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}")
print(f"Error: {e}")

View file

@ -113,4 +113,4 @@ async def set_channel_config(mc):
print()
if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())

View file

@ -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()

View file

@ -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)

View file

@ -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
self._disconnect_callback = callback