Exemplo n.º 1
0
        /// <summary>
        /// Extracts a full NMEA string from the data recieved on the serialport, and parses this.
        /// The remaining and unparsed NMEA string is returned.
        /// </summary>
        /// <param name="strNMEA">NMEA ASCII data</param>
        /// <returns>Unparsed NMEA data</returns>
        private string GetNmeaString(string strNMEA)
        {
            strNMEA = strNMEA.Replace("\n", "").Replace("\r", ""); //Remove linefeeds

            int nStart = strNMEA.IndexOf("$");                     //Position of first NMEA data

            if (nStart < 0 || nStart == strNMEA.Length - 2)
            {
                return(strNMEA);
            }

            //This will never pass the last NMEA sentence, before the next one arrives
            //The following should instead stop at the end of the line.
            int nStop = strNMEA.IndexOf("$", nStart + 1); //Position of next NMEA sentence

            if (nStop > -1)
            {
                string strData = strNMEA.Substring(nStart, nStop - nStart).Trim();
                if (strData.StartsWith("$"))
                {
                    _hasTimedOut        = false;
                    _timeSinceLastEvent = DateTime.Now.Ticks;
                    if (CheckSentence(strData))
                    {
                        FireEvent(GpsHandler.String2Eventtype(strData), strData);
                    }
                }
                return(strNMEA.Substring(nStop));
            }
            return(strNMEA);
        }
Exemplo n.º 2
0
        public MainForm()
        {
            InitializeComponent();

            Gps = new GpsHandler { TimeOut = 5 }; //Initialize GPS handler
            Gps.NewGpsFix += GpsEventHandler; //Hook up GPS data events to a handler
            FrmGpsSettings = new FrmGpsSettings();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Responds to sentence events from GPS receiver
        /// </summary>
        private void GpsEventHandler(object sender, GpsHandler.GpsEventArgs e)
        {
            if (InvokeRequired)
            {
                // Execute the same method, but this time on the GUI thread
                BeginInvoke(new ThreadStart(() => GpsEventHandler(sender, e)));
                return;
            }

            tbRawLog.Text += e.Sentence + "\r\n";
            if (tbRawLog.Text.Length > 20 * 1024 * 1024) //20Kb maximum - prevents crash
            {
                tbRawLog.Text = tbRawLog.Text.Substring(10 * 1024 * 1024);
            }
            tbRawLog.ScrollToCaret(); //Scroll to bottom

            switch (e.TypeOfEvent)
            {
                case GpsEventType.GPRMC:  //Recommended minimum specific GPS/Transit data
                    if (Gps.HasGpsFix) //Is a GPS fix available?
                    {
                        //lbRMCPosition.Text = GPS.GPRMC.Position.ToString("#.000000");
                        lbRMCPosition.Text = Gps.GPRMC.Position.ToString("DMS");
                        lbRMCCourse.Text = Gps.GPRMC.Course.ToString();
                        lbRMCSpeed.Text = Gps.GPRMC.Speed + " mph";
                        lbRMCTimeOfFix.Text = Gps.GPRMC.TimeOfFix.ToString("F");
                        lbRMCMagneticVariation.Text = Gps.GPRMC.MagneticVariation.ToString();
                    }
                    else
                    {
                        statusBar1.Text = "No fix";
                        lbRMCCourse.Text = "N/A";
                        lbRMCSpeed.Text = "N/A";
                        lbRMCTimeOfFix.Text = Gps.GPRMC.TimeOfFix.ToString();
                    }
                    break;
                case GpsEventType.GPGGA: //Global Positioning System Fix Data
                    if (Gps.GPGGA.Position != null)
                        lbGGAPosition.Text = Gps.GPGGA.Position.ToString("DM");
                    else
                        lbGGAPosition.Text = "";
                    lbGGATimeOfFix.Text = Gps.GPGGA.TimeOfFix.Hour + ":" + Gps.GPGGA.TimeOfFix.Minute + ":" + Gps.GPGGA.TimeOfFix.Second;
                    lbGGAFixQuality.Text = Gps.GPGGA.FixQuality.ToString();
                    lbGGANoOfSats.Text = Gps.GPGGA.NoOfSats.ToString();
                    lbGGAAltitude.Text = Gps.GPGGA.Altitude + " " + Gps.GPGGA.AltitudeUnits;
                    lbGGAHDOP.Text = Gps.GPGGA.Dilution.ToString();
                    lbGGAGeoidHeight.Text = Gps.GPGGA.HeightOfGeoid.ToString();
                    lbGGADGPSupdate.Text = Gps.GPGGA.DGPSUpdate.ToString();
                    lbGGADGPSID.Text = Gps.GPGGA.DGPSStationID;
                    break;
                case GpsEventType.GPGLL: //Geographic position, Latitude and Longitude
                    lbGLLPosition.Text = Gps.GPGLL.Position.ToString();
                    lbGLLTimeOfSolution.Text = (Gps.GPGLL.TimeOfSolution.HasValue ? Gps.GPGLL.TimeOfSolution.Value.Hours + ":" + Gps.GPGLL.TimeOfSolution.Value.Minutes.ToString() + ":" + Gps.GPGLL.TimeOfSolution.Value.Seconds.ToString() : "");
                    lbGLLDataValid.Text = Gps.GPGLL.DataValid.ToString();
                    break;
                case GpsEventType.GPGSA: //GPS DOP and active satellites
                    if (Gps.GPGSA.Mode == 'A')
                        lbGSAMode.Text = "Auto";
                    else if (Gps.GPGSA.Mode == 'M')
                        lbGSAMode.Text = "Manual";
                    else lbGSAMode.Text = "";
                    lbGSAFixMode.Text = Gps.GPGSA.FixMode.ToString();
                    lbGSAPRNs.Text = "";
                    if (Gps.GPGSA.PRNInSolution.Count > 0)
                        foreach (string prn in Gps.GPGSA.PRNInSolution)
                            lbGSAPRNs.Text += prn + " ";
                    else
                        lbGSAPRNs.Text += "none";
                    lbGSAPDOP.Text = Gps.GPGSA.PDOP + " (" + DOPtoWord(Gps.GPGSA.PDOP) + ")";
                    lbGSAHDOP.Text = Gps.GPGSA.HDOP + " (" + DOPtoWord(Gps.GPGSA.HDOP) + ")";
                    lbGSAVDOP.Text = Gps.GPGSA.VDOP + " (" + DOPtoWord(Gps.GPGSA.VDOP) + ")";
                    break;
                case GpsEventType.GPGSV: //Satellites in view
                    if (NMEAtabs.TabPages[NMEAtabs.SelectedIndex].Text == "GPGSV") //Only update this tab when it is active
                        DrawGSV();
                    break;
                case GpsEventType.PGRME: //Garmin proprietary sentences.
                    lbRMEHorError.Text = Gps.PGRME.EstHorisontalError.ToString();
                    lbRMEVerError.Text = Gps.PGRME.EstVerticalError.ToString();
                    lbRMESphericalError.Text = Gps.PGRME.EstSphericalError.ToString();
                    break;
                case GpsEventType.TimeOut: //Serialport timeout.
                    statusBar1.Text = "Serialport timeout";
                    break;
            }
        }