Author Topic: CheckSum formula help  (Read 5269 times)

0 Members and 1 Guest are viewing this topic.

Offline BryanTopic starter

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: ca
CheckSum formula help
« on: October 06, 2016, 09:44:59 am »
Hello:

Not sure if this is a programming question or more of a math question, but I am completely stumped in figuring out the below excerpt from a data sheet as it applies to determining the checksum.

For example the following 0xFF,0x81,0xC5, 0x04 are the 4 words and the fifth word 0x09 is the checksum. I assume from what it states, just add the bytes together and then mod 216. If I do this this gives me 0x33, so then if I even I calculated it right I am stumped on the twos compliment and how the end result is 0x09 :-[ :-[

-=Bryan=-
 

Offline hasithvm

  • Contributor
  • Posts: 24
  • Country: ca
Re: CheckSum formula help
« Reply #1 on: October 06, 2016, 10:10:41 am »
The documentation specifies word, not byte, and based on what I could find on Googling the spec text, you're looking at a NavCore V receiver.

It uses 16-bit words in little endian order (least significant byte first), so word 1 is 0x81FF, word 2 is 0x04C5, and the 0x09 byte is the first half of word #3. Add the first four words up, discard bits > 16 bits (AND with 0xFFFF), and negate the sum, bearing the special case in mind.


See: users.rcn.com/mardor/serial.pdf

« Last Edit: October 06, 2016, 10:12:43 am by hasithvm »
 

Offline BryanTopic starter

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: ca
Re: CheckSum formula help
« Reply #2 on: October 06, 2016, 09:53:34 pm »
Thank you very much. for some reason thought the "words" can range from 8-32 bytes depending on the type, but it's pretty clear.  Closer read and it references "words" as "to specify a quantity which occupies 16 bit of storage".

Here is the complete statement,

FF 81 C5 04 09 00 00 00 33 79 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FC FF

so if I got it right the second time around Word 1 is FF,81 (0x81FF), Word 2 is C5,04 (0x4C5), Word 3 is 09,00 (0x09, Word4 is 00,00 (0x00), Word 5 the Header checksum is 33,79 (0x7933)

add Words 1-4 up and it's 0x86CD or 1000 0110 1100 1101 , discard >16 bits (AND 0xFFFF) . This is where I am getting lost... AND 0xFFFF, doesn't that just return the same?

I do note and maybe it's a coincidence but the sum of the 4 words is 0x86CD or
1000 0110 1100 1101 and if bitwise NOT it becomes 111 1001 0011 0010 or 0x7933 the checksum :-//. But from what I understand the NOT is equiv to a twos complement -1 |O

« Last Edit: October 06, 2016, 09:57:41 pm by Bryan »
-=Bryan=-
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2340
Re: CheckSum formula help
« Reply #3 on: October 06, 2016, 10:13:45 pm »
Thank you very much. for some reason thought the "words" can range from 8-32 bytes
No!
This:
Quote
Closer read and it references "words" as "to specify a quantity which occupies 16 bit of storage".
Words are usually two bytes.

Quote
Here is the complete statement,

FF 81 C5 04 09 00 00 00 33 79 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FC FF
Breaking this into words and adding.
Code: [Select]
   81ff
   04c5
   0009
   0000
   7933
   0000
   0004
   0000
    ...      etc
 + fffc
I get the answer 20000 hex. which becomes 0000 when the upper bits are removed.


 

Offline BryanTopic starter

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: ca
Re: CheckSum formula help
« Reply #4 on: October 06, 2016, 10:30:15 pm »
Andy 33,79 0x7933 (word 5) is the checksum of words 1-4. that is the part I am stuck on, trying to determine how words 1-4 equate to the Word 5 checksum given in the statement

Code: [Select]
Word 5 of the message header is the data checksum, used to validate the header portion of the message.

It is computed by summing (modulo 216) all words (including the word containing DEL and SOH) contained in the header and then performing a two’s complement on the sum. SUM = Mod 216 ?4 word(i).

The computation of the header checksum may be expressed mathematically as: if sum = –32768, header checksum = SUM; else header checksum = –SUM where: a.

Unary negation is computed as the two’s complement of a 16-bit data word. b. Mod 216 indicates the least 16 bits of an arithmetic process.

That is, carry bits from bit position 16 are ignored. c. The summation is the algebraic binary sum of the words indicated by the subscript i. d. The –32768 sum value must be treated as a special case since it cannot be negated.

(NOTE: A CURRENT BUG CAUSES CHECKSUM ERRORS FOR A VALUE OF ZERO or –32 768)
« Last Edit: October 06, 2016, 10:48:09 pm by Bryan »
-=Bryan=-
 

Offline hasithvm

  • Contributor
  • Posts: 24
  • Country: ca
Re: CheckSum formula help
« Reply #5 on: October 06, 2016, 10:49:44 pm »
so if I got it right the second time around Word 1 is FF,81 (0x81FF), Word 2 is C5,04 (0x4C5), Word 3 is 09,00 (0x09, Word4 is 00,00 (0x00), Word 5 the Header checksum is 33,79 (0x7933)

add Words 1-4 up and it's 0x86CD or 1000 0110 1100 1101 , discard >16 bits (AND 0xFFFF) . This is where I am getting lost... AND 0xFFFF, doesn't that just return the same?

Nope, that's still right. It's for the case when the sum might be greater than 16-bits, discard any bit @ positions > 16. 

Quote
I do note and maybe it's a coincidence but the sum of the 4 words is 0x86CD or
1000 0110 1100 1101 and if bitwise NOT it becomes 111 1001 0011 0010 or 0x7933 the checksum :-//. But from what I understand the NOT is equiv to a twos complement -1 |O
Almost!

0x86CD = 1000 0110 1100 1101

Two's complement is bitwise invert (~) and add 1:

~(0x86CD) = 0111 1001 0011 0010
~(0x86CD) + 1 = 0111 1001 0011 0011
=0x7933


You made a mistake in your converting the last nybble, I think. 0010 is 0x2, not 0x3.
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2340
Re: CheckSum formula help
« Reply #6 on: October 06, 2016, 11:10:33 pm »
Andy 33,79 0x7933 (word 5) is the checksum of words 1-4. that is the part I am stuck on, trying to determine how words 1-4 equate to the Word 5 checksum given in the statement
Ok, try this:
Code: [Select]
   81ff
   04c5
   0009
   0000
 + 7933
I get the answer 10000 hex. which becomes 0000 when the upper bits are removed.
 

Offline BryanTopic starter

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: ca
Re: CheckSum formula help
« Reply #7 on: October 07, 2016, 06:27:49 am »
Quote
Almost!

0x86CD = 1000 0110 1100 1101

Two's complement is bitwise invert (~) and add 1:

~(0x86CD) = 0111 1001 0011 0010
~(0x86CD) + 1 = 0111 1001 0011 0011
=0x7933

You made a mistake in your converting the last nybble, I think. 0010 is 0x2, not 0x3.

Thank you very much, that works, tried a couple other example statements and they all worked. Goes to show how one like myself who gets playing around with the Arduino and the higher level programming languages lose the basics <g>
« Last Edit: October 07, 2016, 06:45:26 am by Bryan »
-=Bryan=-
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4676
  • Country: us
Re: CheckSum formula help
« Reply #8 on: October 07, 2016, 10:39:54 am »
Quote
just add the bytes together and then mod 216
It's NOT "modulo 216", it's "modulo 216 (2 to the 16th power) !
(I think that's already been made clear, but no one has exactly said it.)
 

Offline BryanTopic starter

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: ca
Re: CheckSum formula help
« Reply #9 on: October 07, 2016, 06:32:23 pm »
Quote
just add the bytes together and then mod 216
It's NOT "modulo 216", it's "modulo 216 (2 to the 16th power) !
(I think that's already been made clear, but no one has exactly said it.)

Yes, cutting from the datasheet didn't help either as they quote it as MOD 216 or Modulo 216.
-=Bryan=-
 

Offline hasithvm

  • Contributor
  • Posts: 24
  • Country: ca
Re: CheckSum formula help
« Reply #10 on: October 07, 2016, 06:55:39 pm »
Quote
just add the bytes together and then mod 216
It's NOT "modulo 216", it's "modulo 216 (2 to the 16th power) !
(I think that's already been made clear, but no one has exactly said it.)

Yes, cutting from the datasheet didn't help either as they quote it as MOD 216 or Modulo 216.

Seems like the datasheet author didn't know how to typeset superscripts...
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->