Optimize receiption of Mavlink Message

Hello, I would like to optimize the reception of different Mavlink message in my plugin for Mission Planner. I can receive these messaegs, but i need to do send them multiple times to receive correctly one (in my plugin, because in the mavlink inspector i receive every messages. The problem is in my plugin).
I’m doing like this :

MAVLink.MAVLinkMessage mavMes = Host.comPort.readPacket();
if (mavMes.msgid == 0)
{
mavlink_heartbeat_t hb = mavMes.ToStructure<mavlink_heartbeat_t>();

if ((hb.type == (byte)MAV_TYPE.GIMBAL) && (connection_allowed_gimbal))
{
    // if we have front = 0, mean that the option display virtual button has been choosen in the toolstrip menu
    if (front == 0)  // we do it like this to avoid problem linked to popup in background/frontground
    {
        front = 1;
        value_slider = slider.Value;
        StartFormThread2();
        StartFormThread();
    }
    if (state == 0)
    {
        CustomMessageBox.Show("Gimbal Heartbeat Detected ");
        Host.comPort.getHeartBeat();
        StartButtonThread();
        StartFormThread2();
        StartFormThread();
        state = 1;
    }
    iStart = Environment.TickCount;
}
if ((Environment.TickCount - iStart > 20000) && (state == 1))
{
    state = 0;
    if (("" + Host.comPort).Contains("Ice") == false) // Mavlink still Connected
    {
        CustomMessageBox.Show("Gimbal Heartbeat not detected anymore");
    }
}

}

if (mavMes.msgid == 47)
{
/* Do actions */
CustomMessageBox.Show("msgID : " + mavMes.msgid);
}

if (mavMes.msgid == 253)
{
mavlink_statustext_t statusText = mavMes.ToStructure<mavlink_statustext_t>();
string s = Encoding.ASCII.GetString(statusText.text);
CustomMessageBox.Show("Status Text : " + s);
}

if (((“” + Host.comPort).Contains(“on Ice”)) && (connection_allowed_gimbal)) // Mavlink deconnected, we don’t want gimbal be allowed
{
connection_allowed_gimbal = false;
}

My first idea was to put every reception into thread to parallelize them :

        MAVLink.MAVLinkMessage mavMes = Host.comPort.readPacket();

        if (mavMes.msgid == 0)
        {
            StartThreadReceiveHeartbeat();
        }
        else if(mavMes.msgid == 253)
        {
            StartThreadReceiveStatusText();
        }
        else if (mavMes.msgid == 287)
        {
            StartThreadReceivePitchYaw();
        }

        if ((("" + Host.comPort).Contains("on Ice")) && (connection_allowed_gimbal)) // Mavlink deconnected, we don't want gimbal be allowed
        {
            connection_allowed_gimbal = false;
        }

But the result is worst. So i’m open to proposal.
Thank you.

I think you do it wrong.
Have to subscribe to the packet received event. It make sure that you got every packet that is received.

            foreach (var port in MainV2.Comports)
            {
                port.OnPacketReceived += receivedMsgPacket;
            }
     private void receivedMsgPacket(object sender, MAVLink.MAVLinkMessage mavMsg)
        {
            if (mavMsg.msgid == (uint)MAVLink.MAVLINK_MSG_ID.STATUSTEXT)
            { .....
1 Like

It works perfectly thank you!