doReboot() public method

reboot the vehicle
public doReboot ( bool bootloadermode = false, bool currentvehicle = true ) : bool
bootloadermode bool reboot into bootloader mode?
currentvehicle bool use current sysid/compid or scan for it
return bool
Example #1
0
        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\n");
                    // keep it local
                    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");
                        MainV2.comPort.BaseStream.Write("exit\rreboot\r");
                        try
                        {
                            MainV2.comPort.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(8);
                    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();

                    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);

                    // 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();
        }
Example #2
0
        private void start_Terminal(bool px4)
        {
            try
            {
                if (MainV2.comPort != null && MainV2.comPort.BaseStream != null && MainV2.comPort.BaseStream.IsOpen)
                    MainV2.comPort.BaseStream.Close();

                setcomport();

                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);

                t11 = new System.Threading.Thread(delegate()
                {
                    threaderror = false;
                    threadrun = true;

                    Console.WriteLine("Terminal thread startup 1 " + threadrun + " " + comPort.IsOpen);

                    try
                    {
                        comPort.Write("\r");

                        // 10 sec
                        waitandsleep(10000);

                        Console.WriteLine("Terminal thread startup 2 " + threadrun + " " + comPort.IsOpen);

                        // 100 ms
                        readandsleep(100);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Terminal thread error 3 " + ex.ToString());
                        threaderror = true;
                    }
                    Console.WriteLine("Terminal thread startup 3 " + threadrun + " " + comPort.IsOpen);

                    try
                    {
                        comPort.Write("\n\n\n");

                        // 1 secs
                        readandsleep(1000);

                        comPort.Write("\r\r\r?\r");
                    }
                    catch (Exception ex)
                    {
                        if (!threaderror)
                            Console.WriteLine("Terminal thread error 4 " + ex.ToString());
                        threaderror = true;
                    }
                    Console.WriteLine("Terminal thread startup 4 " + threadrun + " " + comPort.IsOpen);

                    if (!threaderror) setButtonState(true);

                    while (threadrun && !threaderror)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(10);

                            if (inlogview)
                                continue;

                            if (comPort.BytesToRead > 0)
                            {
                                comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Terminal thread error 5 " + ex.ToString());
                            threaderror = true;
                        }
                    }

                    try
                    {
                        //comPort.Write("\rreboot\r");
                        comPort.DtrEnable = false;
                    }
                    catch { }
                    try
                    {
                        Console.WriteLine("Terminal thread close port");
                        comPort.Close();
                    }
                    catch { }

                    setButtonState(false);
                    while (threadrun)
                    {
                        //stay in thread if threaderror
                        System.Threading.Thread.Sleep(10);
                    }
                    log.Info("Terminal thread exit");
                });
                t11.IsBackground = true;
                t11.Name = "Terminal serial thread";
                t11.Start();

                // doesnt seem to work on mac
                //comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

                BUT_ConnectAPM.Enabled = false;
                BUT_disconnect.Enabled = true;
                TXT_terminal.AppendText("Opened com port\r\n");

                inputStartPos = TXT_terminal.SelectionStart;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                TXT_terminal.AppendText("Cant open com port\r\n");
                return;
            }

            TXT_terminal.Focus();
        }