mirror of
https://github.com/dotMorten/NmeaParser.git
synced 2026-01-06 00:30:04 +01:00
Added Checksum property to message and ToString
This commit is contained in:
parent
eb1d80184d
commit
dfc02b986c
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue