Exemple #1
0
        private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!comPort.IsOpen)
            {
                return;
            }
            try
            {
                lock (thisLock)
                {
                    var data = comPort.ReadExisting();
                    //Console.Write(data);

                    if (sw != null)
                    {
                        sw.Write(data);
                        sw.Flush();
                    }

                    addText(data);
                }
            }
            catch (Exception)
            {
                if (!threadrun)
                {
                    return;
                }
                TXT_terminal.AppendText("Error reading com port\r\n");
            }
        }
Exemple #2
0
        void addText(string data)
        {
            this.BeginInvoke((System.Windows.Forms.MethodInvoker) delegate()
            {
                if (this.Disposing)
                {
                    return;
                }

                TXT_terminal.SelectionStart = TXT_terminal.Text.Length;

                data = data.TrimEnd('\r');                  // else added \n all by itself
                data = data.Replace("\0", "");
                data = data.Replace((char)0x1b + "[K", ""); // remove control code
                TXT_terminal.AppendText(data);

                if (data.Contains("\b"))
                {
                    TXT_terminal.Text           = TXT_terminal.Text.Remove(TXT_terminal.Text.IndexOf('\b'));
                    TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
                }

                // erase to end of line. in our case jump to end of line
                if (data.Contains((char)0x1b + "[K"))
                {
                    TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
                }
                inputStartPos = TXT_terminal.SelectionStart;
            });
        }
Exemple #3
0
        void addText(string data)
        {
            this.BeginInvoke((System.Windows.Forms.MethodInvoker) delegate()
            {
                if (this.Disposing)
                {
                    return;
                }

                TXT_terminal.SelectionStart = TXT_terminal.Text.Length;

                data = data.Replace("U3", "");
                data = data.Replace("U$", "");
                data = data.Replace(@"U""", "");
                data = data.Replace("d'`F", "");
                data = data.Replace("U.", "");
                data = data.Replace("'`", "");

                data = data.TrimEnd('\r'); // else added \n all by itself
                data = data.Replace("\0", "");
                TXT_terminal.AppendText(data);
                if (data.Contains("\b"))
                {
                    TXT_terminal.Text           = TXT_terminal.Text.Remove(TXT_terminal.Text.IndexOf('\b'));
                    TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
                }
                inputStartPos = TXT_terminal.SelectionStart;
            });
        }
Exemple #4
0
        void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!comPort.IsOpen)
            {
                return;
            }
            try
            {
                lock (thisLock)
                {
                    System.Threading.Thread.Sleep(20);
                    byte[] buffer = new byte[256];
                    int    a      = 0;
                    while (comPort.BytesToRead > 0)
                    {
                        byte indata = (byte)comPort.ReadByte();

                        buffer[a] = indata;

                        if (buffer[a] >= 0x20 && buffer[a] < 0x7f || buffer[a] == (int)'\n' || buffer[a] == (int)'\r')
                        {
                            a++;
                        }
                    }

                    addText(ASCIIEncoding.ASCII.GetString(buffer, 0, a + 1));
                }
            }
            catch (Exception) { if (!threadrun)
                                {
                                    return;
                                }
                                TXT_terminal.AppendText("Error reading com port\r\n"); }
        }
Exemple #5
0
 private void BUT_disconnect_Click(object sender, EventArgs e)
 {
     try
     {
         comPort.Close();
         TXT_terminal.AppendText("Closed\n");
     }
     catch { }
 }
Exemple #6
0
        private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!comPort.IsOpen)
            {
                return;
            }

            // if btr > 0 then this shouldnt happen
            comPort.ReadTimeout = 300;

            try
            {
                lock (thisLock)
                {
                    var buffer = new byte[256];
                    var a      = 0;

                    while (comPort.IsOpen && comPort.BytesToRead > 0 && !inlogview)
                    {
                        var indata = (byte)comPort.ReadByte();

                        buffer[a] = indata;

                        if (buffer[a] >= 0x20 && buffer[a] < 0x7f || buffer[a] == '\n' || buffer[a] == 0x1b)
                        {
                            a++;
                        }

                        if (indata == '\n')
                        {
                            break;
                        }

                        if (a == (buffer.Length - 1))
                        {
                            break;
                        }
                    }

                    addText(Encoding.ASCII.GetString(buffer, 0, a + 1));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                if (!threadrun)
                {
                    return;
                }
                TXT_terminal.AppendText("Error reading com port\r\n");
            }
        }
Exemple #7
0
        private void addText(string data)
        {
            BeginInvoke((MethodInvoker) delegate
            {
                if (this.Disposing)
                {
                    return;
                }

                if (inputStartPos > TXT_terminal.Text.Length)
                {
                    inputStartPos = TXT_terminal.Text.Length - 1;
                }

                if (inputStartPos == -1)
                {
                    inputStartPos = 0;
                }

                // gather current typed data
                string currenttypedtext = TXT_terminal.Text.Substring(inputStartPos,
                                                                      TXT_terminal.Text.Length - inputStartPos);

                // remove typed data
                TXT_terminal.Text = TXT_terminal.Text.Remove(inputStartPos, TXT_terminal.Text.Length - inputStartPos);

                TXT_terminal.SelectionStart = TXT_terminal.Text.Length;

                data = data.TrimEnd('\r');                  // else added \n all by itself
                data = data.Replace("\0", "");
                data = data.Replace((char)0x1b + "[K", ""); // remove control code
                TXT_terminal.AppendText(data);

                if (data.Contains("\b"))
                {
                    TXT_terminal.Text           = TXT_terminal.Text.Remove(TXT_terminal.Text.IndexOf('\b'));
                    TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
                }

                // erase to end of line. in our case jump to end of line
                if (data.Contains((char)0x1b + "[K"))
                {
                    TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
                }

                inputStartPos = TXT_terminal.SelectionStart;

                //add back typed text
                TXT_terminal.AppendText(currenttypedtext);
            });
        }
Exemple #8
0
        private void addText(string data)
        {
            Invoke((MethodInvoker) delegate
            {
                TXT_terminal.SelectionStart = TXT_terminal.Text.Length;

                data = data.TrimEnd('\r'); // else added \n all by itself
                data = data.Replace("\0", " ");
                TXT_terminal.AppendText(data);
                if (data.Contains("\b"))
                {
                    TXT_terminal.Text           = TXT_terminal.Text.Remove(TXT_terminal.Text.IndexOf('\b'));
                    TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
                }
            });
        }
Exemple #9
0
        private void TXT_terminal_KeyDown(object sender, KeyEventArgs e)
        {
            TXT_terminal.SelectionStart = TXT_terminal.Text.Length;

            /*    if (e.KeyData == Keys.Up || e.KeyData == Keys.Down || e.KeyData == Keys.Left || e.KeyData == Keys.Right)
             *  {
             *      e.Handled = true; // ignore it
             *  }*/
            lock (thisLock)
            {
                switch (e.KeyData)
                {
                case Keys.Up:
                    if (history > 0)
                    {
                        TXT_terminal.Select(inputStartPos, TXT_terminal.Text.Length - inputStartPos);
                        TXT_terminal.SelectedText = "";
                        TXT_terminal.AppendText(cmdHistory[--history]);
                    }

                    e.Handled = true;
                    break;

                case Keys.Down:
                    if (history < cmdHistory.Count - 1)
                    {
                        TXT_terminal.Select(inputStartPos, TXT_terminal.Text.Length - inputStartPos);
                        TXT_terminal.SelectedText = "";
                        TXT_terminal.AppendText(cmdHistory[++history]);
                    }

                    e.Handled = true;
                    break;

                case Keys.Left:
                case Keys.Back:
                    if (TXT_terminal.SelectionStart <= inputStartPos)
                    {
                        e.Handled = true;
                    }
                    break;

                    //case Keys.Right:
                    //    break;
                }
            }
        }
Exemple #10
0
        private void startreadthread()
        {
            Console.WriteLine("Terminal_Load run " + threadrun + " " + comPort.IsOpen);

            BUT_disconnect.Enabled = true;

            var t11 = new 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);
                    ChangeConnectStatus(false);
                    threadrun = false;
                    return;
                }

                Console.WriteLine("Terminal thread 3 run " + threadrun + " " + comPort.IsOpen);

                while (threadrun)
                {
                    try
                    {
                        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(null, null);
                        }

                        if (comPort is MAVLinkSerialPort)
                        {
                            if (lastsend.AddMilliseconds(500) > DateTime.Now)
                            {
                                // 20 hz
                                ((MAVLinkSerialPort)comPort).timeout = 50;
                            }
                            else
                            {
                                // 5 hz
                                ((MAVLinkSerialPort)comPort).timeout = 200;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Terminal thread 4 " + ex);
                    }
                }

                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 (IsDisposed || Disposing)
            {
                return;
            }

            TXT_terminal.AppendText("Opened com port\r\n");
            inputStartPos = TXT_terminal.SelectionStart;


            TXT_terminal.Focus();
        }
Exemple #11
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
                    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;
            }
        }
Exemple #12
0
        private void Terminal_Load(object sender, EventArgs e)
        {
            try
            {
                if (comPort.IsOpen)
                {
                    comPort.Close();
                }

                comPort.ReadBufferSize = 1024 * 1024;

                comPort.PortName = MainV2.comPort.BaseStream.PortName;

                comPort.Open();

                comPort.toggleDTR();

                var t11 = new Thread(delegate()
                {
                    threadrun = true;

                    var start = DateTime.Now;

                    while ((DateTime.Now - start).TotalMilliseconds < 2000)
                    {
                        try
                        {
                            if (comPort.BytesToRead > 0)
                            {
                                comPort_DataReceived(null, null);
                            }
                        }
                        catch
                        {
                            return;
                        }
                    }
                    try
                    {
                        comPort.Write("\n\n\n");
                    }
                    catch
                    {
                        return;
                    }
                    while (threadrun)
                    {
                        try
                        {
                            Thread.Sleep(10);
                            if (!comPort.IsOpen)
                            {
                                break;
                            }
                            if (comPort.BytesToRead > 0)
                            {
                                comPort_DataReceived(null, null);
                            }
                        }
                        catch
                        {
                        }
                    }

                    comPort.DtrEnable = false;

                    try
                    {
                        //if (sw != null)
                        //  sw.Close();
                    }
                    catch
                    {
                    }

                    if (threadrun == false)
                    {
                        comPort.Close();
                    }
                    Console.WriteLine("Comport thread close");
                });
                t11.IsBackground = true;
                t11.Name         = "Terminal serial thread";
                t11.Start();

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

                TXT_terminal.AppendText("Opened com port\r\n");
            }
            catch (Exception)
            {
                TXT_terminal.AppendText("Cant open serial port\r\n");
                return;
            }

            TXT_terminal.Focus();
        }
Exemple #13
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 " + 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();
        }
Exemple #14
0
        private void Terminal_Load(object sender, EventArgs e)
        {
            try
            {
                MainV2.givecomport = true;

                if (comPort.IsOpen)
                {
                    comPort.Close();
                }

                comPort.ReadBufferSize = 1024 * 1024;

                comPort.PortName = MainV2.comportname;

                comPort.Open();

                comPort.toggleDTR();

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

                    DateTime start = DateTime.Now;

                    while ((DateTime.Now - start).TotalMilliseconds < 2000)
                    {
                        try
                        {
                            if (comPort.BytesToRead > 0)
                            {
                                comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
                            }
                        }
                        catch { return; }
                    }

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

                    while (threadrun)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(10);
                            if (inlogview)
                            {
                                continue;
                            }
                            if (!comPort.IsOpen)
                            {
                                break;
                            }
                            if (comPort.BytesToRead > 0)
                            {
                                comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
                            }
                        }
                        catch { }
                    }

                    comPort.DtrEnable = false;

                    if (threadrun == false)
                    {
                        comPort.Close();
                    }
                    Console.WriteLine("Comport thread close");
                });
                t11.IsBackground = true;
                t11.Name         = "Terminal serial thread";
                t11.Start();
                MainV2.threads.Add(t11);

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

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

            TXT_terminal.Focus();
        }
Exemple #15
0
        private void Terminal_Load(object sender, EventArgs e)
        {
            try
            {
                MainV2.comPort.giveComport = true;

                comPort = MainV2.comPort.BaseStream;

                if (comPort.IsOpen)
                {
                    comPort.Close();
                }

                comPort.ReadBufferSize = 1024 * 1024;

                comPort.PortName = MainV2.comPortName;

                comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);

                comPort.Open();

                comPort.toggleDTR();

                MainV2.comPort.doReboot();

                comPort.DiscardInBuffer();

                Console.WriteLine("Terminal_Load");

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

                    Console.WriteLine("Terminal thread start");

                    // 10 sec
                    waitandsleep(10000);

                    Console.WriteLine("Terminal thread 1");

                    // 100 ms
                    readandsleep(100);

                    Console.WriteLine("Terminal thread 2");

                    try
                    {
                        if (!inlogview)
                        {
                            comPort.Write("\n\n\n");
                        }

                        // 1 secs
                        if (!inlogview)
                        {
                            readandsleep(1000);
                        }

                        if (!inlogview)
                        {
                            comPort.Write("\r\r\r?\r");
                        }
                    }
                    catch (Exception ex) { Console.WriteLine("Terminal thread 3 " + ex.ToString()); threadrun = false; return; }

                    Console.WriteLine("Terminal thread 3");

                    while (threadrun)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(10);
                            if (inlogview)
                            {
                                continue;
                            }
                            if (!comPort.IsOpen)
                            {
                                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");
                        comPort.Close();
                    }
                    catch { }

                    Console.WriteLine("Comport thread close");
                });
                t11.IsBackground = true;
                t11.Name         = "Terminal serial thread";
                t11.Start();

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

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

            TXT_terminal.Focus();
        }