Skip to content

Commit

Permalink
MAVLinkInterface: logdownload mod
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Aug 12, 2024
1 parent 12a7456 commit 0fdf550
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ExtLibs/ArduPilot/Mavlink/MAVLinkInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -5885,7 +5886,7 @@ public async Task<string> GetLog(byte sysid, byte compid, ushort no)
Hashtable set = new Hashtable();

giveComport = true;
MAVLinkMessage buffer;
MAVLinkMessage buffer = MAVLinkMessage.Invalid;

if (Progress != null)
{
Expand All @@ -5912,6 +5913,13 @@ public async Task<string> GetLog(byte sysid, byte compid, ushort no)
DateTime start = DateTime.Now;
int retrys = 3;

ConcurrentQueue<MAVLinkMessage> queue = new ConcurrentQueue<MAVLinkMessage>();
EventHandler<MAVLinkMessage> handler = (sender, msg) =>
{
queue.Enqueue(msg);
};
OnPacketReceived += handler;

while (true)
{
if (!(start.AddMilliseconds(3000) > DateTime.Now))
Expand All @@ -5926,11 +5934,13 @@ public async Task<string> GetLog(byte sysid, byte compid, ushort no)
}

giveComport = false;
OnPacketReceived -= handler;
throw new TimeoutException("Timeout on read - GetLog");
}

var start1 = DateTime.Now;
buffer = await readPacketAsync().ConfigureAwait(false);
if (!queue.TryDequeue(out buffer))
Thread.Sleep(10);
var end = DateTime.Now - start1;
var lapse = end.TotalMilliseconds;
//Console.WriteLine("readPacketAsync: " + lapse);
Expand Down Expand Up @@ -5994,6 +6004,7 @@ public async Task<string> GetLog(byte sysid, byte compid, ushort no)
if (totallength == ms.Length && set.Count >= ((totallength) / 90 + 1))
{
giveComport = false;
OnPacketReceived -= handler;
return filename;
}

Expand Down Expand Up @@ -6023,7 +6034,8 @@ public async Task<string> GetLog(byte sysid, byte compid, ushort no)
}
}

buffer = await readPacketAsync().ConfigureAwait(false);
if (!queue.TryDequeue(out buffer))
Thread.Sleep(10);
if (buffer.Length > 5)
{
if (buffer.msgid == (byte) MAVLINK_MSG_ID.LOG_DATA && buffer.sysid == req.target_system &&
Expand Down Expand Up @@ -6076,6 +6088,7 @@ public async Task<string> GetLog(byte sysid, byte compid, ushort no)
}
}

OnPacketReceived -= handler;
throw new Exception("Failed to get log");
}
}
Expand Down

0 comments on commit 0fdf550

Please sign in to comment.