# Calculate checksum of NMEA-protocol

Discussion in 'Questions (Windows Mobile)' started by schimanski, Nov 23, 2008.

Hello together!

I have created a sub, which add a checksum to a NMEA-protocol. My question is: is there an easier way to calculate the checksum? I think, that my sub sometimes works and sometimes it doesn`t.

NMEA is the NMEA-protocol as string without the checksum,

Code:
`Sub Globals  Dim buffer(0) as ByteEnd SubSub Checksum_NMEA  buffer()=bit_NMEA.stringToBytes(NMEA,1,StrLength(NMEA)-2)  laenge=ArrayLen(buffer())  summe=buffer(0)  for j=1 to laenge-1    summe=bit_NMEA.XOR(summe,buffer(j))  next  checksum=bit_NMEA.DecToHex(summe)  NMEA=NMEA & checksum & crlfEnd Sub`

schimanski

Looks and tests fine for me, admittedly on a limited set of data. However I think that the checksum should always be two characters and your Sub could fail because bit_NMEA.DecToHex doesn't return two characters for a number less than 16 so you should check for this and correct it.

Code:
`...  checksum=bit_NMEA.DecToHex(summe)  If summe < 16 Then    checksum = "0" & checksum   End If  ...`

Thanks, agraham!

I will correct my code. This could be the answer, why my navigationsoftware tomtom sometimes accept the protocoll and sometimes not.

`Sub Globals   'dll: bitwise.dll, object: bit   nmea = "\$GPGGA,102334.994,4841.7903,N,01004.0063,E,0,00,50.0,572.3,M,,,,0000*3F" 'example   Dim checksum   n = 1End SubSub App_Start   Form1.Show   bit.New1   sign = StrAt(nmea,1)   Do Until sign = "*"      sign = Int(Asc(sign))      checksum = bit.XOR(checksum,sign)      n = n + 1      sign = StrAt(nmea,n)   Loop   Msgbox("ASCII:  " & checksum & CRLF & "HEX:    " & StrToUpper(bit.DecToHex(checksum)))End Sub`