public static bool TryParse(string rawNmeaRmcString, out RmcData rmcData) { try { rmcData = Parse(rawNmeaRmcString); return(true); } catch { rmcData = null; return(false); } }
//======================================================================= //======================================================================= private static GpsReading ParseSentences(string[] sentences) { //---- declare vars GpsReading gpsReading = new GpsReading(); //---- loop through each sentence for (int i = 0; i < sentences.Length; i++) { //---- if the sentence has a header and data if (sentences[i].Length > 6) { switch (sentences[i].Trim().Substring(0, 6).ToUpper()) { case "$GPGSA": gpsReading.DopActiveSatellites = GsaData.Parse(sentences[i]); break; case "$GPGSV": gpsReading.SatellitesInView.Add(GsvData.Parse(sentences[i])); break; case "$GPGGA": gpsReading.FixedGpsData = GgaData.Parse(sentences[i]); break; case "$GPRMC": gpsReading.Summary = RmcData.Parse(sentences[i]); break; case "$GPMSS": break; case "$GPVTG": break; } } } //---- return our parsed gps reading return(gpsReading); }
//======================================================================= //======================================================================= #region -= constructors =- #endregion //======================================================================= //======================================================================= #region -= public methods =- #endregion //======================================================================= //======================================================================= #region -= static methods =- public static RmcData Parse(string inputString) { //---- declare vars RmcData rmcData = new RmcData(); string dataString = inputString.Substring(0, inputString.IndexOf('*')); // strip off the checksum string[] values = dataString.Split(','); //---- if we don't have 12 (header + 11), it's no good if (values.Length < 12) { throw new FormatException(); } //---- Status if (values[2].ToUpper() == "A") { rmcData.Status = Status.Valid; } else { rmcData.Status = Status.Invalid; } if (rmcData.Status == Status.Invalid) { return(rmcData); } if (values[1].Length == 6 && values[9].Length == 6) {//---- if the date and time both are six digits //---- make sure that they're actually numbers int temp; bool isparse = true; try { temp = int.Parse(values[1]); temp = int.Parse(values[9]); } catch (ArgumentException) { isparse = false; } if (isparse) { //---- should add more validation here int day = int.Parse(values[9].Substring(0, 2)); int month = int.Parse(values[9].Substring(2, 2)); int year = int.Parse(values[9].Substring(4, 2)); int hour = int.Parse(values[1].Substring(0, 2)); int minute = int.Parse(values[1].Substring(2, 2)); int second = int.Parse(values[1].Substring(4, 2)); rmcData.UtcDateTime = new DateTime(year, month, day, hour, minute, second); } else { throw new FormatException("Date or time string is invalid"); } } else if (values[1].Length == 10 && values[9].Length == 6) {//---- if the date and time both are six digits //---- make sure that they're actually numbers int temp; double timeTemp; bool isparse = true; try { timeTemp = double.Parse(values[1]); temp = int.Parse(values[9]); } catch (ArgumentException) { isparse = false; } if (isparse) { //---- should add more validation here int day = int.Parse(values[9].Substring(0, 2)); int month = int.Parse(values[9].Substring(2, 2)); int year = int.Parse(values[9].Substring(4, 2)); int hour = int.Parse(values[1].Substring(0, 2)); int minute = int.Parse(values[1].Substring(2, 2)); int second = int.Parse(values[1].Substring(4, 2)); int millisecond = int.Parse(values[1].Substring(7, 3)); rmcData.UtcDateTime = new DateTime(year, month, day, hour, minute, second); } else { throw new FormatException("Date or time string is invalid"); } } else { throw new FormatException("Date or time string is invalid"); } //---- lat/long position rmcData.Position = Position.Parse(values[3] + "," + values[4] + ";" + values[5] + "," + values[6]); //---- speed in knots rmcData.GroundSpeed = Decimal.Parse(values[7]); //---- true course rmcData.Heading = Decimal.Parse(values[8]); //---- magnetic declination rmcData.MagneticVariation = MagneticVariation.Parse(values[10] + "," + values[11]); //---- return return(rmcData); //1 220516 Time Stamp //2 A validity - A-ok, V-invalid //3 5133.82 current Latitude //4 N North/South //5 00042.24 current Longitude //6 W East/West //7 173.8 Speed in knots //8 231.8 True course //9 130694 Date Stamp //10 004.2 Variation //11 W East/West //12 *70 checksum }