You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pymavlink中wait_heartbeat()卡住问题求助(HereLink场景)

核心原因

  • HereLink的Air-unit默认需要地面站主动发起MAVLink握手(发送心跳包),才会持续输出MAVLink消息。你的Python脚本一开始只调用wait_heartbeat()等待接收,但没有主动发送心跳,Air-unit无法识别这个UDP连接是有效的地面站,因此不会向该端口推送任何数据。
  • QGC启动后,会自动周期性发送MAV_HEARTBEAT心跳包,同时主动请求数据流转发,Air-unit收到这些信号后,才会开始向对应的连接发送心跳和其他消息,这时你的脚本就能正常接收数据了。

修复后的代码

import time
from pymavlink import mavutil

# UDP连接无需设置baud参数,直接移除该配置
master = mavutil.mavlink_connection('udp:192.168.144.133:14550')

# 主动发送心跳包,告知Air-unit当前连接为地面站
master.mav.heartbeat_send(
    mavutil.mavlink.MAV_TYPE_GCS,
    mavutil.mavlink.MAV_AUTOPILOT_INVALID,
    0, 0, 0
)

# 等待Air-unit的心跳响应
master.wait_heartbeat()
print(f"已建立连接,系统ID: {master.target_system}, 组件ID: {master.target_component}")

# 主动请求全量数据流,设置为每秒10次推送
master.mav.request_data_stream_send(
    master.target_system,
    master.target_component,
    mavutil.mavlink.MAV_DATA_STREAM_ALL,
    10,
    1
)

while True:
    try:
        msg = master.recv_match()
        if msg:
            print(msg.to_dict())
    except Exception as e:
        print(f"接收错误: {e}")
    time.sleep(0.1)

关键说明

  1. 移除UDP连接的baud参数:该参数仅适用于串口(如USB、UART)连接,UDP协议不需要波特率配置,原代码中的baud=57600属于无效配置,可直接删除。
  2. 主动发送心跳包:这是让Air-unit识别有效地面站连接的必要步骤,没有这一步,Air-unit不会主动向你的脚本推送任何消息。
  3. 请求数据流:通过request_data_stream_send可以指定需要Air-unit推送的消息类型和频率,确保能持续收到所需的MAVLink数据。

内容的提问来源于stack exchange,提问作者EhsanGGGGG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:22:42