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") print("Connected to MeshCore device")
res = await meshcore.commands.send_device_query()
# Get contacts # Get contacts
result = await meshcore.commands.get_contacts() result = await meshcore.commands.get_contacts()
if result.type == EventType.ERROR: if result.type == EventType.ERROR:

View File

@@ -1,8 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
import asyncio import asyncio
from meshcore import MeshCore, EventType
from meshcore import MeshCore
PORT = "/dev/tty.usbserial-583A0069501" PORT = "/dev/tty.usbserial-583A0069501"
BAUDRATE = 115200 BAUDRATE = 115200

View File

@@ -67,10 +67,9 @@ class MeshCore:
@classmethod @classmethod
async def create_serial(cls, port: str, baudrate: int = 115200, debug: bool = False, default_timeout=None, 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""" """Create and connect a MeshCore instance using serial connection"""
connection = SerialConnection(port, baudrate) connection = SerialConnection(port, baudrate, cx_dly=cx_dly)
await asyncio.sleep(0.2) # Time for transport to establish
mc = cls(connection, debug=debug, default_timeout=default_timeout, mc = cls(connection, debug=debug, default_timeout=default_timeout,
auto_reconnect=auto_reconnect, max_reconnect_attempts=max_reconnect_attempts) auto_reconnect=auto_reconnect, max_reconnect_attempts=max_reconnect_attempts)

View File

@@ -9,7 +9,7 @@ import serial_asyncio
logger = logging.getLogger("meshcore") logger = logging.getLogger("meshcore")
class SerialConnection: class SerialConnection:
def __init__(self, port, baudrate): def __init__(self, port, baudrate, cx_dly=0.2):
self.port = port self.port = port
self.baudrate = baudrate self.baudrate = baudrate
self.frame_started = False self.frame_started = False
@@ -18,6 +18,7 @@ class SerialConnection:
self.header = b"" self.header = b""
self.inframe = b"" self.inframe = b""
self._disconnect_callback = None self._disconnect_callback = None
self.cx_dly = cx_dly
class MCSerialClientProtocol(asyncio.Protocol): class MCSerialClientProtocol(asyncio.Protocol):
def __init__(self, cx): def __init__(self, cx):
@@ -52,6 +53,7 @@ class SerialConnection:
loop, lambda: self.MCSerialClientProtocol(self), loop, lambda: self.MCSerialClientProtocol(self),
self.port, baudrate=self.baudrate) self.port, baudrate=self.baudrate)
await asyncio.sleep(self.cx_dly) # wait for cx to establish
logger.info("Serial Connection started") logger.info("Serial Connection started")
return self.port return self.port