UART communication using the MAVLink protocol,HELP ME

16 views
Skip to first unread message

刘企

unread,
Apr 8, 2025, 8:54:23 AMApr 8
to MAVLink

#include <unistd.h>

#include "fpl011.h"

#include "src/mavlink2.h"

#include "serial_flow_example.h"

#include <unistd.h>


void request_system_status(FPl011 *uart) {

mavlink_message_t msg;

mavlink_command_long_t cmd = {

.command = MAV_CMD_REQUEST_MESSAGE,

.param1 = MAVLINK_MSG_ID_SYS_STATUS

};

mavlink_msg_command_long_encode(OUR_SYS_ID, OUR_COMP_ID, &msg, &cmd);


uint8_t buffer[MAVLINK_MAX_PACKET_LEN];

uint16_t len = mavlink_msg_to_send_buffer(buffer, &msg);

FPl011Send(uart, buffer, len);

}



int main()

{

FError ret = FSerialFlowInit();

if (ret != FT_SUCCESS) {

printf("UART初始化失败!错误码:%d", ret);

return -1;

}


mavlink_message_t received_msg;

set_sys_status_stream(MAVLINK_COMM_0);

request_system_status(&uart_instance);

while(1)

{

// 发送心跳包(每秒1次)

SendMavlinkHeartbeat(&uart_instance);


// 处理接收到的消息

bool message_received = process_mavlink_messages(&uart_instance, &received_msg);

if (message_received) {

print_sys_status(&received_msg);

}


// 非阻塞延时(100ms)

fsleep_millisec(100);

}


return 0;


}

void SendMavlinkHeartbeat(FPl011 *uart) {

mavlink_message_t msg;

uint8_t buffer[MAVLINK_MAX_PACKET_LEN];


mavlink_msg_heartbeat_pack(OUR_SYS_ID, OUR_COMP_ID, &msg,

MAV_TYPE_ONBOARD_CONTROLLER, MAV_AUTOPILOT_GENERIC, 0, 0, 0);


uint16_t len = mavlink_msg_to_send_buffer(buffer, &msg);

u32 sent = FPl011Send(uart, buffer, len);


// 添加调试输出

printf("[发送] 心跳包长度: %d, 实际发送字节: %d\n", len, sent);

}

bool process_mavlink_messages(FPl011 *uart, mavlink_message_t *msg) {

static mavlink_status_t status;

uint8_t byte;

bool message_received = false;

static int byte_count = 0;


while (FPl011Receive(uart, &byte, 1) == FT_SUCCESS) {

// 打印原始字节(十六进制和ASCII)

printf("[RAW] Byte %02d: 0x%02X | ASCII: %c\n",

byte_count++, byte, (byte >= 32 && byte <= 126) ? byte : '.');


if (mavlink_parse_char(MAVLINK_COMM_0, byte, msg, &status)) {

message_received = true;

printf("[MAVLink] 解析到消息 ID=%d, 长度=%d\n", msg->msgid, msg->len);

}

}

return message_received;

}



// 设置 SYS_STATUS 消息的发送频率为1Hz(间隔1,000,000微秒)

void set_sys_status_stream(mavlink_channel_t channel) {

mavlink_message_t msg;

uint8_t buffer[MAVLINK_MAX_PACKET_LEN];


mavlink_msg_command_long_pack(

OUR_SYS_ID,

OUR_COMP_ID,

&msg,

TARGET_SYS_ID,

TARGET_COMP_ID,

MAV_CMD_SET_MESSAGE_INTERVAL, // 命令ID

0, // 确认标记

MAVLINK_MSG_ID_SYS_STATUS, // 参数1:消息ID

1000000, // 参数2:间隔时间(微秒,1秒=1,000,000)

0, 0, 0, 0, 0 // 其他参数未使用

);

FPl011 *uart;

uint16_t len = mavlink_msg_to_send_buffer(buffer, &msg);

u32 sent = FPl011Send(&uart_instance, buffer, len);

}


void print_sys_status(mavlink_message_t* msg) {

if (msg->msgid != MAVLINK_MSG_ID_SYS_STATUS) {

printf("错误:传入的消息不是 SYS_STATUS 类型!\n");

return;

}


mavlink_sys_status_t sys_status;

mavlink_msg_sys_status_decode(msg, &sys_status);


// 检查电压和电池剩余值是否有效

if (sys_status.voltage_battery == 0 && sys_status.battery_remaining == 0) {

printf("警告:接收到的电池数据全为0,可能未正确配置或未收到数据!\n");

} else {

printf("[系统状态] 电压: %.1f V, 电池剩余: %d%%\n",

sys_status.voltage_battery / 1000.0f,

sys_status.battery_remaining);

}

}

During UART communication using the MAVLink protocol, the receiving end continuously receives 0x15 (NAK, Negative Acknowledgment) characters, leading to failure in parsing valid MAVLink messages. Suspicions include potential issues with excessive message length or hardware/configuration errors.

Hamish Willee

unread,
Apr 9, 2025, 8:01:36 PMApr 9
to MAVLink
I don't believe MAVLink itself uses this NAK character, so it must be being injected somewhere else.

Have you tried the "out of the box" example https://mavlink.io/en/mavgen_c/example_c_uart.html to see whether that has the same problems?

I haven't got enough practical experience to help sorry. If it were me I'd be posting this request with runnable code (i.e. perhaps a mini project on github) on the discussion boards of the flight stack I was using, along with information about my hardware setup and software setup (e.g. flight stack, what version) etc.
Reply all
Reply to author
Forward
0 new messages