美文网首页
iOS MAVlink协议

iOS MAVlink协议

作者: fanaf | 来源:发表于2023-03-23 17:02 被阅读0次

    基于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
        } 
      }
    

    }

    相关文章

      网友评论

          本文标题:iOS MAVlink协议

          本文链接:https://www.haomeiwen.com/subject/gmbirdtx.html