private void start_Terminal(bool px4) { setcomport(); try { if (MainV2.comPort != null && MainV2.comPort.BaseStream != null && MainV2.comPort.BaseStream.IsOpen) { MainV2.comPort.BaseStream.Close(); } if (comPort.IsOpen) { Console.WriteLine("Terminal Start - Close Port"); threadrun = false; // if (DialogResult.Cancel == CustomMessageBox.Show("The port is open\n Continue?", "Continue", MessageBoxButtons.YesNo)) { // return; } comPort.Close(); // allow things to cleanup System.Threading.Thread.Sleep(400); } comPort.ReadBufferSize = 1024 * 1024 * 4; comPort.PortName = MainV2.comPortName; // test moving baud rate line comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text); if (px4) { TXT_terminal.AppendText("Rebooting " + MainV2.comPortName + " at " + comPort.BaudRate + "\n"); // keep it local using (MAVLinkInterface mine = new MAVLinkInterface()) { mine.BaseStream.PortName = MainV2.comPortName; mine.BaseStream.BaudRate = comPort.BaudRate; mine.giveComport = true; mine.BaseStream.Open(); // check if we are a mavlink stream byte[] buffer = mine.readPacket(); if (buffer.Length > 0) { log.Info("got packet - sending reboot via mavlink"); TXT_terminal.AppendText("Via Mavlink\n"); mine.doReboot(false); try { mine.BaseStream.Close(); } catch { } } else { log.Info("no packet - sending reboot via console"); TXT_terminal.AppendText("Via Console\n"); try { mine.BaseStream.Write("reboot\r"); mine.BaseStream.Write("exit\rreboot\r"); } catch { } try { mine.BaseStream.Close(); } catch { } } } TXT_terminal.AppendText("Waiting for reboot\n"); // wait 7 seconds for px4 reboot log.Info("waiting for reboot"); DateTime deadline = DateTime.Now.AddSeconds(9); while (DateTime.Now < deadline) { System.Threading.Thread.Sleep(500); Application.DoEvents(); } int a = 0; while (a < 5) { try { if (!comPort.IsOpen) { comPort.Open(); } } catch { } System.Threading.Thread.Sleep(200); a++; } } else { log.Info("About to open " + comPort.PortName); comPort.Open(); log.Info("toggle dtr"); comPort.toggleDTR(); } try { comPort.DiscardInBuffer(); } catch { } Console.WriteLine("Terminal_Load run " + threadrun + " " + comPort.IsOpen); BUT_disconnect.Enabled = true; System.Threading.Thread t11 = new System.Threading.Thread(delegate() { threadrun = true; Console.WriteLine("Terminal thread start run run " + threadrun + " " + comPort.IsOpen); try { comPort.Write("\r"); } catch { } // 10 sec waitandsleep(10000); Console.WriteLine("Terminal thread 1 run " + threadrun + " " + comPort.IsOpen); // 100 ms readandsleep(100); Console.WriteLine("Terminal thread 2 run " + threadrun + " " + comPort.IsOpen); try { if (!inlogview && comPort.IsOpen) { comPort.Write("\n\n\n"); } // 1 secs if (!inlogview && comPort.IsOpen) { readandsleep(1000); } if (!inlogview && comPort.IsOpen) { comPort.Write("\r\r\r?\r"); } } catch (Exception ex) { Console.WriteLine("Terminal thread 3 " + ex.ToString()); ChangeConnectStatus(false); threadrun = false; return; } Console.WriteLine("Terminal thread 3 run " + threadrun + " " + comPort.IsOpen); while (threadrun) { try { System.Threading.Thread.Sleep(10); if (!threadrun) { break; } if (this.Disposing) { break; } if (inlogview) { continue; } if (!comPort.IsOpen) { Console.WriteLine("Comport Closed"); ChangeConnectStatus(false); break; } if (comPort.BytesToRead > 0) { comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null); } } catch (Exception ex) { Console.WriteLine("Terminal thread 4 " + ex.ToString()); } } threadrun = false; try { comPort.DtrEnable = false; } catch { } try { Console.WriteLine("term thread close run " + threadrun + " " + comPort.IsOpen); ChangeConnectStatus(false); comPort.Close(); } catch { } Console.WriteLine("Comport thread close run " + threadrun); }); t11.IsBackground = true; t11.Name = "Terminal serial thread"; t11.Start(); // doesnt seem to work on mac //comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); if (this.IsDisposed || this.Disposing) { return; } TXT_terminal.AppendText("Opened com port\r\n"); inputStartPos = TXT_terminal.SelectionStart; } catch (Exception ex) { log.Error(ex); TXT_terminal.AppendText("Cant open serial port\r\n"); return; } TXT_terminal.Focus(); }
private void start_Terminal(bool px4) { setcomport(); try { if (MainV2.comPort != null && MainV2.comPort.BaseStream != null && MainV2.comPort.BaseStream.IsOpen) { MainV2.comPort.BaseStream.Close(); } if (comPort.IsOpen) { Console.WriteLine("Terminal Start - Close Port"); threadrun = false; // if (DialogResult.Cancel == CustomMessageBox.Show("The port is open\n Continue?", "Continue", MessageBoxButtons.YesNo)) { // return; } comPort.Close(); // allow things to cleanup Thread.Sleep(400); } comPort.ReadBufferSize = 1024 * 1024 * 4; comPort.PortName = MainV2.comPortName; // test moving baud rate line comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text); if (px4) { TXT_terminal.AppendText("Rebooting " + MainV2.comPortName + " at " + comPort.BaudRate + "\n"); // keep it local using (var mine = new MAVLinkInterface()) { mine.BaseStream.PortName = MainV2.comPortName; mine.BaseStream.BaudRate = comPort.BaudRate; mine.giveComport = true; mine.BaseStream.Open(); // check if we are a mavlink stream var buffer = mine.readPacket(); if (buffer.Length > 0) { log.Info("got packet - sending reboot via mavlink"); TXT_terminal.AppendText("Via Mavlink\n"); mine.doReboot(false); try { mine.BaseStream.Close(); } catch { } } else { log.Info("no packet - sending reboot via console"); TXT_terminal.AppendText("Via Console\n"); try { mine.BaseStream.Write("reboot\r"); mine.BaseStream.Write("exit\rreboot\r"); } catch { } try { mine.BaseStream.Close(); } catch { } } } TXT_terminal.AppendText("Waiting for reboot\n"); // wait 7 seconds for px4 reboot log.Info("waiting for reboot"); var deadline = DateTime.Now.AddSeconds(9); while (DateTime.Now < deadline) { Thread.Sleep(500); Application.DoEvents(); } var a = 0; while (a < 5) { try { if (!comPort.IsOpen) { comPort.Open(); } } catch { } Thread.Sleep(200); a++; } } else { log.Info("About to open " + comPort.PortName); comPort.Open(); log.Info("toggle dtr"); comPort.toggleDTR(); } try { comPort.DiscardInBuffer(); } catch { } startreadthread(); } catch (Exception ex) { log.Error(ex); TXT_terminal.AppendText("Cant open serial port\r\n"); return; } }
private void start_Terminal(bool px4) { setcomport(); try { if (MainV2.comPort != null && MainV2.comPort.BaseStream != null && MainV2.comPort.BaseStream.IsOpen) MainV2.comPort.BaseStream.Close(); if (comPort.IsOpen) { Console.WriteLine("Terminal Start - Close Port"); threadrun = false; // if (DialogResult.Cancel == CustomMessageBox.Show("The port is open\n Continue?", "Continue", MessageBoxButtons.YesNo)) { // return; } comPort.Close(); // allow things to cleanup Thread.Sleep(400); } comPort.ReadBufferSize = 1024*1024*4; comPort.PortName = MainV2.comPortName; // test moving baud rate line comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text); if (px4) { TXT_terminal.AppendText("Rebooting " + MainV2.comPortName + " at " + comPort.BaudRate + "\n"); // keep it local using (var mine = new MAVLinkInterface()) { mine.BaseStream.PortName = MainV2.comPortName; mine.BaseStream.BaudRate = comPort.BaudRate; mine.giveComport = true; mine.BaseStream.Open(); // check if we are a mavlink stream var buffer = mine.readPacket(); if (buffer.Length > 0) { log.Info("got packet - sending reboot via mavlink"); TXT_terminal.AppendText("Via Mavlink\n"); mine.doReboot(false); try { mine.BaseStream.Close(); } catch { } } else { log.Info("no packet - sending reboot via console"); TXT_terminal.AppendText("Via Console\n"); try { mine.BaseStream.Write("reboot\r"); mine.BaseStream.Write("exit\rreboot\r"); } catch { } try { mine.BaseStream.Close(); } catch { } } } TXT_terminal.AppendText("Waiting for reboot\n"); // wait 7 seconds for px4 reboot log.Info("waiting for reboot"); var deadline = DateTime.Now.AddSeconds(9); while (DateTime.Now < deadline) { Thread.Sleep(500); Application.DoEvents(); } var a = 0; while (a < 5) { try { if (!comPort.IsOpen) comPort.Open(); } catch { } Thread.Sleep(200); a++; } } else { log.Info("About to open " + comPort.PortName); comPort.Open(); log.Info("toggle dtr"); comPort.toggleDTR(); } try { comPort.DiscardInBuffer(); } catch { } startreadthread(); } catch (Exception ex) { log.Error(ex); TXT_terminal.AppendText("Cant open serial port\r\n"); return; } }