CheckSum formula help - Page 1
Forum section: electronics / general

author: Bryan
author_rank: Frequent Contributor
timestamp: October 06, 2016, 09:44:59 am
content: 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

author: hasithvm
author_rank: Contributor
timestamp: October 06, 2016, 10:10:41 am
content: 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

author: Bryan
author_rank: Frequent Contributor
timestamp: October 06, 2016, 09:53:34 pm
content: 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 FFso 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 or1000 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

author: Andy Watson
author_rank: Super Contributor
timestamp: October 06, 2016, 10:13:45 pm
content: Quote from: Bryan on October 06, 2016, 09:53:34 pmNo!This:QuoteWords are usually two bytes.QuoteBreaking this into words and adding.Code:81ff04c5000900007933000000040000... etc+ fffcI get the answer 20000 hex. which becomes 0000 when the upper bits are removed.

author: Bryan
author_rank: Frequent Contributor
timestamp: October 06, 2016, 10:30:15 pm
content: 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 statementCode: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)

author: hasithvm
author_rank: Contributor
timestamp: October 06, 2016, 10:49:44 pm
content: Quote from: Bryan on October 06, 2016, 09:53:34 pmNope, that's still right. It's for the case when the sum might be greater than 16-bits, discard any bit @ positions > 16.QuoteAlmost!0x86CD = 1000 0110 1100 1101Two's complement is bitwise invert (~) and add 1:~(0x86CD) = 0111 1001 00110010~(0x86CD) + 1 = 0111 1001 0011 0011=0x7933You made a mistake in your converting the last nybble, I think. 0010 is 0x2, not 0x3.

author: Andy Watson
author_rank: Super Contributor
timestamp: October 06, 2016, 11:10:33 pm
content: Quote from: Bryan on October 06, 2016, 10:30:15 pmOk, try this:Code:81ff04c500090000+ 7933I get the answer 10000 hex. which becomes 0000 when the upper bits are removed.

author: Bryan
author_rank: Frequent Contributor
timestamp: October 07, 2016, 06:27:49 am
content: QuoteThank 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>

author: westfw
author_rank: Super Contributor
timestamp: October 07, 2016, 10:39:54 am
content: QuoteIt'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.)

author: Bryan
author_rank: Frequent Contributor
timestamp: October 07, 2016, 06:32:23 pm
content: Quote from: westfw on October 07, 2016, 10:39:54 amYes, cutting from the datasheet didn't help either as they quote it as MOD 216 or Modulo 216.

author: hasithvm
author_rank: Contributor
timestamp: October 07, 2016, 06:55:39 pm
content: Quote from: Bryan on October 07, 2016, 06:32:23 pmSeems like the datasheet author didn't know how to typeset superscripts...