From 93aa1d8192f08820f4daa8d1d700beb199b3973b Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 30 Jul 2014 23:43:48 -0700 Subject: [PATCH] Added $GPBOD --- src/NmeaParser.Shared/Nmea/Gps/GPBOD.cs | 67 +++++++++++++++++++ src/NmeaParser.Shared/Nmea/Gps/GPGSV.cs | 2 +- src/NmeaParser.Shared/NmeaDevice.cs | 2 +- .../NmeaParser.Shared.projitems | 1 + src/NmeaParser.Tests/NmeaMessages.cs | 41 ++++++++++++ 5 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/NmeaParser.Shared/Nmea/Gps/GPBOD.cs diff --git a/src/NmeaParser.Shared/Nmea/Gps/GPBOD.cs b/src/NmeaParser.Shared/Nmea/Gps/GPBOD.cs new file mode 100644 index 0000000..73e20b3 --- /dev/null +++ b/src/NmeaParser.Shared/Nmea/Gps/GPBOD.cs @@ -0,0 +1,67 @@ +// +// Copyright (c) 2014 Morten Nielsen +// +// Licensed under the Microsoft Public License (Ms-PL) (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://opensource.org/licenses/Ms-PL.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NmeaParser.Nmea.Gps +{ + /// + /// Bearing Origin to Destination + /// + [NmeaMessageType(Type = "GPBOD")] + public class Gpbod : NmeaMessage + { + protected override void LoadMessage(string[] message) + { + if (message[0].Length > 0) + TrueBearing = double.Parse(message[0], CultureInfo.InvariantCulture); + else + TrueBearing = double.NaN; + if (message[2].Length > 0) + MagneticBearing = double.Parse(message[2], CultureInfo.InvariantCulture); + else + MagneticBearing = double.NaN; + if (message.Length > 4 && !string.IsNullOrEmpty(message[4])) + DestinationID = message[4]; + if (message.Length > 5 && !string.IsNullOrEmpty(message[5])) + OriginID = message[5]; + } + /// + /// True Bearing from start to destination + /// + public double TrueBearing { get; private set; } + + /// + /// Magnetic Bearing from start to destination + /// + public double MagneticBearing { get; private set; } + + /// + /// Name of origin + /// + public string OriginID { get; set; } + + /// + /// Name of destination + /// + public string DestinationID { get; set; } + } +} diff --git a/src/NmeaParser.Shared/Nmea/Gps/GPGSV.cs b/src/NmeaParser.Shared/Nmea/Gps/GPGSV.cs index 4ecc046..fa81e53 100644 --- a/src/NmeaParser.Shared/Nmea/Gps/GPGSV.cs +++ b/src/NmeaParser.Shared/Nmea/Gps/GPGSV.cs @@ -36,7 +36,7 @@ namespace NmeaParser.Nmea.Gps SVsInView = int.Parse(message[2]); List svs = new List(); - for (int i = 3; i < 18; i+=4) + for (int i = 3; i < message.Length - 4; i += 4) { if (message[i].Length == 0) continue; diff --git a/src/NmeaParser.Shared/NmeaDevice.cs b/src/NmeaParser.Shared/NmeaDevice.cs index a266eb3..b55b882 100644 --- a/src/NmeaParser.Shared/NmeaDevice.cs +++ b/src/NmeaParser.Shared/NmeaDevice.cs @@ -98,7 +98,7 @@ namespace NmeaParser if (lineEnd > -1) { line = message.Substring(0, lineEnd).Trim(); - message = message.Substring(lineEnd).Trim(); + message = message.Substring(lineEnd); } } if (!string.IsNullOrEmpty(line)) diff --git a/src/NmeaParser.Shared/NmeaParser.Shared.projitems b/src/NmeaParser.Shared/NmeaParser.Shared.projitems index 2504439..f482a6b 100644 --- a/src/NmeaParser.Shared/NmeaParser.Shared.projitems +++ b/src/NmeaParser.Shared/NmeaParser.Shared.projitems @@ -13,6 +13,7 @@ + diff --git a/src/NmeaParser.Tests/NmeaMessages.cs b/src/NmeaParser.Tests/NmeaMessages.cs index 48fc655..e1f4506 100644 --- a/src/NmeaParser.Tests/NmeaMessages.cs +++ b/src/NmeaParser.Tests/NmeaMessages.cs @@ -263,5 +263,46 @@ namespace NmeaParser.Tests Assert.AreEqual(145.1226666666666666667, gll.Longitude); Assert.AreEqual(TimeSpan.Zero, gll.FixTime); } + + + [TestMethod] + public void TestGpbod_Empty() + { + string input = "$GPBOD,,T,,M,,*47"; + var msg = NmeaMessage.Parse(input); + Assert.IsInstanceOfType(msg, typeof(Gpbod)); + Gpbod bod = (Gpbod)msg; + Assert.AreEqual(double.NaN, bod.TrueBearing, "TrueBearing"); + Assert.AreEqual(double.NaN, bod.MagneticBearing, "MagneticBearing"); + Assert.IsNull(bod.OriginID, "OriginID"); + Assert.IsNull(bod.DestinationID, "DestinationID"); + } + + [TestMethod] + public void TestGpbod_GoToMode() + { + string input = "$GPBOD,099.3,T,105.6,M,POINTB,*48"; + var msg = NmeaMessage.Parse(input); + Assert.IsInstanceOfType(msg, typeof(Gpbod)); + Gpbod bod = (Gpbod)msg; + Assert.AreEqual(99.3, bod.TrueBearing, "TrueBearing"); + Assert.AreEqual(105.6, bod.MagneticBearing, "MagneticBearing"); + Assert.AreEqual("POINTB", bod.DestinationID, "DestinationID"); + Assert.IsNull(bod.OriginID, "OriginID"); + } + + + [TestMethod] + public void TestGpbod() + { + string input = "$GPBOD,097.0,T,103.2,M,POINTB,POINTA*4A"; + var msg = NmeaMessage.Parse(input); + Assert.IsInstanceOfType(msg, typeof(Gpbod)); + Gpbod bod = (Gpbod)msg; + Assert.AreEqual(97d, bod.TrueBearing, "TrueBearing"); + Assert.AreEqual(103.2, bod.MagneticBearing, "MagneticBearing"); + Assert.AreEqual("POINTB", bod.DestinationID, "DestinationID"); + Assert.AreEqual("POINTA", bod.OriginID, "OriginID"); + } } }