PDA

View Full Version : Strange CRC.


rack
08-08-2008, 03:27 PM
Hi,

I try to identify a serial protocol for some devices.

The transmitted strings are in this form (example):

0180007A0405

the last 2 digit are the checksum modulo 256 (100h) of the previous digits as ascii characters (hex):

0=30+
1=31+
8=38+
0=30+
0=30+
0=30+
7=37+
A=41+
0=30+
4=34

summ = 205

CRC= 205 mod 100 = 05

the bytes 7A and 04 are another checksum (BCC) for the digits before those bytes (01 80 00).

This is the only description I've found about this check:

-----
Each message contains a Block Check Sum word BCC, composed by two
bytes ordered as BCC1 & BCC2 which are calculated, using one's complement arithmetic (a carry
resulting from a two byte sum is added to the result), with the following procedure:

- Initialize S1(0)=0 and S2(0)=0

- Perform partial calculation using all message's N bytes B(n) starting form first byte B(1)

S1(n) = S1(n-1) + B(n)
S2(n) = S2(n-1) + S1(n)

- Calculate checksum bytes as

BCC1 = - (S1(N) + S2(N))
BCC2 = S2(N)
-----

I've followed the above steps but the results are always wrong...

Any suggestions, please ?

Thanks in advance.

kao
08-09-2008, 10:46 AM
See Wikipedia for one's complement (http://en.wikipedia.org/wiki/Signed_number_representations#Ones.27_complement)

Here's how it gets calculated:

index | 00 | 01 | 02 | 03
--------------------------
b(x) | .. | 01 | 80 | 00
s1(x) | 00 | 01 | 81 | 81
s2(x) | 00 | 01 | 82 | 04 (*1)

bcc1 = ~(81+4) = ~85 = 7A (*2)
bcc2 = 04


Notes.
*1) 0x81 + 0x82 = 0x103 = 4 ("a carry resulting from a two byte sum is added to the result")
*2) one's complement to 85 is 7A. Its not the same as minus sign in ordinary arithmetics.

Have a nice day,
kao.

rack
08-09-2008, 02:55 PM
Thank you very much kao!!!

My mistake was think at the "BCC" as the "CRC" (last byte) and operate with the ascii values (30,31,38,30,...) instead the simple bytes.

Regards.