CRC’s are normally 2 or 4 bytes, whereas a Checksum is a single byte that is computed by adding together the value of each character in a buffer. The high 8 bits are thrown away leaving an 8 byte checksum.
Instead of simply adding the values together, some implementations Exclusive OR the values, which offers slightly better error correction.
The following function can be used to compute a Checksum and with a slight modification, an XOR Checksum. This example uses a CSTRING as the source of data since it is assumed you will be working with data received from the Serial Port.
Prototype:
GetChecksum(*CSTRING),BYTE
Procedure:
GetChecksum FUNCTION(RStr)
Cnt SHORT
StrLen SHORT
Tally SHORT
ChkSum BYTE
CODE
Tally = 0
StrLen = Len(Clip(RStr))
! Tally up the Checksum
! Comment Out or Remove the Line inside
! the Loop that you do not need.
Loop Cnt = 1 to StrLen
! Compute a Regular Checksum
Tally += Val(RStr[Cnt])
! Compute an Exclusive OR Checksum
Tally = BXOR(Val(RStr[Cnt]),Tally)
End
ChkSum = Tally ! Checksum = lower 8 bits
Return(ChkSum) 