public async Task Load(string file, ProgressUtility progress) { this.file = file; this.startTime = DateTime.MinValue; this.duration = TimeSpan.Zero; bool first = true; // QT time is milliseconds from the following epoch. byte[] msgBuf = new byte[256]; GCHandle handle = GCHandle.Alloc(msgBuf, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); DateTime lastTime = DateTime.MinValue; await Task.Run(() => { // MAVLINK_MSG_ID using (Stream s = File.OpenRead(file)) { BinaryReader reader = new BinaryReader(s); while (s.Position < s.Length) { progress.ShowProgress(0, s.Length, s.Position); try { MavLinkMessage header = new MavLinkMessage(); header.ReadHeader(reader); while (!header.IsValid()) { // hmm. looks like a bad record, so now what? header.ShiftHeader(reader); } int read = s.Read(msgBuf, 0, header.Length); if (read == header.Length) { header.Crc = reader.ReadUInt16(); if (!header.IsValidCrc(msgBuf, read)) { continue; } Type msgType = MAVLink.MAVLINK_MESSAGE_INFO[(int)header.MsgId]; if (msgType != null) { // convert to proper mavlink structure. header.TypedPayload = Marshal.PtrToStructure(ptr, msgType); } Message message = AddMessage(header); if (first) { startTime = message.Timestamp; first = false; } } } catch { // try and continue... } } } handle.Free(); }); this.duration = lastTime - startTime; }
public async Task Load(string file, ProgressUtility progress) { this.file = file; this.startTime = DateTime.MinValue; this.duration = TimeSpan.Zero; bool first = true; // QT time is milliseconds from the following epoch. byte[] msgBuf = new byte[256]; GCHandle handle = GCHandle.Alloc(msgBuf, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); DateTime lastTime = DateTime.MinValue; await Task.Run(() => { long length = 0; // MAVLINK_MSG_ID using (Stream s = File.OpenRead(file)) { length = s.Length; BinaryReader reader = new BinaryReader(s); while (s.Position < length) { progress.ShowProgress(0, length, s.Position); try { MavLinkMessage header = new MavLinkMessage(); header.ReadHeader(reader); int read = s.Read(msgBuf, 0, header.Length); if (read == header.Length) { header.Crc = reader.ReadUInt16(); bool checkCrc = true; if ((int)header.MsgId == MAVLink.mavlink_telemetry.MessageId) { if (header.Crc == 0) // telemetry has no crc. { checkCrc = false; continue; } } if (checkCrc && !header.IsValidCrc(msgBuf, read)) { continue; } Type msgType = MavLinkMessage.GetMavlinkType((uint)header.MsgId); if (msgType != null) { // convert to proper mavlink structure. header.TypedPayload = Marshal.PtrToStructure(ptr, msgType); } Message message = AddMessage(header); if (first) { startTime = message.Timestamp; startTicks = message.Ticks; first = false; } } } catch { // try and continue... } } } progress.ShowProgress(0, length, length); handle.Free(); }); this.duration = lastTime - startTime; }