mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-14 05:06:53 +00:00
Update serial cx to more smartly await for connection
This commit is contained in:
@@ -20,6 +20,7 @@ class SerialConnection:
|
|||||||
self.inframe = b""
|
self.inframe = b""
|
||||||
self._disconnect_callback = None
|
self._disconnect_callback = None
|
||||||
self.cx_dly = cx_dly
|
self.cx_dly = cx_dly
|
||||||
|
self._connected_event = asyncio.Event()
|
||||||
|
|
||||||
class MCSerialClientProtocol(asyncio.Protocol):
|
class MCSerialClientProtocol(asyncio.Protocol):
|
||||||
def __init__(self, cx):
|
def __init__(self, cx):
|
||||||
@@ -30,12 +31,16 @@ class SerialConnection:
|
|||||||
logger.debug('port opened')
|
logger.debug('port opened')
|
||||||
if isinstance(transport, serial_asyncio.SerialTransport) and transport.serial:
|
if isinstance(transport, serial_asyncio.SerialTransport) and transport.serial:
|
||||||
transport.serial.rts = False # You can manipulate Serial object via transport
|
transport.serial.rts = False # You can manipulate Serial object via transport
|
||||||
|
# Signal that connection is established
|
||||||
|
self.cx._connected_event.set()
|
||||||
|
|
||||||
def data_received(self, data):
|
def data_received(self, data):
|
||||||
self.cx.handle_rx(data)
|
self.cx.handle_rx(data)
|
||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
logger.debug('Serial port closed')
|
logger.debug('Serial port closed')
|
||||||
|
# Clear the connected event
|
||||||
|
self.cx._connected_event.clear()
|
||||||
if self.cx._disconnect_callback:
|
if self.cx._disconnect_callback:
|
||||||
asyncio.create_task(self.cx._disconnect_callback("serial_disconnect"))
|
asyncio.create_task(self.cx._disconnect_callback("serial_disconnect"))
|
||||||
|
|
||||||
@@ -49,12 +54,16 @@ class SerialConnection:
|
|||||||
"""
|
"""
|
||||||
Connects to the device
|
Connects to the device
|
||||||
"""
|
"""
|
||||||
|
# Clear any previous connection state
|
||||||
|
self._connected_event.clear()
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
await serial_asyncio.create_serial_connection(
|
await serial_asyncio.create_serial_connection(
|
||||||
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
|
# Wait for the actual connection to be established
|
||||||
|
await self._connected_event.wait()
|
||||||
logger.info("Serial Connection started")
|
logger.info("Serial Connection started")
|
||||||
return self.port
|
return self.port
|
||||||
|
|
||||||
@@ -99,6 +108,8 @@ class SerialConnection:
|
|||||||
if self.transport:
|
if self.transport:
|
||||||
self.transport.close()
|
self.transport.close()
|
||||||
self.transport = None
|
self.transport = None
|
||||||
|
# Clear the connected event
|
||||||
|
self._connected_event.clear()
|
||||||
logger.debug("Serial Connection closed")
|
logger.debug("Serial Connection closed")
|
||||||
|
|
||||||
def set_disconnect_callback(self, callback):
|
def set_disconnect_callback(self, callback):
|
||||||
|
|||||||
Reference in New Issue
Block a user