mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
Use the frame start once we've found it
This commit is contained in:
parent
5bfe63912c
commit
4df3655752
2 changed files with 35 additions and 0 deletions
|
|
@ -79,6 +79,10 @@ class SerialConnection:
|
||||||
idx = data.find(b"\x3e")
|
idx = data.find(b"\x3e")
|
||||||
if idx < 0: # no start of frame
|
if idx < 0: # no start of frame
|
||||||
return
|
return
|
||||||
|
# Discard any leading junk bytes before the actual frame marker.
|
||||||
|
# Some radios interleave console/debug text on the same UART, so
|
||||||
|
# valid companion frames may begin at an offset inside the chunk.
|
||||||
|
data = data[idx:]
|
||||||
self.header = data[0:1]
|
self.header = data[0:1]
|
||||||
data = data[1:]
|
data = data[1:]
|
||||||
|
|
||||||
|
|
|
||||||
31
tests/unit/test_serial_connection.py
Normal file
31
tests/unit/test_serial_connection.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from meshcore.serial_cx import SerialConnection
|
||||||
|
|
||||||
|
|
||||||
|
class RecordingReader:
|
||||||
|
def __init__(self):
|
||||||
|
self.frames = []
|
||||||
|
|
||||||
|
async def handle_rx(self, data):
|
||||||
|
self.frames.append(bytes(data))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_handle_rx_discards_leading_junk_before_frame_start():
|
||||||
|
conn = SerialConnection("/dev/null", 115200)
|
||||||
|
reader = RecordingReader()
|
||||||
|
conn.set_reader(reader)
|
||||||
|
|
||||||
|
payload = b"\x00\x01\x02\x53"
|
||||||
|
frame = b"\x3e" + len(payload).to_bytes(2, "little") + payload
|
||||||
|
|
||||||
|
conn.handle_rx(b"junk bytes\r\n" + frame)
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
|
assert reader.frames == [payload]
|
||||||
|
assert conn.header == b""
|
||||||
|
assert conn.inframe == b""
|
||||||
|
assert conn.frame_expected_size == 0
|
||||||
Loading…
Add table
Add a link
Reference in a new issue