Checksum in nmea

Discussion in 'Code Samples & Tips' started by wolfgang, Feb 15, 2008.

  1. wolfgang

    wolfgang Member Licensed User

    Hi,
    maybe someone can use it:
    Code:
    Sub Globals
       
    'dll: bitwise.dll, object: bit
       nmea = "$GPGGA,161649.417,4841.7903,N,01004.0221,E,2,05,2.0,578.3,M,48.0,M,1.0,0000*7D" 'example
       Dim checksum
       n = 
    1
    End Sub

    Sub 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
     
  2. Stulish

    Stulish Active Member Licensed User

    B4A NMEA Checksum update

    Hi wolfgang,

    You inspired me to create the B4A version of the checksum

    So i put together the subroutine below to calculate the checksum. i have worked with NMEA a lot with Visual Basic and found as the data was streaming the buffer might not hold a full sentence thus the code below checks the NMEA for a start position (stPos) which is 1 character after the $ sign and it also checks for the stop position (spPos), which is 1 character before the * sign. If there is not a complete sentence then the checksum is not returned just a null value.

    Not sure if this will be any use to anyone :D

    Code:
    Sub NMEA_Checksum(NMEA As String)
    Dim stPos As Int, spPos As Int, a As Int, checksum As Int, charInt As Int,chk As String 
    a=
    0
    Do While a<NMEA.Length-1
       
    If NMEA.CharAt(a)="$" AND stPos=0 Then stPos=a+1
       
    If NMEA.CharAt(a)="*" AND spPos=0 Then spPos=a-1
       
    If stPos<>0 AND spPos<>0 Then a=NMEA.Length-2
       a=a+
    1
    Loop
    checksum=
    0 
    If stPos<>0 AND spPos<>0 AND spPos>stPos Then 
       
    For a=stPos To spPos
          charInt = 
    Asc(NMEA.CharAt(a))
          checksum = 
    Bit.Xor(checksum,charInt)
       
    Next
        
    Msgbox("ASCII:  " & checksum & CRLF & "HEX:    " & Bit.ToHexString(checksum).ToUpperCase ,"CHECKSUM")
       
    Return Bit.ToHexString(checksum).ToUpperCase
    Else
       
    Return Null
    End If
    End Sub
     
    Last edited: Jun 25, 2012
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice