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 = new 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(Strings.ErrorRenameFile+ " " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } return logfile; }
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); }
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { while (comPort.BytesToRead > 0 && threadrun) { updateDisplay(); string line = ""; comPort.ReadTimeout = 500; try { line = comPort.ReadLine(); //readline(comPort); if (!line.Contains("\n")) line = line + "\n"; } catch { line = comPort.ReadExisting(); //byte[] data = readline(comPort); //line = Encoding.ASCII.GetString(data, 0, data.Length); } receivedbytes += line.Length; //string line = Encoding.ASCII.GetString(data, 0, data.Length); switch (status) { case serialstatus.Connecting: if (line.Contains("ENTER") || line.Contains("GROUND START") || line.Contains("reset to FLY") || line.Contains("interactive setup") || line.Contains("CLI") || line.Contains("Ardu")) { try { //System.Threading.Thread.Sleep(200); //comPort.Write("\n\n\n\n"); } catch { } System.Threading.Thread.Sleep(500); // clear history this.Invoke((System.Windows.Forms.MethodInvoker) delegate() { TXT_seriallog.Clear(); }); // comPort.Write("logs\r"); status = serialstatus.Done; } break; case serialstatus.Closefile: sw.Close(); DateTime logtime = new DFLog().GetFirstGpsTime(logfile); if (logtime != DateTime.MinValue) { string newlogfilename = MainV2.LogDir + Path.DirectorySeparatorChar + logtime.ToString("yyyy-MM-dd HH-mm-ss") + ".log"; try { File.Move(logfile, newlogfilename); logfile = newlogfilename; } catch (Exception ex) { log.Error(ex); CustomMessageBox.Show( "Failed to rename file " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } TextReader tr = new StreamReader(logfile); // this.Invoke( (System.Windows.Forms.MethodInvoker) delegate() { TXT_seriallog.AppendText("Creating KML for " + logfile); }); LogOutput lo = new LogOutput(); while (tr.Peek() != -1) { lo.processLine(tr.ReadLine()); } tr.Close(); try { lo.writeKML(logfile + ".kml"); } catch { } // usualy invalid lat long error status = serialstatus.Done; comPort.DiscardInBuffer(); break; case serialstatus.Createfile: receivedbytes = 0; Directory.CreateDirectory(MainV2.LogDir); logfile = MainV2.LogDir + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + currentlog + ".log"; sw = new StreamWriter(logfile); status = serialstatus.Waiting; lock (thisLock) { this.Invoke((System.Windows.Forms.MethodInvoker) delegate() { TXT_seriallog.Clear(); }); } //if (line.Contains("Dumping Log")) { status = serialstatus.Reading; } break; case serialstatus.Done: // // if (line.Contains("start") && line.Contains("end")) { Regex regex2 = new Regex(@"^Log ([0-9]+)[,\s]", RegexOptions.IgnoreCase); if (regex2.IsMatch(line)) { MatchCollection matchs = regex2.Matches(line); logcount = int.Parse(matchs[0].Groups[1].Value); genchkcombo(logcount); //status = serialstatus.Done; } } if (line.Contains("No logs")) { status = serialstatus.Done; } break; case serialstatus.Reading: if (line.Contains("packets read") || line.Contains("Done") || line.Contains("logs enabled")) { status = serialstatus.Closefile; Console.Write("CloseFile: " + line); break; } sw.Write(line); continue; case serialstatus.Waiting: if (line.Contains("Dumping Log") || line.Contains("GPS:") || line.Contains("NTUN:") || line.Contains("CTUN:") || line.Contains("PM:")) { status = serialstatus.Reading; Console.Write("Reading: " + line); } break; } lock (thisLock) { this.BeginInvoke((MethodInvoker) delegate() { Console.Write(line); TXT_seriallog.AppendText(line.Replace((char) 0x0, ' ')); // auto scroll if (TXT_seriallog.TextLength >= 10000) { TXT_seriallog.Text = TXT_seriallog.Text.Substring(TXT_seriallog.TextLength/2); } TXT_seriallog.SelectionStart = TXT_seriallog.Text.Length; TXT_seriallog.ScrollToCaret(); TXT_seriallog.Refresh(); }); } } //log.Info("exit while"); } catch (Exception ex) { CustomMessageBox.Show("Error reading data" + ex.ToString()); } }
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 = new 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(Strings.ErrorRenameFile + " " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } return(logfile); }
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); }
void comPort_DataReceived(object sender, object e) { try { while (comPort.BytesToRead > 0 && threadrun) { updateDisplay(); string line = ""; comPort.ReadTimeout = 500; try { line = comPort.ReadLine(); //readline(comPort); if (!line.Contains("\n")) { line = line + "\n"; } } catch { line = comPort.ReadExisting(); //byte[] data = readline(comPort); //line = Encoding.ASCII.GetString(data, 0, data.Length); } receivedbytes += line.Length; //string line = Encoding.ASCII.GetString(data, 0, data.Length); switch (status) { case serialstatus.Connecting: if (line.Contains("ENTER") || line.Contains("GROUND START") || line.Contains("reset to FLY") || line.Contains("interactive setup") || line.Contains("CLI") || line.Contains("Ardu")) { try { //System.Threading.Thread.Sleep(200); //comPort.Write("\n\n\n\n"); } catch { } System.Threading.Thread.Sleep(500); // clear history this.Invoke((System.Windows.Forms.MethodInvoker) delegate() { TXT_seriallog.Clear(); }); // comPort.Write("logs\r"); status = serialstatus.Done; } break; case serialstatus.Closefile: sw.Close(); DateTime logtime = new DFLog(null).GetFirstGpsTime(logfile); 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); logfile = newlogfilename; } catch (Exception ex) { log.Error(ex); CustomMessageBox.Show( "Failed to rename file " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } TextReader tr = new StreamReader(logfile); // this.Invoke( (System.Windows.Forms.MethodInvoker) delegate() { TXT_seriallog.AppendText("Creating KML for " + logfile); }); LogOutput lo = new LogOutput(); while (tr.Peek() != -1) { lo.processLine(tr.ReadLine()); } tr.Close(); try { lo.writeKML(logfile + ".kml"); } catch { } // usualy invalid lat long error status = serialstatus.Done; comPort.DiscardInBuffer(); break; case serialstatus.Createfile: receivedbytes = 0; Directory.CreateDirectory(Settings.Instance.LogDir); logfile = Settings.Instance.LogDir + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + currentlog + ".log"; sw = new StreamWriter(logfile); status = serialstatus.Waiting; lock (thisLock) { this.Invoke((System.Windows.Forms.MethodInvoker) delegate() { TXT_seriallog.Clear(); }); } //if (line.Contains("Dumping Log")) { status = serialstatus.Reading; } break; case serialstatus.Done: // // if (line.Contains("start") && line.Contains("end")) { Regex regex2 = new Regex(@"^Log ([0-9]+)[,\s]", RegexOptions.IgnoreCase); if (regex2.IsMatch(line)) { MatchCollection matchs = regex2.Matches(line); logcount = int.Parse(matchs[0].Groups[1].Value); genchkcombo(logcount); //status = serialstatus.Done; } } if (line.Contains("No logs")) { status = serialstatus.Done; } break; case serialstatus.Reading: if (line.Contains("packets read") || line.Contains("Done") || line.Contains("logs enabled")) { status = serialstatus.Closefile; Console.Write("CloseFile: " + line); break; } sw.Write(line); continue; case serialstatus.Waiting: if (line.Contains("Dumping Log") || line.Contains("GPS:") || line.Contains("NTUN:") || line.Contains("CTUN:") || line.Contains("PM:")) { status = serialstatus.Reading; Console.Write("Reading: " + line); } break; } lock (thisLock) { this.BeginInvoke((MethodInvoker) delegate() { Console.Write(line); TXT_seriallog.AppendText(line.Replace((char)0x0, ' ')); // auto scroll if (TXT_seriallog.TextLength >= 10000) { TXT_seriallog.Text = TXT_seriallog.Text.Substring(TXT_seriallog.TextLength / 2); } TXT_seriallog.SelectionStart = TXT_seriallog.Text.Length; TXT_seriallog.ScrollToCaret(); TXT_seriallog.Refresh(); }); } } //log.Info("exit while"); } catch (Exception ex) { CustomMessageBox.Show("Error reading data" + ex.ToString()); } }
string GetLog(ushort no) { 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)) { ms.Seek(0, SeekOrigin.Begin); if (ms != null) log.Info("Got Log length: " + ms.Length); status = serialstatus.Done; updateDisplay(); MainV2.comPort.Progress -= comPort_Progress; 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)); 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"; 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 = 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(Strings.ErrorRenameFile + " " + logfile + "\nto " + newlogfilename, Strings.ERROR); } } return logfile; }