Simple 1 Byte Checksum Method


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2013
    Posts
    41

    Default Simple 1 Byte Checksum Method

    Hello,

    I need to encode/decode a simple 8Bit Checksum based on a transmitter which has no documentation.

    Apparently, it is just using the last 2 Bytes of a 10 Byte packet to calculate it.
    (Figured THAT out by dumb luck.)

    Using a Logic Analyzer, it appears it is the simple sum of these two bytes.
    Payload[8] + Payload[9] = CheckSum
    5 + 2 = 7
    245 + 2 = 247
    53 + 1 = 54
    54 + 1 = 55
    21 + 2 = 23
    etc, etc,

    I have not been able to capture what happens over 255 but assume it must roll-over somehow?

    Browsing all the "CheckSum" threads here, I think the code may be something like the following:
    ----------------------------------
    Payload VAR BYTE[11]
    (Payload elements...)
    CheckWord VAR WORD
    CheckSum VAR BYTE


    CheckWord = Payload[8] + Payload[9]
    CheckSum = CheckWord.byte0 ^ $FF '<--- This?
    'CheckSum = CheckWord // 256 '<--- Or That?
    -----------------------------------


    Any ideas?

    Thanks,

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    would it not be easier to

    if Payload[10] -( Payload[8] + Payload[9]) > 0 then goto badckecksum

    or conversely

    Payload[10] =Payload[8] + Payload[9] to create chksum
    Last edited by richard; - 6th March 2015 at 20:34. Reason: not sure if creating or checking chksum

  3. #3
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    Right. That's simple.
    Problem is when they add up greater than 256 becoming a WORD.

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    it can't become a word if the var is declare as a byte it just rolls over .
    Payload var byte[11]

    btw the transmitter its not a fineoffset weatherstation ?

  5. #5
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    It can't be that simple?
    What if there were more than 2 Bytes like 10?

    No it's not a weatherstation, more like a entry door counter.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    a byte can hold a value between 0 and 255
    255 + 1=0
    255+10=9
    255+255+255+1= 254

  7. #7
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    Richard,

    Wow!
    That is so clean and easy.
    (I do have a tendency of making things complicated.)

    Thank you very much!

    Jay Zebryk
    Southbridge, Massachusetts

  8. #8
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Simple 1 Byte Checksum Method

    This information may not pertain to your current goal since you are trying to decode an existing checksum method being used by your transmitter.

    However, for a simple tried and true method of byte level checksums on a datastream, the Intel Hex8 checksum method works well and is very simple.
    I use this method in many projects with good success.

    The Intel Hex8 file (I8HEX) checksum is the 2s compliment of the sum of the payload bytes.
    The beauty of using the 2s compliment of the sum of the payload bytes is that the validation check is simple.
    To validate the payload at the receiving end, the receiver sums the received payload bytes including the received checksum byte.
    The result should equal zero (0).

    This works because of the use of unsigned byte variables and the rollover from 255 to 0 for overflows.
    In this context a number plus the 2s compliment of itself will equal zero (0) as a 2s compliment of a number behaves like the negative of the original number.

    Computing the 2s compliment of a byte can be done by first computing the 1s compliment of the number, then adding one to the result.
    1scomp = (number ^ $FF) This computes the 1s compliment
    2scomp = 1scomp + 1

    For example:
    number = 73
    1scomp = (73 ^ $ff) = 182
    2scomp = 182 + 1 = 183

    To validate just add the number to its 2s compliment.
    73 + 183 = 0

    This checksum method can be used for just about any length of payload bytes.
    Regards,
    TABSoft

Similar Threads

  1. Reading a speed sensor best method?
    By davewanna in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 27th January 2012, 03:37
  2. Calculate Byte Value Checksum in PBP Pro
    By Pedro Pinto in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 8th July 2009, 23:50
  3. ASM Addressing method help needed
    By sougata in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th January 2007, 18:01
  4. Current Calculation Method
    By DynamoBen in forum Off Topic
    Replies: 4
    Last Post: - 16th September 2006, 10:21
  5. Which is the method to use
    By dtit in forum General
    Replies: 0
    Last Post: - 18th January 2005, 10:53

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts