nmea checksum in c# .net cf -
i'm trying write own nmea parser,since need info gps, , don't need interpret messages. problem have nmea message validator gives me wrong checksum. can see i'm droing wrong?
i'm using idea codepedia - calculating , validating nmea sentences.
// returns true if sentence's checksum matches // calculated checksum // calculates checksum sentence private static bool isvalid(string sentence) { if (sentence == "") return false; if (sentence.length < 14) return false; try { string[] words = getwords(sentence); log.writetolog(words); int checksum = 0; string checktocompare = words[words.length - 1]; stringbuilder sb = new stringbuilder(); (int = 1; < (words.length - 2); i++) { sb.append(words[i]); } string sentecentoparse = sb.tostring(); foreach (char chary in sentecentoparse) { checksum ^= convert.tobyte(chary); } log.writetolog("checksum: " + checksum.tostring("x2")); log.writetolog("obtained checksum: " + checktocompare); return string.equals(checksum.tostring("x2"), checktocompare, stringcomparison.ordinalignorecase); } catch (exception exc) { log.writetolog("exception caused sentence:" + sentence); log.writetolog("exception error message: " + exc.message); return false; } } // divides sentence individual words public static string[] getwords(string sentence) { char[] separator = { ',', '*' }; return sentence.split(separator); }
run log
10:30:07 23-02-2011 opening port 10:30:08 23-02-2011 opened port 10:30:10 23-02-2011 processing data 10:30:12 23-02-2011 $gpgga 10:30:12 23-02-2011 102957.92 10:30:12 23-02-2011 4104.8569 10:30:12 23-02-2011 n 10:30:12 23-02-2011 00836.4700 10:30:12 23-02-2011 w 10:30:12 23-02-2011 1 10:30:12 23-02-2011 4 10:30:12 23-02-2011 15.100 10:30:12 23-02-2011 157.133 10:30:12 23-02-2011 m 10:30:12 23-02-2011 52.386 10:30:12 23-02-2011 m 10:30:12 23-02-2011 0 10:30:12 23-02-2011 0 10:30:13 23-02-2011 79 10:30:13 23-02-2011 checksum: 6d 10:30:13 23-02-2011 obtained checksum: 79 10:30:13 23-02-2011 invalid sentence 10:30:13 23-02-2011 $gprmc 10:30:13 23-02-2011 102957.92 10:30:13 23-02-2011 10:30:13 23-02-2011 4104.8569 10:30:13 23-02-2011 n 10:30:13 23-02-2011 00836.4700 10:30:13 23-02-2011 w 10:30:13 23-02-2011 0.000 10:30:13 23-02-2011 5.822 10:30:13 23-02-2011 230211 10:30:13 23-02-2011 0 10:30:13 23-02-2011 w 10:30:14 23-02-2011 10:30:14 23-02-2011 2b 10:30:14 23-02-2011 checksum: 4e 10:30:15 23-02-2011 obtained checksum: 2b 10:30:15 23-02-2011 invalid sentence
if use function page linked:
private static string getchecksum(string sentence) { //start first item int checksum= convert.tobyte(sentence[sentence.indexof('$')+1]); // loop through chars checksum (int i=sentence.indexof('$')+2 ; i<sentence.indexof('*') ; i++) { // no. xor checksum character's value checksum^=convert.tobyte(sentence[i]); } // return checksum formatted two-character hexadecimal return checksum.tostring("x2"); }
and pass in string, $ start , * @ end (before checksum), , commas between words, works.
string s1 = getchecksum("$gpgga,102957.92,4104.8569,n,00836.4700,w,1,4,15.100,157.133,m,52.386,m,0,0*79"); string s2 = getchecksum("$gprmc,102957.92,a,4104.8569,n,00836.4700,w,0.000,5.822,230211,0,w,a*2b");
s1 = 79 , s2 = 2b hope.
Comments
Post a Comment