Added Checksum property to message and ToString

This commit is contained in:
mort5161 2015-03-20 15:14:58 -07:00
parent eb1d80184d
commit dfc02b986c
2 changed files with 29 additions and 2 deletions

View file

@ -77,7 +77,6 @@ namespace NmeaParser.Nmea
int checksumTest = 0;
for (int i = 1; i < message.Length; i++)
{
if (i == 0) continue;
checksumTest ^= Convert.ToByte(message[i]);
}
if (checksum != checksumTest)
@ -160,7 +159,29 @@ namespace NmeaParser.Nmea
/// </returns>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "${0},{1}", MessageType, string.Join(",", MessageParts));
return string.Format(CultureInfo.InvariantCulture, "${0},{1}*{2:X2}", MessageType, string.Join(",", MessageParts), Checksum);
}
/// <summary>
/// Gets the checksum value of the message.
/// </summary>
public byte Checksum
{
get
{
int checksumTest = 0;
for (int j = -1; j < MessageParts.Count; j++)
{
string message = j < 0 ? MessageType : MessageParts[j];
if (j >= 0)
checksumTest ^= 0x2C; //Comma separator
for (int i = 0; i < message.Length; i++)
{
checksumTest ^= Convert.ToByte(message[i]);
}
}
return Convert.ToByte(checksumTest);
}
}
internal static double StringToLatitude(string value, string ns)

View file

@ -41,6 +41,12 @@ namespace NmeaParser.Tests
{
var msg = NmeaMessage.Parse(line);
Assert.IsNotNull(msg);
var idx = line.IndexOf('*');
if (idx >= 0)
{
byte checksum = (byte)Convert.ToInt32(line.Substring(idx + 1), 16);
Assert.AreEqual(checksum, msg.Checksum);
}
Assert.IsNotInstanceOfType(msg, typeof(Nmea.UnknownMessage), "Type " + msg.MessageType + " not supported");
}
}