118150-001 REV. B
Page 9 of 66
If the DSP detects a checksum error, the received message is ignored
– no
acknowledge or data is sent back to the host. A timeout will act as an implied
NACK.
Here is another example, this time for command 22 (Request Status) which has
no arguments.
The original message with a placeholder for checksum is:
<STX>22,<CS><ETX>
First, you add up all the characters starting with the ‘2’ in the command number
to the comma before the checksum with their ASCII values (in hexadecimal):
0x32 + 0x32 + 0x2C = 0x90
Next, you then take the two’s complement of that number by negating it, by
subtracting it from 0x100 (decimal 256), and only retain the lowest 7 bits by
bitwise ANDing the results with 0x7F:
This combines the steps of getting the twos complement, truncating the result to
8 bits and clearing the 8
th
bit.
(0x100
– 0x90) & 0x7F = 0x70
Finally, bitwise OR the result with 0x40:
0x70 | 0x40 = 0x70
The checksum byte is 0x70 (Decimal 112, ASCII: p)
The following is sample code, written in Visual Basic, for the generation of
checksums:
Public Function ProcessOutputString(outputString As String) As String
Dim i As Integer
Dim CSb1 As Integer
Dim CSb2 As Integer
Dim CSb3 As Integer
Dim CSb$
Dim X
X = 0
For i = 1 To (Len(outputString)) 'Starting with the CMD character
X = X + Asc(Mid(outputString, i, 1)) 'adds ascii values together
Next i
CSb1 = 256 - X
'Twos Complement
CSb2 = 63 And (CSb1)
CSb3 = 64 Or (CSb2) 'OR 0x40
CSb$ = Chr(Val("&H" & (Hex(CSb3))))