mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
deal with serial_cx issues
This commit is contained in:
parent
190fde8a45
commit
d4278b8afa
5 changed files with 12 additions and 10 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue