Need help to read altitude Value using C#

Hello guys i’m trying to read altitude attitude using C#, even if i sent a request the only think i receive is the heartbeat.
can someone help please.
it have been 1 week i’m working on that.
this is my code check it please ?

    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()
    private void OpenSerialPort()

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

            mySerialPort.DataReceived += SerialPortDataReceived;
        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);

    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)

            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;

            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);


Where did you define this?

in the Mavlink bibliotheque

Disclaimer: I’m no expert in any way.

But, when I look at the code that you’ve shown, I don’t see anywhere where you actually SEND the request…

You create your req variable. You call your GeneratePacket function. but then you never SEND the packet. You have a Serial.ReadByte(), but you never did a WriteByte() command. So i would expect that the only thing you’re getting back is the heartbeat.

Am I missing something?

Their is a serial.write in the last ligne of code in the generate function

Hmm yup. The code widget on the forum shows it as commented out.

1 Like

Should that be i+1?

Acording to MSDN:

Type: System.Int32
The number of bytes to write.

It’s a 0 index array, so you dont have 6 bytes, you have 7.

No, i have 6 byte as shown in the mavlink message bellow
6 bytes header
0. message header, always 0xFE

  1. message length (9)
  2. sequence number – rolls around from 255 to 0 (0x4e, previous was 0x4d)
  3. System ID - what system is sending this message (1)
  4. Component ID- what component of the system is sending the message (1)
  5. Message ID (e.g. 0 = heartbeat and many more! Don’t be shy, you can add too…)

i managed to get datas From mavlink it was a miss understanding of. we can’t get just the altitude but we should get all sensors Data so unstead of doing 30 i did 0 and now i’m parsing the message to get the Id i want

but i have now a little problem with the messages. the pixhawk send messages but after a fewmoment of sending he stop sending it. and he shows me this message in the console app:
Le thread 0x2990 s’est arrêté avec le code 0 (0x0).
Le thread 0x135c s’est arrêté avec le code 0 (0x0).
Le thread 0x1a38 s’est arrêté avec le code 0 (0x0).
Le thread 0x343c s’est arrêté avec le code 0 (0x0).
Le thread 0x2f70 s’est arrêté avec le code 0 (0x0).
Le thread 0x3024 s’est arrêté avec le code 0 (0x0).
Le thread 0x2c6c s’est arrêté avec le code 0 (0x0).
Le thread 0x32ac s’est arrêté avec le code 0 (0x0).
Le thread 0x3774 s’est arrêté avec le code 0 (0x0).

do you have any idea how to correct that.

Here is a sample application that sends and receives data using C# and Mavlink

The Mavlink library is old and I will need to update it but the app works.



Tnx Man you really helped me i appreciate that .

I’m to stupid i was creating all the mavlink library by my self hahahahaha