Esempio n. 1
0
        /// <summary>
        /// Reads the Heading direction
        /// </summary>
        /// <returns>The direction as an azimuth angle</returns>
        public Azimuth ReadHeading()
        {
            // Does it support the value we want?
            IHeadingSentence sentence = ReadTypedSentence() as IHeadingSentence;

            // If not, start over (recursive)
            while (sentence == null)
            {
                sentence = ReadTypedSentence() as IHeadingSentence;
            }
            // Return the location
            return(sentence.Heading);
        }
Esempio n. 2
0
        /// <summary>
        /// Translates the NmeaSentence
        /// </summary>
        /// <param name="sentence">The NMeaSentence to parse</param>
        public void Parse(NmeaSentence sentence)
        {
            /* NMEA data is parsed in a specific order to maximize data quality and also to reduce latency
             * problems.  The date/time is processed first to minimize latency.  Then, dilution of precision
             * values are processed.  Finally, if precision is good enough to work with, remaining values are
             * processed.
             */

            // Is this a fix method message?
            IFixMethodSentence fixMethodSentence = sentence as IFixMethodSentence;

            if (fixMethodSentence != null)
            {
                SetFixMethod(fixMethodSentence.FixMethod);
            }

            // Is this a fix quality message?
            IFixQualitySentence fixQualitySentence = sentence as IFixQualitySentence;

            if (fixQualitySentence != null)
            {
                SetFixQuality(fixQualitySentence.FixQuality);
            }

            #region Process common GPS information

            // If a fix is required, don't process time
            if (!IsFixRequired || (IsFixRequired && IsFixed))
            {
                // Does this sentence support the UTC date and time?
                IUtcDateTimeSentence dateTimeSentence = sentence as IUtcDateTimeSentence;
                if (dateTimeSentence != null)
                {
                    SetDateTimes(dateTimeSentence.UtcDateTime);
                }

                /* Some NMEA sentences provide UTC time information, but no date!  To make this work,
                 * we must combine the time report with the current UTC date.
                 */
                IUtcTimeSentence timeSentence = sentence as IUtcTimeSentence;
                if (timeSentence != null && !timeSentence.UtcTime.Equals(TimeSpan.MinValue) && !timeSentence.UtcTime.Equals(TimeSpan.Zero))
                {
                    SetDateTimes(new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
                                              timeSentence.UtcTime.Hours, timeSentence.UtcTime.Minutes, timeSentence.UtcTime.Seconds, timeSentence.UtcTime.Milliseconds,
                                              DateTimeKind.Utc));
                }
            }

            // Does this sentence support horizontal DOP?
            IHorizontalDilutionOfPrecisionSentence hdopSentence = sentence as IHorizontalDilutionOfPrecisionSentence;
            if (hdopSentence != null)
            {
                SetHorizontalDilutionOfPrecision(hdopSentence.HorizontalDilutionOfPrecision);
            }

            // Does the sentence support vertical DOP?
            IVerticalDilutionOfPrecisionSentence vdopSentence = sentence as IVerticalDilutionOfPrecisionSentence;
            if (vdopSentence != null)
            {
                SetVerticalDilutionOfPrecision(vdopSentence.VerticalDilutionOfPrecision);
            }

            // Does the sentence support mean DOP?
            IPositionDilutionOfPrecisionSentence mdopSentence = sentence as IPositionDilutionOfPrecisionSentence;
            if (mdopSentence != null)
            {
                SetMeanDilutionOfPrecision(mdopSentence.PositionDilutionOfPrecision);
            }

            #endregion Process common GPS information

            // If a fix is required, don't process time
            if (!IsFixRequired || (IsFixRequired && IsFixed))
            {
                // Is precision good enough to work with?
                if (HorizontalDilutionOfPrecision.Value <= MaximumHorizontalDilutionOfPrecision.Value)
                {
                    #region Process real-time positional data

                    // Does this sentence support lat/long info?
                    IPositionSentence positionSentence = sentence as IPositionSentence;
                    if (positionSentence != null)
                    {
                        SetPosition(positionSentence.Position);
                    }

                    // Does this sentence support bearing?
                    IBearingSentence bearingSentence = sentence as IBearingSentence;
                    if (bearingSentence != null)
                    {
                        SetBearing(bearingSentence.Bearing);
                    }

                    // Does this sentence support heading?
                    IHeadingSentence headingSentence = sentence as IHeadingSentence;
                    if (headingSentence != null)
                    {
                        SetHeading(headingSentence.Heading);
                    }

                    // Does this sentence support speed?
                    ISpeedSentence speedSentence = sentence as ISpeedSentence;
                    if (speedSentence != null)
                    {
                        SetSpeed(speedSentence.Speed);
                    }

                    #endregion Process real-time positional data
                }

                // Is the vertical DOP low enough to be worth processing?
                if (VerticalDilutionOfPrecision.Value <= MaximumVerticalDilutionOfPrecision.Value)
                {
                    #region Process altitude data

                    // Does this sentence support altitude?
                    IAltitudeSentence altitudeSentence = sentence as IAltitudeSentence;
                    if (altitudeSentence != null)
                    {
                        SetAltitude(altitudeSentence.Altitude);
                    }

                    // Does this sentence support altitude?
                    IAltitudeAboveEllipsoidSentence altitudeAboveEllipsoidSentence = sentence as IAltitudeAboveEllipsoidSentence;
                    if (altitudeAboveEllipsoidSentence != null)
                    {
                        SetAltitude(altitudeAboveEllipsoidSentence.AltitudeAboveEllipsoid);
                    }

                    // Does this sentence support geoidal separation?
                    IGeoidalSeparationSentence geoidSeparationSentence = sentence as IGeoidalSeparationSentence;
                    if (geoidSeparationSentence != null)
                    {
                        SetGeoidalSeparation(geoidSeparationSentence.GeoidalSeparation);
                    }

                    #endregion Process altitude data
                }
            }

            #region Lower-priority information

            // Is this a fix mode sentence?
            IFixModeSentence fixModeSentence = sentence as IFixModeSentence;
            if (fixModeSentence != null)
            {
                SetFixMode(fixModeSentence.FixMode);
            }

            // Does this sentence have fix status?
            IFixStatusSentence fixedSentence = sentence as IFixStatusSentence;
            if (fixedSentence != null)
            {
                SetFixStatus(fixedSentence.FixStatus);
            }

            // Does this sentence support magnetic variation?
            IMagneticVariationSentence magVarSentence = sentence as IMagneticVariationSentence;
            if (magVarSentence != null)
            {
                SetMagneticVariation(magVarSentence.MagneticVariation);
            }

            // Process satellite data
            ISatelliteCollectionSentence satelliteSentence = sentence as ISatelliteCollectionSentence;
            if (satelliteSentence != null)
            {
                /* GPS.NET 2.0 performed thorough comparison of satellites in order to update
                 * an *existing* instance of Satellite objects.  I think now that this was overkill.
                 * For performance and limited memory use, satellites are just overwritten.
                 */
                AppendSatellites(satelliteSentence.Satellites);
            }

            // Fixed satellite count
            IFixedSatelliteCountSentence fixedCountSentence = sentence as IFixedSatelliteCountSentence;
            if (fixedCountSentence != null)
            {
                SetFixedSatelliteCount(fixedCountSentence.FixedSatelliteCount);
            }

            // Process fixed satellites
            IFixedSatellitesSentence fixedSatellitesSentence = sentence as IFixedSatellitesSentence;
            if (fixedSatellitesSentence != null)
            {
                SetFixedSatellites(fixedSatellitesSentence.FixedSatellites);
            }

            #endregion Lower-priority information
        }
Esempio n. 3
0
        /// <summary>
        /// Decode the NMEA data.
        /// </summary>
        /// <param name="nmeaStrings">String array containing NMEA sentences.</param>
        /// <returns>Gps Data.</returns>
        public static GpsData DecodeNmea(string[] nmeaStrings)
        {
            GpsData gpsData = new GpsData();

            try
            {
                for (int x = 0; x < nmeaStrings.Length; x++)
                {
                    // Parse all the nmea setences found
                    NmeaSentence sentence = new NmeaSentence(nmeaStrings[x]);

                    // Is this a GPRMC sentence?
                    if (sentence.CommandWord.EndsWith("RMC", StringComparison.Ordinal))
                    {
                        gpsData.GPRMC = new GprmcSentence(sentence.Sentence);
                    }

                    // Is this a GPHDT sentence?
                    if (sentence.CommandWord.EndsWith("HDT", StringComparison.Ordinal))
                    {
                        gpsData.GPHDT = new GphdtSentence(sentence.Sentence);
                    }

                    // Is this a GPVTG sentence?
                    if (sentence.CommandWord.EndsWith("VTG", StringComparison.Ordinal))
                    {
                        gpsData.GPVTG = new GpvtgSentence(sentence.Sentence);
                    }

                    // Is this a GPGGA sentence?
                    if (sentence.CommandWord.EndsWith("GGA", StringComparison.Ordinal))
                    {
                        gpsData.GPGGA = new GpggaSentence(sentence.Sentence);
                    }

                    // Does this sentence support lat/long info?
                    IPositionSentence positionSentence = sentence as IPositionSentence;
                    if (positionSentence != null)
                    {
                        gpsData.Position = positionSentence.Position;
                    }

                    // Does this sentence support bearing?
                    IBearingSentence bearingSentence = sentence as IBearingSentence;
                    if (bearingSentence != null)
                    {
                        gpsData.Bearing = bearingSentence.Bearing;
                    }

                    // Does this sentence support heading?
                    IHeadingSentence headingSentence = sentence as IHeadingSentence;
                    if (headingSentence != null)
                    {
                        gpsData.Heading = headingSentence.Heading;
                    }

                    // Does this sentence support speed?
                    ISpeedSentence speedSentence = sentence as ISpeedSentence;
                    if (speedSentence != null)
                    {
                        gpsData.Speed = speedSentence.Speed;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error decoding GPS data.", e);
            }

            return(gpsData);
        }