Dump All Messages
This example script shows how to dump all messages in human readable form
Writing the script
One code sample is worth 1024 words:
#!/usr/bin/env python3
'''
dump all messages in YAML format
'''
import dronecan, time, math
# get command line arguments
from argparse import ArgumentParser
parser = ArgumentParser(description='dump all DroneCAN messages')
parser.add_argument("--bitrate", default=1000000, type=int, help="CAN bit rate")
parser.add_argument("--node-id", default=100, type=int, help="CAN node ID")
parser.add_argument("--dna-server", action='store_true', default=False, help="run DNA server")
parser.add_argument("port", default=None, type=str, help="serial port")
args = parser.parse_args()
# Initializing a DroneCAN node instance.
node = dronecan.make_node(args.port, node_id=args.node_id, bitrate=args.bitrate)
# Initializing a node monitor, so we can see what nodes are online
node_monitor = dronecan.app.node_monitor.NodeMonitor(node)
if args.dna_server:
# optionally start a DNA server
dynamic_node_id_allocator = dronecan.app.dynamic_node_id.CentralizedServer(node, node_monitor)
# callback for printing all messages in human-readable YAML format.
node.add_handler(None, lambda msg: print(dronecan.to_yaml(msg)))
# Running the node until the application is terminated or until first error.
try:
node.spin()
except KeyboardInterrupt:
pass
Running the script
Save the above code somewhere and run it. It will print all messages on the bus in YAML format