环境
windows10 + python3.11 + pymavlink2.4.40
工具
虚拟串口工具 Virtual Serial Port Driver 6.9
串口/网络调试助手 sscom5.13.1.exe
串口通信
-
虚拟串口 COM1, COM2
-
调试助手连接COM2
-
pymavlink连接COM1
from pymavlink import mavutil
import timeconnect = mavutil.mavlink_connection('COM1', source_system=1, source_component=2)target_system = connect.target_system
target_component = connect.target_componentmission_request_list_message = connect.mav.mission_request_list_encode(target_system, target_component)
# 发送mavlink消息
count = 10
while count > 0:connect.mav.send(mission_request_list_message)count -= 1time.sleep(1)
- COM2收到COM1的消息
UDP通信
-
调试助手建立UDP连接
-
pymavlink使用UDP发送消息到指定ip和端口
from pymavlink import mavutil
import timeconnect = mavutil.mavlink_connection('udpout:127.0.0.1:8000', source_system=1, source_component=2)
# connect = mavutil.mavlink_connection('udpbcast:127.0.0.1:8000', source_system=1, source_component=2)
target_system = connect.target_system
target_component = connect.target_componentmission_request_list_message = connect.mav.mission_request_list_encode(target_system, target_component)
# 发送mavlink消息
count = 10
while count > 0:connect.mav.send(mission_request_list_message)count -= 1time.sleep(1)
- 调试助手收到消息
使用pymavlink建立udp连接后接收消息
from pymavlink import mavutilconnect = mavutil.mavlink_connection('udpin:127.0.0.1:10000', source_system=1, source_component=2)while True:msg = connect.recv_match(blocking=True)# print(list(msg.data))print(msg.get_type())
- 发送消息
- 接收消息
心跳包
connect.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_ONBOARD_CONTROLLER,mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
FE 09 00 01 02 00 00 00 00 00 12 08 00 00 03 03 F5
解析 GLOBAL_POSITION_INT
- 使用网络调试助手通过串口连接pixhawk,得到一条mavlink消息
FE 1C 0A 01 01 21 0B 45 01 00 00 00 00 00 00 00 00 00 92 13 00 00 9E 26 00 00 00 00 02 00 1B 00 97 0A 72 9B
十六进制21对应十进制33
2. 在mavlink官网查看#33的消息结构
- 使用pymavlink接收消息并解析
4.1 源码
from pymavlink import mavutil
import time
connect = mavutil.mavlink_connection('udpin:127.0.0.1:10000', source_system=1, source_component=1)while True:msg = connect.recv_match(blocking=True)print(msg.get_type())print(msg.get_fieldnames())print(type(msg))print(msg.time_boot_ms)print("******")print(msg.lat)print(msg.lon)print(msg.alt)print(msg.relative_alt)print("-------")print(msg.vx)print(msg.vy)print(msg.vz)print(msg.hdg)
4.2 结果
解析 ATTITUDE
报文:
FE 1C 1F 01 01 1E 08 46 01 00 41 87 66 3C 19 BC 26 3C DB 4C F2 3E 48 24 B3 38 20 9C 55 B9 9E 57 41 38 C6 84
解析代码
while True:msg = connect.recv_match(blocking=True)print(msg.get_type())print(msg.get_fieldnames())print(msg.roll)print(msg.pitch)print(msg.yaw)
结果: