基于C语言,解析MAVlink消息:
创建解析消息类
class MavLinkMessage: NSObject {
static let share = MavLinkMessage()
var dicId = Dictionary<String, Any>()
}
/// 解析无人机信息 data 长连接返回的无人机数据 ,port和host 是udp连接的post和host
func udpPort(didReceive data: Data,_ port: UInt16,_ host:String) {
let bytes: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer.allocate(capacity: data.count)
data.copyBytes(to: bytes, count: data.count)
for index in 0..<data.count {
let byte: UInt8 = (bytes + index).pointee
var message = mavlink_message_t()
var status = mavlink_status_t()
let channel = UInt8(MAVLINK_COMM_0.rawValue)
if mavlink_parse_char(channel, byte, &message, &status) != 0 {
let mess = message.encodeDescription(port,host)
print(mess)
}
}
bytes.deallocate()
}
}
extension mavlink_message_t {
/* 解析MavLInk数据
*/
public func encodeDescription(_ port: UInt16,_ host: String) -> String{
let mav = MavLinkMessage.share
mav.dicId["\(message.msgid)"] = "\(message.msgid)"
switch msgid {
case UInt32(MAVLINK_MSG_ID_HEARTBEAT):
/// 心跳消息
break
}
}
}
网友评论