Exemplo n.º 1
0
        private void clientdata(IAsyncResult ar)
        {
            var client = ((UdpClient)ar.AsyncState);

            if (client == null || client.Client == null)
            {
                return;
            }
            try
            {
                var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;

                var udpclient = new UdpSerial(client);

                var mav = new MAVLinkInterface();
                mav.BaseStream = udpclient;

                MainV2.comPort = mav;
                MainV2.Comports.Add(mav);

                mav.Open(false, true);

                mav.getParamList();
                //mav.getParamListAsync(mav.MAV.sysid, mav.MAV.compid).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Log.Warning("", ex.ToString());
            }
        }
Exemplo n.º 2
0
        private void clientdata(IAsyncResult ar)
        {
            var client = ((UdpClient)ar.AsyncState);

            if (client == null || client.Client == null)
            {
                return;
            }
            try
            {
                var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;

                var udpclient = new UdpSerial(client);

                var mav = new MAVLinkInterface();
                mav.BaseStream = udpclient;

                MainV2.comPort = mav;
                MainV2.Comports.Add(mav);

                mav.Open(false, true);

                mav.getParamList();

                Forms.Device.BeginInvokeOnMainThread(() =>
                {
                });
            }
            catch (Exception ex)
            {
                Log.Warning("", ex.ToString());
            }
        }
Exemplo n.º 3
0
        private void clientdata(IAsyncResult ar)
        {
            var client = ((UdpClient)ar.AsyncState);

            if (client == null || client.Client == null)
            {
                return;
            }
            try
            {
                var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;

                var udpclient = new UdpSerial(client);

                var mav = new MAVLinkInterface();
                mav.BaseStream = udpclient;

                MainV2.comPort = mav;
                MainV2.Comports.Add(mav);

                mav.Open(false, true);

                mav.getParamList();

                Forms.Device.BeginInvokeOnMainThread(() =>
                {
                });

                Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            while (mav.BaseStream.BytesToRead < 10 || mav.giveComport == true)
                            {
                                Thread.Sleep(20);
                            }

                            var packet = mav.readPacket();

                            mav.MAV.cs.UpdateCurrentSettings(null);
                        }
                        catch (Exception ex)
                        {
                            Log.Warning("", ex.ToString());
                            Thread.Sleep(10);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Log.Warning("", ex.ToString());
            }
        }
Exemplo n.º 4
0
        private void pb_connect_Click(object sender, EventArgs e)
        {
            //pb_connect.BackgroundImage = global::GCSServer.Properties.Resources.disconnect;
            comPort.giveComport = false;

            log.Info("MenuConnect Start");
            //lb_log.Items.Add("MenuConnect Start");

            // sanity check
            if (comPort.BaseStream.IsOpen && comPort.MAV.cs.groundspeed > 4)
            {
                if (DialogResult.No == MessageBox.Show(Strings.Stillmoving, Strings.Disconnect, MessageBoxButtons.YesNo))
                {
                    return;
                }
            }

            try
            {
                log.Info("Cleanup last logfiles");
                //lb_log.Items.Add("Cleanup last logfiles");
                // cleanup from any previous sessions
                if (comPort.logfile != null)
                {
                    comPort.logfile.Close();
                }

                if (comPort.rawlogfile != null)
                {
                    comPort.rawlogfile.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Strings.ErrorClosingLogFile + ex.Message, Strings.ERROR);
            }

            comPort.logfile    = null;
            comPort.rawlogfile = null;

            // decide if this is a connect or disconnect
            #region DISCONNECT
            // IF BUAT DISCONNECT, dont bother
            if (comPort.BaseStream.IsOpen)
            {
                log.Info("We are disconnecting");
                //lb_log.Items.Add("Disconnecting...");
                try
                {
                    //if (speechEngine != null) // cancel all pending speech
                    //    speechEngine.SpeakAsyncCancelAll();

                    comPort.BaseStream.DtrEnable = false;
                    comPort.Close();
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }

                //// now that we have closed the connection, cancel the connection stats
                //// so that the 'time connected' etc does not grow, but the user can still
                //// look at the now frozen stats on the still open form
                //try
                //{
                //    // if terminal is used, then closed using this button.... exception
                //    if (this.connectionStatsForm != null)
                //        ((ConnectionStats)this.connectionStatsForm.Controls[0]).StopUpdates();
                //}
                //catch { }

                //// refresh config window if needed
                //if (MyView.current != null)
                //{
                //    if (MyView.current.Name == "HWConfig")
                //        MyView.ShowScreen("HWConfig");
                //    if (MyView.current.Name == "SWConfig")
                //        MyView.ShowScreen("SWConfig");
                //}

                //try
                //{
                //    System.Threading.ThreadPool.QueueUserWorkItem((WaitCallback)delegate
                //    {
                //        try
                //        {
                //            MissionPlanner.Log.LogSort.SortLogs(Directory.GetFiles(Form1.LogDir, "*.tlog"));
                //        }
                //        catch { }
                //    }
                //    );
                //}
                //catch { }

                //ganti gambar connect
                //this.btn_Start.Text = "CONNECT";
                changeConnectStatusDisplay("CONNECT");
            }
            #endregion
            #region CONNECT
            else
            {
                log.Info("We are connecting");
                //lb_log.Items.Add("Connecting....");
                switch (cb_source.Text)
                {
                //case "TCP":
                //    comPort.BaseStream = new TcpSerial();
                //    break;
                //case "UDP":
                //    comPort.BaseStream = new UdpSerial();
                //    break;
                //case "UDPCl":
                //    comPort.BaseStream = new UdpSerialConnect();
                //    break;
                case "AUTO":    //kalau auto defaultnya pake serial port
                default:
                    comPort.BaseStream = new SerialPort();
                    break;
                }

                //RESET current state ,dont bother
                comPort.MAV.cs.ResetInternals();

                //cleanup any log being played, dont bother
                comPort.logreadmode = false;
                if (comPort.logplaybackfile != null)
                {
                    comPort.logplaybackfile.Close();
                }
                comPort.logplaybackfile = null;

                try
                {
                    // do autoscan
                    if (cb_source.Text == "AUTO")
                    {
                        //Panggil scan supaya ngisi variable portinterface
                        CommsSerialScan.Scan(false);

                        DateTime deadline = DateTime.Now.AddSeconds(50);

                        //klo portnya ga ketemu2 return dari fungsi dan say fail
                        while (CommsSerialScan.foundport == false)
                        {
                            System.Threading.Thread.Sleep(100);

                            if (DateTime.Now > deadline)
                            {
                                MessageBox.Show(Strings.Timeout);
                                //_connectionControl.IsConnected(false);
                                return;
                            }
                        }

                        //ambil portname dan baud lgs dari variable portinterface
                        //_connectionControl.CMB_serialport.Text = Comms.CommsSerialScan.portinterface.PortName;
                        //_connectionControl.CMB_baudrate.Text = Comms.CommsSerialScan.portinterface.BaudRate.ToString();
                        cb_source.Text   = CommsSerialScan.portinterface.PortName;
                        cb_baudrate.Text = CommsSerialScan.portinterface.BaudRate.ToString();
                    }

                    log.Info("Set Portname");
                    // set port, then options
                    comPort.BaseStream.PortName = cb_source.Text;

                    log.Info("Set Baudrate");
                    try
                    {
                        comPort.BaseStream.BaudRate = int.Parse(cb_baudrate.Text);
                    }
                    catch (Exception exp)
                    {
                        log.Error(exp);
                    }
                    //lb_log.Items.Add("Port Name : " + cb_source.Text + ",Baudrate : " + cb_baudrate.Text);

                    // prevent serialreader from doing anything
                    comPort.giveComport = true;//jika true maka exclusive use

                    log.Info("About to do dtr if needed");
                    // reset on connect logic. ,buat DTR - pentingkah??
                    if (config["CHK_resetapmonconnect"] == null || bool.Parse(config["CHK_resetapmonconnect"].ToString()) == true)
                    {
                        log.Info("set dtr rts to false");
                        comPort.BaseStream.DtrEnable = false;
                        comPort.BaseStream.RtsEnable = false;

                        comPort.BaseStream.toggleDTR();
                    }

                    comPort.giveComport = false;

                    // setup to record new logs , dont bother
                    try
                    {
                        Directory.CreateDirectory(Form1.LogDir);
                        comPort.logfile = new BufferedStream(File.Open(Form1.LogDir + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".tlog", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None));

                        comPort.rawlogfile = new BufferedStream(File.Open(Form1.LogDir + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".rlog", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None));

                        log.Info("creating logfile " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".tlog");
                    }
                    catch (Exception exp2) { log.Error(exp2); MessageBox.Show(Strings.Failclog); } // soft fail

                    // reset connect time - for timeout functions
                    connecttime = DateTime.Now;

                    #region Connectnya
                    // do the connect - CONNECT pake mavlink
                    comPort.Open(false);

                    if (!comPort.BaseStream.IsOpen)//klo g open maka close yg exisitng, mungkin gara2 ngeunplug tanpa disconnect
                    {
                        log.Info("comport is closed. existing connect");
                        try
                        {
                            //_connectionControl.IsConnected(false);
                            //UpdateConnectIcon();
                            changeConnectStatusDisplay("CONNECT");
                            comPort.Close();
                        }
                        catch { }
                        return;
                    }

                    //-------------------------NANTI AJA
                    //// KALAU ADA ID lebih dari satu maka bisa pilih yg mana, masukin ke sysidcurrent
                    //// 3dr radio is hidden as no hb packet is ever emitted
                    //if (comPort.sysidseen.Count > 1)
                    //{
                    //    // we have more than one mav
                    //    // user selection of sysid
                    //    MissionPlanner.Controls.SysidSelector id = new SysidSelector();

                    //    id.ShowDialog();
                    //}
                    #endregion

                    comPort.getParamList();

                    #region masalah firmware
                    //// detect firmware we are conected to.
                    //if (comPort.MAV.cs.firmware == Firmwares.ArduCopter2)
                    //{
                    //    _connectionControl.TOOL_APMFirmware.SelectedIndex = _connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduCopter2);
                    //}
                    //else if (comPort.MAV.cs.firmware == Firmwares.Ateryx)
                    //{
                    //    _connectionControl.TOOL_APMFirmware.SelectedIndex = _connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.Ateryx);
                    //}
                    //else if (comPort.MAV.cs.firmware == Firmwares.ArduRover)
                    //{
                    //    _connectionControl.TOOL_APMFirmware.SelectedIndex = _connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduRover);
                    //}
                    //else if (comPort.MAV.cs.firmware == Firmwares.ArduPlane)
                    //{
                    //    _connectionControl.TOOL_APMFirmware.SelectedIndex = _connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduPlane);
                    //}

                    //// check for newer firmware
                    //var softwares = Firmware.LoadSoftwares();

                    //if (softwares.Count > 0)
                    //{
                    //    try
                    //    {
                    //        string[] fields1 = comPort.MAV.VersionString.Split(' ');

                    //        foreach (Firmware.software item in softwares)
                    //        {
                    //            string[] fields2 = item.name.Split(' ');

                    //            // check primare firmware type. ie arudplane, arducopter
                    //            if (fields1[0] == fields2[0])
                    //            {
                    //                Version ver1 = VersionDetection.GetVersion(comPort.MAV.VersionString);
                    //                Version ver2 = VersionDetection.GetVersion(item.name);

                    //                if (ver2 > ver1)
                    //                {
                    //                    Common.MessageShowAgain(Strings.NewFirmware, Strings.NewFirmwareA + item.name + Strings.Pleaseup);
                    //                    break;
                    //                }

                    //                // check the first hit only
                    //                break;
                    //            }
                    //        }
                    //    }
                    //    catch (Exception ex) { log.Error(ex); }
                    //}
                    #endregion

                    #region Miscellaneous
                    //FlightData.CheckBatteryShow();

                    //MissionPlanner.Utilities.Tracking.AddEvent("Connect", "Connect", comPort.MAV.cs.firmware.ToString(), comPort.MAV.param.Count.ToString());
                    //MissionPlanner.Utilities.Tracking.AddTiming("Connect", "Connect Time", (DateTime.Now - connecttime).TotalMilliseconds, "");

                    //MissionPlanner.Utilities.Tracking.AddEvent("Connect", "Baud", comPort.BaseStream.BaudRate.ToString(), "");

                    // save the baudrate for this port
                    config[cb_baudrate.Text + "_BAUD"] = cb_baudrate.Text;

                    this.Text = "GCS Server " + comPort.MAV.VersionString;

                    //// refresh config window if needed
                    //if (MyView.current != null)
                    //{
                    //    if (MyView.current.Name == "HWConfig")
                    //        MyView.ShowScreen("HWConfig");
                    //    if (MyView.current.Name == "SWConfig")
                    //        MyView.ShowScreen("SWConfig");
                    //}


                    //// load wps on connect option.
                    //if (config["loadwpsonconnect"] != null && bool.Parse(config["loadwpsonconnect"].ToString()) == true)
                    //{
                    //    // only do it if we are connected.
                    //    if (comPort.BaseStream.IsOpen)
                    //    {
                    //        MenuFlightPlanner_Click(null, null);
                    //        FlightPlanner.BUT_read_Click(null, null);
                    //    }
                    //}
                    #endregion
                    //GANTI GAMBR DISCONNECT
                    // set connected icon
                    //this.MenuConnect.Image = displayicons.disconnect;
                    MessageBox.Show(Strings.Done);
                    //lb_log.Items.Add("Done!");
                    changeConnectStatusDisplay("DISCONNECT");
                }
                catch (Exception ex)
                {
                    log.Warn(ex);
                    try
                    {
                        //_connectionControl.IsConnected(false);
                        //UpdateConnectIcon();
                        comPort.Close();
                    }
                    catch { }
                    MessageBox.Show("Can not establish a connection\n\n" + ex.Message);
                    //lb_log.Items.Add("Connection failed");
                    return;
                }
            }
            #endregion
        }