Read magneto data using C#

Hello guys i’m tryning to read mavlink data using C# all the ressources that i found was in C, so i didn’t found something in c # can you help please :frowning:
i’m new with mavlink and new in c# so really nead some codes to base on, i will arpeciate
Tnx

Mavlink does have C# bindings. Take a look at the documentation at https://github.com/ArduPilot/mavlink

There’s a few examples of people using Mavlink in C#:
-Mission Planner https://github.com/ArduPilot/MissionPlanner
-mavlink.net https://github.com/dsuarezv/mavlink.net

1 Like

Hello again i’m having some problems in sending and receiving message.
Can you help please.
private void OpenSerialPort()
{

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
      
        try
        {
            mySerialPort.DataReceived += SerialPortDataReceived;
            mySerialPort.Open();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
        }
    }

    private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {            
        var serialPort = (SerialPort)sender;
        var data = serialPort.ReadByte();
        Console.WriteLine("Data : "+data);
        heartbeatData.Add((byte)data);
       
    }
    void Mavlink_request_data(byte id, byte rate)
    {
        MAVLink.MAVLinkMessage mAVLinkMessage = new MAVLink.MAVLinkMessage();
        uint[] buffer = new uint[MAVLink.MAVLINK_MAX_PACKET_LEN];
        MAVLink.MAVLinkParamList mAVLinkParams = new MAVLink.MAVLinkParamList();
        MAVLink.Mavlink_request_data_stream_t mavlink_Request_Data_Stream_T = new MAVLink.Mavlink_request_data_stream_t
        {
            req_message_rate = rate,
            req_stream_id = id,
            start_stop = 1,
            target_system = 1,
            target_component = 1,
        };

        byte[] MVQRQT = new byte[6];
        MVQRQT[0] = mavlink_Request_Data_Stream_T.target_system;
        MVQRQT[1] = mavlink_Request_Data_Stream_T.target_component;
        MVQRQT[2] = mavlink_Request_Data_Stream_T.req_stream_id;
        MVQRQT[3] = (byte)mavlink_Request_Data_Stream_T.req_message_rate;
        MVQRQT[5] = mavlink_Request_Data_Stream_T.start_stop;

        byte[] packet = GeneratePacket(MVQRQT,(byte) MAVLink.MAVLINK_MSG_ID.REQUEST_DATA_STREAM);
        SendPacket(packet);
    }

    byte[] GeneratePacket(byte[] packetData, byte msgID)
    {
        byte[] Packet = new byte[packetData.Length + 6 + 2];
        Packet[0] = (byte)254;
        Packet[1] = (byte)packetData.Length;
        Packet[2] = (byte)1;
        Packet[3] = (byte)255;
        Packet[4] = (byte)1;
        Packet[5] = msgID;
        for (int n = 0; n < packetData.Length; n++)
            Packet[6 + n] = packetData[n];
        ushort checksum = MavlinkCRC.crc_calculate(Packet, Packet[1] + 6);
        checksum = MavlinkCRC.crc_accumulate(MAVLink.MAVLINK_MESSAGE_CRCS[msgID], checksum);
        byte ck_a = (byte)(checksum & 0xFF); ///< High byte
        byte ck_b = (byte)(checksum >> 8); ///< Low byte
        Packet[Packet.Length - 2] = ck_a;
        Packet[Packet.Length - 1] = ck_b;
        return Packet;
    }
   

    bool SendPacket(byte[] packetData)
    {
        try
        {
            if (mySerialPort.IsOpen)
            {
                mySerialPort.Write(packetData, 0, packetData.Length);
                return true;
            }
        }
        catch
        {
            return false;
        }
        return false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Mavlink_request_data(200,1);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Mavlink_request_data(30, 1);
    }
}

}

when i click on the button2 to have the altitude i have an error that say:
System.IO.FileLoadException : ‘Impossible de charger le fichier ou l’assembly ‘System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ ou une de ses dépendances. La définition trouvée du manifeste de l’assembly ne correspond pas à la référence de l’assembly. (Exception de HRESULT : 0x80131040)’

I’m not sure what that error is … I think it might be something to do with running the incorrect .net framework version.

hey i correct this error using object unsteed of byte in the parameters (it’s worked and i don’t know how :p).
i’m trying to read the mavlink msg but the only think i got is the heartbeat it have been one week i’m working on that can you help please this is my code.

can you check it please. i’m trying here to read attitude (id==30)

    public int i;
    MAVLink.MAVLinkMessage t = new MAVLink.MAVLinkMessage();
    SerialPort mySerialPort = new SerialPort("COM14");
    public static MAVLink.Mavlink_heartbeat_t hb = new MAVLink.Mavlink_heartbeat_t();
    List<byte> heartbeatData = new List<byte>();

    public Form1()
    {
        InitializeComponent();
        OpenSerialPort();
    }
    private void OpenSerialPort()
    {

        mySerialPort.BaudRate = 57000;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;

        try
        {
            mySerialPort.DataReceived += SerialPortDataReceived;
            mySerialPort.Open();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    }

    private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        Mavlink_request_data(30, 0x02);//send request to read attitude id==30
        var serialPort = (SerialPort)sender;
        var data = serialPort.ReadByte();
        Console.WriteLine("Data : " + data);
        heartbeatData.Add((byte)data);
    }

    void Mavlink_request_data(byte id, byte rate)
    {
        
        MAVLink.MAVLinkMessage mAVLinkMessage = new MAVLink.MAVLinkMessage();

        uint[] buffer = new uint[MAVLink.MAVLINK_MAX_PACKET_LEN];
        MAVLink.MAVLinkParamList mAVLinkParams = new MAVLink.MAVLinkParamList();
        MAVLink.Mavlink_request_data_stream_t req = new MAVLink.Mavlink_request_data_stream_t
        {
            target_system = 1,
            target_component = 1,

            req_message_rate = rate,
            start_stop = 1, // start
            req_stream_id = (byte)id // id
        };

        // send each one twice.
        GeneratePacket((byte)MAVLINK_MSG_ID.REQUEST_DATA_STREAM, req);
        GeneratePacket((byte)MAVLINK_MSG_ID.REQUEST_DATA_STREAM, req);

    }

    void GeneratePacket(byte messageType, object indata)
    {
        if (!mySerialPort.IsOpen)
        {
            return;
        }            

       
            byte[] data;

            data = MavlinkUtil.StructureToByteArray(indata);

            //Console.WriteLine(DateTime.Now + " PC Doing req "+ messageType + " " + this.BytesToRead);
            byte[] packet = new byte[data.Length + 6 + 2];

            packet[0] = 254;
            packet[1] = (byte)data.Length;
            packet[2] = (byte)0; //numero de la sequence ici

            

            packet[3] = 255; // this is always 255 - MYGCS
            packet[4] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER;
            packet[5] = messageType;

            int i = 6;
            foreach (byte b in data)
            {
                packet[i] = b;
                i++;
            }

            ushort checksum = MavlinkCRC.crc_calculate(packet, packet[1] + 6);

            checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_CRCS[messageType], checksum);

            byte ck_a = (byte)(checksum & 0xFF); ///< High byte
            byte ck_b = (byte)(checksum >> 8); ///< Low byte

            packet[i] = ck_a;
            i += 1;
            packet[i] = ck_b;
            i += 1;

            if (mySerialPort.IsOpen)
            {
                mySerialPort.Write(packet, 0, i);
            }                

    }