private void BUT_bintolog_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Binary Log|*.bin"; ofd.ShowDialog(); if (File.Exists(ofd.FileName)) { List <string> log = BinaryLog.ReadLog(ofd.FileName); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "log|*.log"; DialogResult res = sfd.ShowDialog(); if (res == System.Windows.Forms.DialogResult.OK) { StreamWriter sw = new StreamWriter(sfd.OpenFile()); foreach (string line in log) { sw.Write(line); } sw.Close(); } } }
string GetLog(ushort no) { MainV2.comPort.Progress += comPort_Progress; status = serialstatus.Reading; // get df log from mav var ms = MainV2.comPort.GetLog(no); status = serialstatus.Done; updateDisplay(); MainV2.comPort.Progress -= comPort_Progress; // set log fn byte[] hbpacket = MainV2.comPort.getHeartBeat(); MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)MainV2.comPort.DebugPacket(hbpacket); logfile = MainV2.LogDir + Path.DirectorySeparatorChar + MainV2.comPort.MAV.aptype.ToString() + Path.DirectorySeparatorChar + hbpacket[3] + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + no + ".bin"; // make log dir Directory.CreateDirectory(Path.GetDirectoryName(logfile)); // save memorystream to file using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(logfile))) { bw.Write(ms.ToArray()); } // create ascii log BinaryLog.ConvertBin(logfile, logfile + ".log"); //update the new filename logfile = logfile + ".log"; // get gps time of assci log DateTime logtime = DFLog.GetFirstGpsTime(logfile); // rename log is we have a valid gps time if (logtime != DateTime.MinValue) { string newlogfilename = MainV2.LogDir + Path.DirectorySeparatorChar + MainV2.comPort.MAV.aptype.ToString() + Path.DirectorySeparatorChar + hbpacket[3] + Path.DirectorySeparatorChar + logtime.ToString("yyyy-MM-dd HH-mm-ss") + ".log"; try { File.Move(logfile, newlogfilename); // rename bin as well File.Move(logfile.Replace(".log", ""), newlogfilename.Replace(".log", ".bin")); logfile = newlogfilename; } catch { CustomMessageBox.Show("Failed to rename file " + logfile + "\nto " + newlogfilename, "Error"); } } return(logfile); }
public T this[int index] { get { // return cached value is same index if (indexcachelineno == index) { return((T)currentindexcache); } long startoffset = linestartoffset[index]; long endoffset = startoffset; if ((index + 1) >= linestartoffset.Count) { endoffset = basestream.Length; } else { endoffset = linestartoffset[index + 1]; } int length = (int)(endoffset - startoffset); if (linestartoffset[index] != basestream.Position) { basestream.Seek(linestartoffset[index], SeekOrigin.Begin); } if (binary) { var answer = BinaryLog.ReadMessage(basestream); currentindexcache = (object)answer; indexcachelineno = index; } else { byte[] data = new byte[length]; basestream.Read(data, 0, length); currentindexcache = (object)ASCIIEncoding.ASCII.GetString(data); indexcachelineno = index; } return((T)currentindexcache); } set { throw new NotImplementedException(); } }
private void BUT_bintolog_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Binary Log|*.bin"; ofd.ShowDialog(); if (File.Exists(ofd.FileName)) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "log|*.log"; DialogResult res = sfd.ShowDialog(); if (res == System.Windows.Forms.DialogResult.OK) { BinaryLog.ConvertBin(ofd.FileName, sfd.FileName); } } }
string GetLog(ushort no, string fileName) { log.Info("GetLog " + no); MainV2.comPort.Progress += comPort_Progress; status = SerialStatus.Reading; // used for log fn MAVLink.MAVLinkMessage hbpacket = MainV2.comPort.getHeartBeat(); if (hbpacket != null) { log.Info("Got hbpacket length: " + hbpacket.Length); } // get df log from mav using (var ms = MainV2.comPort.GetLog(no)) { if (ms != null) { log.Info("Got Log length: " + ms.Length); } ms.Seek(0, SeekOrigin.Begin); status = SerialStatus.Done; MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)MainV2.comPort.DebugPacket(hbpacket); logfile = Settings.Instance.LogDir + Path.DirectorySeparatorChar + MainV2.comPort.MAV.aptype.ToString() + Path.DirectorySeparatorChar + hbpacket.sysid + Path.DirectorySeparatorChar + no + " " + MakeValidFileName(fileName) + ".bin"; // make log dir Directory.CreateDirectory(Path.GetDirectoryName(logfile)); log.Info("about to write: " + logfile); // save memorystream to file using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(logfile))) { byte[] buffer = new byte[256 * 1024]; while (ms.Position < ms.Length) { int read = ms.Read(buffer, 0, buffer.Length); bw.Write(buffer, 0, read); } } } log.Info("about to convertbin: " + logfile); // create ascii log BinaryLog.ConvertBin(logfile, logfile + ".log"); //update the new filename logfile = logfile + ".log"; // rename file if needed log.Info("about to GetFirstGpsTime: " + logfile); // get gps time of assci log DateTime logtime = new DFLog().GetFirstGpsTime(logfile); // rename log is we have a valid gps time if (logtime != DateTime.MinValue) { string newlogfilename = Settings.Instance.LogDir + Path.DirectorySeparatorChar + MainV2.comPort.MAV.aptype.ToString() + Path.DirectorySeparatorChar + hbpacket.sysid + Path.DirectorySeparatorChar + logtime.ToString("yyyy-MM-dd HH-mm-ss") + ".log"; try { File.Move(logfile, newlogfilename); // rename bin as well File.Move(logfile.Replace(".log", ""), newlogfilename.Replace(".log", ".bin")); logfile = newlogfilename; } catch { CustomMessageBox.Show(Strings.ErrorRenameFile + " " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } MainV2.comPort.Progress -= comPort_Progress; return(logfile); }
public static void MapLogs(string[] logs) { foreach (var logfile in logs) { if (File.Exists(logfile + ".jpg")) { continue; } double minx = 99999; double maxx = -99999; double miny = 99999; double maxy = -99999; List <PointLatLngAlt> locs = new List <PointLatLngAlt>(); try { if (logfile.ToLower().EndsWith(".tlog")) { MAVLinkInterface mine = new MAVLinkInterface(); using (mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.Read))) { mine.logreadmode = true; CurrentState cs = new CurrentState(); while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length) { byte[] packet = mine.readPacket(); if (packet.Length < 5) { continue; } try { if (MainV2.speechEngine != null) { MainV2.speechEngine.SpeakAsyncCancelAll(); } } catch { } if (packet[5] == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT) { var loc = packet.ByteArrayToStructure <MAVLink.mavlink_global_position_int_t>(6); if (loc.lat == 0 || loc.lon == 0) { continue; } locs.Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f)); minx = Math.Min(minx, loc.lon / 10000000.0f); maxx = Math.Max(maxx, loc.lon / 10000000.0f); miny = Math.Min(miny, loc.lat / 10000000.0f); maxy = Math.Max(maxy, loc.lat / 10000000.0f); } } } } else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log")) { bool bin = logfile.ToLower().EndsWith(".bin"); using (var st = File.OpenRead(logfile)) { using (StreamReader sr = new StreamReader(st)) { while (sr.BaseStream.Position < sr.BaseStream.Length) { string line = ""; if (bin) { line = BinaryLog.ReadMessage(sr.BaseStream); } else { line = sr.ReadLine(); } if (line.StartsWith("FMT")) { DFLog.FMTLine(line); } else if (line.StartsWith("GPS")) { var item = DFLog.GetDFItemFromLine(line, 0); var lat = double.Parse(item.items[DFLog.FindMessageOffset(item.msgtype, "Lat")]); var lon = double.Parse(item.items[DFLog.FindMessageOffset(item.msgtype, "Lng")]); if (lat == 0 || lon == 0) { continue; } locs.Add(new PointLatLngAlt(lat, lon)); minx = Math.Min(minx, lon); maxx = Math.Max(maxx, lon); miny = Math.Min(miny, lat); maxy = Math.Max(maxy, lat); } } } } } if (locs.Count > 10) { // add a bit of buffer var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001); var map = GetMap(area); var grap = Graphics.FromImage(map); PointF lastpoint = new PointF(); foreach (var loc in locs) { PointF newpoint = GetPixel(area, loc, map.Size); if (!lastpoint.IsEmpty) { grap.DrawLine(Pens.Red, lastpoint, newpoint); } lastpoint = newpoint; } map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); map.Dispose(); map = null; } else { DoTextMap(logfile + ".jpg", "No gps data"); } } catch (Exception ex) { if (ex.ToString().Contains("Mavlink 0.9")) { DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9"); } continue; } } }
public static void log(string fn) { string[] filelines; if (fn.ToLower().EndsWith(".bin")) { filelines = BinaryLog.ReadLog(fn).ToArray(); } else { // read the file filelines = File.ReadAllLines(fn); } // store all the arrays List <MLArray> mlList = new List <MLArray>(); // store data to putinto the arrays Dictionary <string, List <double[]> > data = new Dictionary <string, List <double[]> >(); // store line item lengths Hashtable len = new Hashtable(); // store whats we have seen in the log Hashtable seen = new Hashtable(); // store the params seen SortedDictionary <string, double> param = new SortedDictionary <string, double>(); // keep track of line no int a = 0; foreach (var line in filelines) { a++; Console.Write(a + "\r"); string strLine = line.Replace(", ", ","); strLine = strLine.Replace(": ", ":"); string[] items = strLine.Split(',', ':'); // process the fmt messages if (line.StartsWith("FMT")) { // +1 for line no string[] names = new string[items.Length - 5 + 1]; names[0] = "LineNo"; Array.ConstrainedCopy(items, 5, names, 1, names.Length - 1); MLArray format = CreateCellArray(items[3] + "_label", names); if (items[3] == "PARM") { } else { mlList.Add(format); } len[items[3]] = names.Length; } // process param messages else if (line.StartsWith("PARM")) { try { param[items[1]] = double.Parse(items[2], CultureInfo.InvariantCulture); } catch { } }// everyting else is generic else { // make sure the line is long enough if (items.Length < 2) { continue; } // check we have a valid fmt message for this message type if (!len.ContainsKey(items[0])) { continue; } // check the fmt length matchs what the log has if (items.Length != (int)len[items[0]]) { continue; } // make it as being seen seen[items[0]] = 1; double[] dbarray = new double[items.Length]; // set line no dbarray[0] = a; for (int n = 1; n < items.Length; n++) { try { dbarray[n] = double.Parse(items[n], CultureInfo.InvariantCulture); } catch { } } if (!data.ContainsKey(items[0])) { data[items[0]] = new List <double[]>(); } data[items[0]].Add(dbarray); } } foreach (var item in data) { double[][] temp = item.Value.ToArray(); MLArray dbarray = new MLDouble(item.Key, temp); mlList.Add(dbarray); } MLCell cell = new MLCell("PARM", new int[] { param.Keys.Count, 2 }); int m = 0; foreach (var item in param.Keys) { cell[m, 0] = new MLChar(null, item.ToString()); cell[m, 1] = new MLDouble(null, new double[] { (double)param[item] }, 1); m++; } mlList.Add(cell); MLArray seenmsg = CreateCellArray("Seen", seen.Keys.Cast <string>().ToArray()); mlList.Add(seenmsg); try { MatFileWriter mfw = new MatFileWriter(fn + ".mat", mlList, true); } catch (Exception err) { MessageBox.Show("There was an error when creating the MAT-file: \n" + err.ToString(), "MAT-File Creation Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } }
private void Form1_Load(object sender, EventArgs e) { rowno = 1; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Log Files|*.log;*.bin"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.InitialDirectory = MainV2.LogDir; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { Stream stream; if (openFileDialog1.FileName.ToLower().EndsWith(".bin")) { // extract log List <string> loglines = BinaryLog.ReadLog(openFileDialog1.FileName); // convert log to memory stream; stream = new MemoryStream(); // create single string with entire log foreach (string line in loglines) { stream.Write(ASCIIEncoding.ASCII.GetBytes(line), 0, line.Length); } // back to stream start stream.Seek(0, SeekOrigin.Begin); } else { stream = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); } this.Text = "Log Browser - " + Path.GetFileName(openFileDialog1.FileName); PopulateDataTableFromUploadedFile(stream); stream.Close(); dataGridView1.DataSource = m_dtCSV; dataGridView1.Columns[0].Visible = false; } catch (Exception ex) { CustomMessageBox.Show("Failed to read File: " + ex.ToString()); } foreach (DataGridViewColumn column in dataGridView1.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } DrawMap(); CreateChart(zg1); } else { return; } }
string GetLog(string no, string fileName) { log.Info("GetLog " + no); status = SerialStatus.Reading; logfile = Settings.Instance.LogDir + Path.DirectorySeparatorChar + MakeValidFileName(fileName) + ".bin"; // make log dir Directory.CreateDirectory(Path.GetDirectoryName(logfile)); log.Info("about to write: " + logfile); // save memorystream to file SftpClient client = new SftpClient(_connectionInfo); client.Connect(); using (var logstream = File.Open(logfile, FileMode.Create, FileAccess.Write)) { client.DownloadFile(no, logstream, downloadCallback); } client.Disconnect(); log.Info("about to convertbin: " + logfile); // create ascii log BinaryLog.ConvertBin(logfile, logfile + ".log"); //update the new filename logfile = logfile + ".log"; // rename file if needed log.Info("about to GetFirstGpsTime: " + logfile); // get gps time of assci log DateTime logtime = new DFLog().GetFirstGpsTime(logfile); // rename log is we have a valid gps time if (logtime != DateTime.MinValue) { string newlogfilename = Settings.Instance.LogDir + Path.DirectorySeparatorChar + logtime.ToString("yyyy-MM-dd HH-mm-ss") + ".log"; try { File.Move(logfile, newlogfilename); // rename bin as well File.Move(logfile.Replace(".log", ""), newlogfilename.Replace(".log", ".bin")); logfile = newlogfilename; } catch { CustomMessageBox.Show(Strings.ErrorRenameFile + " " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } MainV2.comPort.Progress -= comPort_Progress; return(logfile); }
private void Form1_Load(object sender, EventArgs e) { Hashtable seenmessagetypes = new Hashtable(); OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Log Files|*.log;*.bin"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.InitialDirectory = MainV2.LogDir; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { Stream stream; if (openFileDialog1.FileName.ToLower().EndsWith(".bin")) { log.Info("before " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); // extract log - converts to assci lines var loglines = BinaryLog.ReadLog(openFileDialog1.FileName); log.Info("loglines " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); GC.Collect(); log.Info("loglines2 " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); // create temp file string tempfile = Path.GetTempFileName(); log.Info("temp file " + tempfile); stream = File.Open(tempfile, FileMode.Create, FileAccess.ReadWrite, FileShare.Delete); // save ascii lines to file foreach (string line in loglines) { stream.Write(ASCIIEncoding.ASCII.GetBytes(line), 0, line.Length); } stream.Flush(); // back to stream start stream.Seek(0, SeekOrigin.Begin); loglines.Clear(); loglines = null; // force memory reclaim after loglines clear GC.Collect(); log.Info("loglines.clear " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } else { stream = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); } logdata = DFLog.ReadLog(stream); this.Text = "Log Browser - " + Path.GetFileName(openFileDialog1.FileName); log.Info("about to create DataTable " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); m_dtCSV = new DataTable(); log.Info("process to datagrid " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); bool largelog = logdata.Count > 500000 ? true : false; foreach (var item in logdata) { if (item.items != null) { while (m_dtCSV.Columns.Count < (item.items.Length + typecoloum)) { m_dtCSV.Columns.Add(); } seenmessagetypes[item.msgtype] = ""; if (largelog) { continue; } DataRow dr = m_dtCSV.NewRow(); dr[0] = item.lineno; dr[1] = item.time.ToString("yyyy-MM-dd HH:mm:ss.fff"); for (int a = 0; a < item.items.Length; a++) { dr[a + typecoloum] = item.items[a]; } m_dtCSV.Rows.Add(dr); } } log.Info("Done " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); //PopulateDataTableFromUploadedFile(stream); stream.Close(); log.Info("set dgv datasourse " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); if (MainV2.MONO) { //if (m_dtCSV.Rows.Count > 5000) // { // CustomMessageBox.Show("This log apears to be a large log, the grid view will be disabled.\nAll graphing will still work however", "Large Log"); // dataGridView1.Visible = false; // } // else { BindingSource bs = new BindingSource(); bs.DataSource = m_dtCSV; dataGridView1.DataSource = bs; } } else { dataGridView1.VirtualMode = true; dataGridView1.RowCount = logdata.Count; dataGridView1.ColumnCount = m_dtCSV.Columns.Count; } dataGridView1.Columns[0].Visible = false; log.Info("datasource set " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } catch (Exception ex) { CustomMessageBox.Show("Failed to read File: " + ex.ToString()); } foreach (DataGridViewColumn column in dataGridView1.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } CreateChart(zg1); ResetTreeView(seenmessagetypes); if (DFLog.logformat.Count == 0) { CustomMessageBox.Show("Log Browse will not function correctly without FMT messages in your log.\nThese appear to be missing from your log.", "Error"); this.Close(); return; } } else { this.Close(); return; } }
public static void MapLogs(string[] logs) { foreach (var logfile in logs) { if (File.Exists(logfile + ".jpg")) continue; double minx = 99999; double maxx = -99999; double miny = 99999; double maxy = -99999; List<PointLatLngAlt> locs = new List<PointLatLngAlt>(); try { if (logfile.ToLower().EndsWith(".tlog")) { using (MAVLinkInterface mine = new MAVLinkInterface()) using (mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { mine.logreadmode = true; CurrentState cs = new CurrentState(); while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length) { byte[] packet = mine.readPacket(); if (packet.Length < 5) continue; try { if (MainV2.speechEngine != null) MainV2.speechEngine.SpeakAsyncCancelAll(); } catch { } if (packet[5] == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT) { var loc = packet.ByteArrayToStructure<MAVLink.mavlink_global_position_int_t>(6); if (loc.lat == 0 || loc.lon == 0) continue; locs.Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f)); minx = Math.Min(minx, loc.lon / 10000000.0f); maxx = Math.Max(maxx, loc.lon / 10000000.0f); miny = Math.Min(miny, loc.lat / 10000000.0f); maxy = Math.Max(maxy, loc.lat / 10000000.0f); } } } } else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log")) { bool bin = logfile.ToLower().EndsWith(".bin"); BinaryLog binlog = new BinaryLog(); DFLog dflog = new DFLog(); using (var st = File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(st)) { while (sr.BaseStream.Position < sr.BaseStream.Length) { string line = ""; if (bin) { line = binlog.ReadMessage(sr.BaseStream); } else { line = sr.ReadLine(); } if (line.StartsWith("FMT")) { dflog.FMTLine(line); } else if (line.StartsWith("GPS")) { var item = dflog.GetDFItemFromLine(line, 0); var lat = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lat")]); var lon = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lng")]); if (lat == 0 || lon == 0) continue; locs.Add(new PointLatLngAlt(lat, lon)); minx = Math.Min(minx, lon); maxx = Math.Max(maxx, lon); miny = Math.Min(miny, lat); maxy = Math.Max(maxy, lat); } } } } } if (locs.Count > 10) { // add a bit of buffer var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001); var map = GetMap(area); var grap = Graphics.FromImage(map); PointF lastpoint = new PointF(); foreach (var loc in locs) { PointF newpoint = GetPixel(area, loc, map.Size); if (!lastpoint.IsEmpty) grap.DrawLine(Pens.Red, lastpoint, newpoint); lastpoint = newpoint; } map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); map.Dispose(); map = null; } else { DoTextMap(logfile + ".jpg", "No gps data"); } } catch (Exception ex) { if (ex.ToString().Contains("Mavlink 0.9")) DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9"); continue; } } }
public static void MapLogs(string[] logs) { foreach (var logfile in logs) { if (File.Exists(logfile + ".jpg")) { continue; } double minx = 99999; double maxx = -99999; double miny = 99999; double maxy = -99999; Dictionary <int, List <PointLatLngAlt> > loc_list = new Dictionary <int, List <PointLatLngAlt> >(); try { if (logfile.ToLower().EndsWith(".tlog")) { using (MAVLinkInterface mine = new MAVLinkInterface()) using ( mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) ) { mine.logreadmode = true; while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length) { MAVLink.MAVLinkMessage packet = mine.readPacket(); if (packet.Length < 5) { continue; } try { if (MainV2.speechEngine != null) { MainV2.speechEngine.SpeakAsyncCancelAll(); } } catch { } if (packet.msgid == (byte)MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT) { var loc = packet.ToStructure <MAVLink.mavlink_global_position_int_t>(); if (loc.lat == 0 || loc.lon == 0) { continue; } var id = MAVList.GetID(packet.sysid, packet.compid); if (!loc_list.ContainsKey(id)) { loc_list[id] = new List <PointLatLngAlt>(); } loc_list[id].Add(new PointLatLngAlt(loc.lat / 10000000.0f, loc.lon / 10000000.0f)); minx = Math.Min(minx, loc.lon / 10000000.0f); maxx = Math.Max(maxx, loc.lon / 10000000.0f); miny = Math.Min(miny, loc.lat / 10000000.0f); maxy = Math.Max(maxy, loc.lat / 10000000.0f); } } } } else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log")) { bool bin = logfile.ToLower().EndsWith(".bin"); BinaryLog binlog = new BinaryLog(); DFLog dflog = new DFLog(); using (var st = File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(st)) { loc_list[0] = new List <PointLatLngAlt>(); while (sr.BaseStream.Position < sr.BaseStream.Length) { string line = ""; if (bin) { line = binlog.ReadMessage(sr.BaseStream); } else { line = sr.ReadLine(); } if (line.StartsWith("FMT")) { dflog.FMTLine(line); } else if (line.StartsWith("GPS")) { var item = dflog.GetDFItemFromLine(line, 0); var lat = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lat")]); var lon = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lng")]); if (lat == 0 || lon == 0) { continue; } loc_list[0].Add(new PointLatLngAlt(lat, lon)); minx = Math.Min(minx, lon); maxx = Math.Max(maxx, lon); miny = Math.Min(miny, lat); maxy = Math.Max(maxy, lat); } } } } } if (loc_list.First().Value.Count > 10) { // add a bit of buffer var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001); var map = GetMap(area); var grap = Graphics.FromImage(map); Color[] colours = { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet, Color.Pink }; int a = 0; foreach (var locs in loc_list.Values) { PointF lastpoint = new PointF(); var pen = new Pen(colours[a % (colours.Length - 1)]); foreach (var loc in locs) { PointF newpoint = GetPixel(area, loc, map.Size); if (!lastpoint.IsEmpty) { grap.DrawLine(pen, lastpoint, newpoint); } lastpoint = newpoint; } a++; } map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); map.Dispose(); map = null; } else { DoTextMap(logfile + ".jpg", "No gps data"); } } catch (Exception ex) { if (ex.ToString().Contains("Mavlink 0.9")) { DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9"); } continue; } } }
public static void ProcessLog(string fn) { StreamReader sr; if (fn.ToLower().EndsWith(".bin")) { string tmpfile = Path.GetTempFileName(); BinaryLog.ConvertBin(fn, tmpfile); sr = new StreamReader(tmpfile); } else { sr = new StreamReader(fn); } // store all the arrays List <MLArray> mlList = new List <MLArray>(); // store data to putinto the arrays Dictionary <string, DoubleList> data = new Dictionary <string, DoubleList>(); // store line item lengths Hashtable len = new Hashtable(); // store whats we have seen in the log Hashtable seen = new Hashtable(); // store the params seen SortedDictionary <string, double> param = new SortedDictionary <string, double>(); // keep track of line no int a = 0; log.Info("ProcessLog start " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); while (!sr.EndOfStream) { var line = sr.ReadLine(); a++; if (a % 100 == 0) { Console.Write(a + "\r"); } string strLine = line.Replace(", ", ","); strLine = strLine.Replace(": ", ":"); string[] items = strLine.Split(',', ':'); // process the fmt messages if (line.StartsWith("FMT")) { // +1 for line no string[] names = new string[items.Length - 5 + 1]; names[0] = "LineNo"; Array.ConstrainedCopy(items, 5, names, 1, names.Length - 1); MLArray format = CreateCellArray(items[3] + "_label", names); if (items[3] == "PARM") { } else { mlList.Add(format); } len[items[3]] = names.Length; } // process param messages else if (line.StartsWith("PARM")) { try { param[items[2]] = double.Parse(items[3], CultureInfo.InvariantCulture); } catch { } } // everyting else is generic else { // make sure the line is long enough if (items.Length < 2) { continue; } // check we have a valid fmt message for this message type if (!len.ContainsKey(items[0])) { continue; } // check the fmt length matchs what the log has if (items.Length != (int)len[items[0]]) { continue; } // make it as being seen seen[items[0]] = 1; double[] dbarray = new double[items.Length]; // set line no dbarray[0] = a; for (int n = 1; n < items.Length; n++) { double dbl = 0; double.TryParse(items[n], NumberStyles.Any, CultureInfo.InvariantCulture, out dbl); dbarray[n] = dbl; } if (!data.ContainsKey(items[0])) { data[items[0]] = new DoubleList(); } data[items[0]].Add(dbarray); } // split at x records if (a % 2000000 == 0) { GC.Collect(); DoWrite(fn + "-" + a, data, param, mlList, seen); mlList.Clear(); data.Clear(); param.Clear(); seen.Clear(); GC.Collect(); } } DoWrite(fn + "-" + a, data, param, mlList, seen); sr.Close(); }
string GetLog(ushort no, string fileName) { log.Info("GetLog " + no); MainV2.comPort.Progress += comPort_Progress; status = SerialStatus.Reading; // used for log fn byte[] hbpacket = MainV2.comPort.getHeartBeat(); if (hbpacket != null) { log.Info("Got hbpacket length: " + hbpacket.Length); } // get df log from mav using (var ms = MainV2.comPort.GetLog(no)) { if (ms != null) { log.Info("Got Log length: " + ms.Length); } ms.Seek(0, SeekOrigin.Begin); status = SerialStatus.Done; MAVLink.mavlink_heartbeat_t hb = (MAVLink.mavlink_heartbeat_t)MainV2.comPort.DebugPacket(hbpacket); logfile = Settings.Instance.LogDir + Path.DirectorySeparatorChar + MainV2.comPort.MAV.aptype.ToString() + Path.DirectorySeparatorChar + hbpacket[3] + Path.DirectorySeparatorChar + no + " " + MakeValidFileName(fileName) + ".bin"; // make log dir Directory.CreateDirectory(Path.GetDirectoryName(logfile)); log.Info("about to write: " + logfile); // save memorystream to file using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(logfile))) { byte[] buffer = new byte[256 * 1024]; while (ms.Position < ms.Length) { int read = ms.Read(buffer, 0, buffer.Length); bw.Write(buffer, 0, read); } } } log.Info("about to convertbin: " + logfile); // create ascii log BinaryLog.ConvertBin(logfile, logfile + ".log"); //update the new filename logfile = logfile + ".log"; MainV2.comPort.Progress -= comPort_Progress; return(logfile); }
public static void MapLogs(string[] logs) { foreach (var logfile in logs) { if (File.Exists(logfile + ".jpg")) continue; double minx = 99999; double maxx = -99999; double miny = 99999; double maxy = -99999; bool sitl = false; Dictionary<int,List<PointLatLngAlt>> loc_list = new Dictionary<int, List<PointLatLngAlt>>(); try { if (logfile.ToLower().EndsWith(".tlog")) { using (MAVLinkInterface mine = new MAVLinkInterface()) using ( mine.logplaybackfile = new BinaryReader(File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) ) { mine.logreadmode = true; mine.speechenabled = false; while (mine.logplaybackfile.BaseStream.Position < mine.logplaybackfile.BaseStream.Length) { MAVLink.MAVLinkMessage packet = mine.readPacket(); if (packet.Length < 5) continue; if (packet.msgid == (byte)MAVLink.MAVLINK_MSG_ID.SIM_STATE || packet.msgid == (byte)MAVLink.MAVLINK_MSG_ID.SIMSTATE) { sitl = true; } if (packet.msgid == (byte) MAVLink.MAVLINK_MSG_ID.GLOBAL_POSITION_INT) { var loc = packet.ToStructure<MAVLink.mavlink_global_position_int_t>(); if (loc.lat == 0 || loc.lon == 0) continue; var id = MAVList.GetID(packet.sysid, packet.compid); if (!loc_list.ContainsKey(id)) loc_list[id] = new List<PointLatLngAlt>(); loc_list[id].Add(new PointLatLngAlt(loc.lat/10000000.0f, loc.lon/10000000.0f)); minx = Math.Min(minx, loc.lon/10000000.0f); maxx = Math.Max(maxx, loc.lon/10000000.0f); miny = Math.Min(miny, loc.lat/10000000.0f); maxy = Math.Max(maxy, loc.lat/10000000.0f); } } } } else if (logfile.ToLower().EndsWith(".bin") || logfile.ToLower().EndsWith(".log")) { bool bin = logfile.ToLower().EndsWith(".bin"); BinaryLog binlog = new BinaryLog(); DFLog dflog = new DFLog(); using (var st = File.Open(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(st)) { loc_list[0] = new List<PointLatLngAlt>(); while (sr.BaseStream.Position < sr.BaseStream.Length) { string line = ""; if (bin) { line = binlog.ReadMessage(sr.BaseStream); } else { line = sr.ReadLine(); } if (line.StartsWith("FMT")) { dflog.FMTLine(line); } else if (line.StartsWith("GPS")) { var item = dflog.GetDFItemFromLine(line, 0); if (!dflog.logformat.ContainsKey("GPS")) continue; var status = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Status")]); var lat = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lat")]); var lon = double.Parse(item.items[dflog.FindMessageOffset(item.msgtype, "Lng")]); if (lat == 0 || lon == 0 || status < 3) continue; loc_list[0].Add(new PointLatLngAlt(lat, lon)); minx = Math.Min(minx, lon); maxx = Math.Max(maxx, lon); miny = Math.Min(miny, lat); maxy = Math.Max(maxy, lat); } } } } } if (loc_list.Count > 0 && loc_list.First().Value.Count > 10) { // add a bit of buffer var area = RectLatLng.FromLTRB(minx - 0.001, maxy + 0.001, maxx + 0.001, miny - 0.001); var map = GetMap(area); var grap = Graphics.FromImage(map); if (sitl) { AddTextToMap(grap, "SITL"); } Color[] colours = { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet, Color.Pink }; int a = 0; foreach (var locs in loc_list.Values) { PointF lastpoint = new PointF(); var pen = new Pen(colours[a%(colours.Length - 1)]); foreach (var loc in locs) { PointF newpoint = GetPixel(area, loc, map.Size); if (!lastpoint.IsEmpty) grap.DrawLine(pen, lastpoint, newpoint); lastpoint = newpoint; } a++; } map.Save(logfile + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); map.Dispose(); map = null; } else { DoTextMap(logfile + ".jpg", "No gps data"); } } catch (Exception ex) { if (ex.ToString().Contains("Mavlink 0.9")) DoTextMap(logfile + ".jpg", "Old log\nMavlink 0.9"); continue; } } }
private void Form1_Load(object sender, EventArgs e) { rowno = 1; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Log Files|*.log;*.bin"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.InitialDirectory = MainV2.LogDir; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { Stream stream; if (openFileDialog1.FileName.ToLower().EndsWith(".bin")) { // extract log List <string> loglines = BinaryLog.ReadLog(openFileDialog1.FileName); // convert log to memory stream; stream = new MemoryStream(); // create single string with entire log foreach (string line in loglines) { stream.Write(ASCIIEncoding.ASCII.GetBytes(line), 0, line.Length); } // back to stream start stream.Seek(0, SeekOrigin.Begin); } else { stream = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); } var logdata = DFLog.ReadLog(stream); this.Text = "Log Browser - " + Path.GetFileName(openFileDialog1.FileName); m_dtCSV = new DataTable(); log.Info("process to datagrid"); foreach (var item in logdata) { if (item.items != null) { while (m_dtCSV.Columns.Count < (item.items.Length + typecoloum)) { m_dtCSV.Columns.Add(); } DataRow dr = m_dtCSV.NewRow(); dr[0] = item.lineno; dr[1] = item.time.ToString("yyyy-MM-dd HH:mm:ss.fff"); for (int a = 0; a < item.items.Length; a++) { dr[a + typecoloum] = item.items[a]; } m_dtCSV.Rows.Add(dr); } } log.Info("Done"); //PopulateDataTableFromUploadedFile(stream); stream.Close(); log.Info("set dgv datasourse"); dataGridView1.DataSource = m_dtCSV; dataGridView1.Columns[0].Visible = false; log.Info("datasource set"); } catch (Exception ex) { CustomMessageBox.Show("Failed to read File: " + ex.ToString()); } foreach (DataGridViewColumn column in dataGridView1.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } log.Info("Get map"); DrawMap(); log.Info("map done"); CreateChart(zg1); } else { this.Close(); return; } }