如何判断 NMEA 或 TAIP 句子是否有效
本文关键字:TAIP 句子 是否 有效 NMEA 何判断 判断 | 更新日期: 2023-09-27 18:30:53
这个问题(我打算自己回答,以自私地提高我的微观声誉)是 c# 代码,用于判断 NMEA 或 TAIP 格式的给定句子是否有效...... 本质上是计算校验和应该是什么,并将其与给定的校验和进行比较,并返回真或假......
这是我想出的代码...一些功劳归功于 Carson63000 提供的答案
这里class Program
{
static void Main(string[] args)
{
Console.WriteLine(IsNMEAMessageValid("$GPRMC,102957.92,A,4104.8569,N,00836.4700,W,0.000,5.822,230211,0,W,A*2B").ToString());
Console.WriteLine(IsNMEAMessageValid("$GPRMC,102957.92,A,4104.8569,N,00836.4700,W,0.000,5.822,230211,0,W,A*1B").ToString()); // should be false, changed the checksum from 2B to 1B
Console.WriteLine(IsTAIPMessageValid(">RPV68168+4008572-0828021400026912;ID=M123;*d<").ToString());
Console.WriteLine(IsTAIPMessageValid(">RPV68168+4008572-0828021400026912;ID=M123;*c<").ToString()); // should be false, changed the checksum from d to c
Console.WriteLine(IsTAIPMessageValid(">RPV54366+4001403-0828656300005512;ID=GH75;*7c<").ToString());
Console.WriteLine(IsTAIPMessageValid(">RPV54366+4001403-0828656300005512;ID=GH75;*7b<").ToString()); // should be false, changed the checksum from 7c to 7b
Console.ReadLine();
}
private static bool IsNMEAMessageValid(string sentence)
{
// Checksum for NMEA includes XOR on all characters between (not including) the $ and the *, including the commas...
int checksum = Convert.ToByte(sentence[sentence.IndexOf('$') + 1]);
for (int i = sentence.IndexOf('$') + 2; i < sentence.IndexOf('*'); i++)
{
checksum ^= Convert.ToByte(sentence[i]);
}
int givenChecksum = Convert.ToInt16(sentence.Split('*')[1], 16);
return checksum == givenChecksum;
}
private static bool IsTAIPMessageValid(string sentence)
{
// Checksum for TAIP includes XOR on all characters starting with > and up to and including *
int checksum = Convert.ToByte(sentence[sentence.IndexOf('>')]);
for (int i = sentence.IndexOf('>') + 1; i < sentence.IndexOf('*') + 1; i++)
{
checksum ^= Convert.ToByte(sentence[i]);
}
char[] splitChars = { '*', '<' };
int givenChecksum = Convert.ToInt16(sentence.Split(splitChars)[1], 16);
return checksum == givenChecksum;
}
}