MAVLink C UDP 示例
The MAVLink UDP Example is a simple C example that sends some data to QGroundControl using MAVLink over UDP. QGroundControl responds with heartbeats and other messages, which are then printed by this program.
INFO
The example should work on any Unix-like system (Linux, MacOS, BSD, etc.). These instructions were tested on a clean Ubuntu LTS 20.04 installation using the default version of gcc (9.3.0).
构建/运行示例
下面的说明演示如何生成和运行该示例。
Install MAVLink and generate the MAVLink 2.0 libraries into the mavlink/include directory. For example, to generate the headers for common.xml you could use the command line:
shpython3 -m pymavlink.tools.mavgen --lang=C --wire-protocol=2.0 --output=./include/ message_definitions/v1.0/common.xml
TIP
Alternatively you can clone the mavlink/mavlink repository and Download prebuilt headers to the same location.
INFO
The example will not work with MAVLink 1 because it uses a message that includes extension fields which do not exist in MAVLink 1 (
SYS_STATUS
).INFO
You can put/generate the library wherever you like, but the build command below assumes they are located in directory named include below the MAVLink root directory.
Open a terminal and navigate to examples/c
使用下列命令编译GCC:
shgcc -std=c99 -I ../../include/common -o mavlink_udp udp_example.c
INFO
The MAVLink header directory must be added to the include path. The path here assumes you are building the code from the example directory, and that have installed the headers in mavlink/include.
从终端运行可执行文件:
bash./mavlink_udp
默认情况下,实例将接收本地主机IP地址,1451端口的数据。 You can specify another IP address as a command line argument (use
./mavlink_udp --help
to see usage).Open QGroundControl on the same machine.
QGroundControl immediately starts broadcasting its
HEARTBEAT
on port 14551.INFO
QGroundControl returns data, but will not actually "connect" to the example (it will continue to display the message Waiting for Vehicle Connection).
例子应该开始显示终端收到的数据:
sh~/github/mavlink/examples/c$ ./mavlink_udp Bytes Received: 17 Datagram: fe 09 00 ff 00 00 00 00 00 00 06 08 c0 04 03 19 87 Received packet: SYS: 255, COMP: 0, LEN: 9, MSG ID: 0 Bytes Received: 17 Datagram: fe 09 01 ff 00 00 00 00 00 00 06 08 c0 04 03 f3 f9 Received packet: SYS: 255, COMP: 0, LEN: 9, MSG ID: 0 ...