mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
Add trace packet type
This commit is contained in:
parent
a5f1ec5c26
commit
ea2f17025f
5 changed files with 168 additions and 4 deletions
70
examples/serial_trace.py
Normal file
70
examples/serial_trace.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env python3
|
||||
import asyncio
|
||||
import argparse
|
||||
from meshcore import MeshCore
|
||||
from meshcore.events import EventType
|
||||
|
||||
async def get_repeater_name(mc, hash_prefix):
|
||||
"""Find a contact by its 2-character hash prefix and return its name"""
|
||||
# Ensure contacts are available
|
||||
await mc.ensure_contacts()
|
||||
|
||||
# Find contact with matching hash prefix
|
||||
contact = mc.get_contact_by_key_prefix(hash_prefix)
|
||||
if contact:
|
||||
return contact.get("adv_name", f"Unknown ({hash_prefix})")
|
||||
else:
|
||||
return f"Unknown ({hash_prefix})"
|
||||
|
||||
async def main():
|
||||
parser = argparse.ArgumentParser(description='MeshCore Serial Trace Example')
|
||||
parser.add_argument('-p', '--port', required=True, help='Serial port path')
|
||||
parser.add_argument('--path', type=str, help='Trace path (comma-separated hex values)')
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
# Connect to device
|
||||
print(f"Connecting to {args.port}...")
|
||||
mc = await MeshCore.create_serial(args.port, 115200, debug=True)
|
||||
|
||||
# Send trace packet
|
||||
print(f"Sending trace packet...")
|
||||
result = await mc.commands.send_trace(path=args.path)
|
||||
|
||||
if result:
|
||||
print("Trace packet sent successfully")
|
||||
print("Waiting for trace response...")
|
||||
|
||||
# Wait for a trace response with 15-second timeout
|
||||
event = await mc.wait_for_event(EventType.TRACE_DATA, timeout=15)
|
||||
|
||||
if event:
|
||||
trace = event.payload
|
||||
print(f"Trace data received:")
|
||||
print(f" Tag: {trace['tag']}")
|
||||
print(f" Flags: {trace.get('flags', 0)}")
|
||||
print(f" Path Length: {trace.get('path_len', 0)}")
|
||||
|
||||
if trace.get('path'):
|
||||
print(f" Path ({len(trace['path'])} nodes):")
|
||||
|
||||
# Process nodes with hash (repeaters)
|
||||
for i, node in enumerate(trace['path']):
|
||||
if 'hash' in node:
|
||||
# Look up repeater name
|
||||
repeater_name = await get_repeater_name(mc, node['hash'])
|
||||
print(f" Node {i+1}: {repeater_name}, SNR={node['snr']:.1f} dB")
|
||||
else:
|
||||
print(f" Node {i+1}: SNR={node['snr']:.1f} dB (final node)")
|
||||
else:
|
||||
print("No trace response received within timeout")
|
||||
else:
|
||||
print("Failed to send trace packet")
|
||||
|
||||
await mc.disconnect()
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue