Calculating a checksum


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Default Calculating a checksum

    Hello all

    I realise this may seem easy to some of you, but I am having some difficulties grasping the concept and executing the generation of a checksum.

    What I have done is extract data from a GPS and parse them into one payload of ascii characters like this:

    0320080914221805049972100017555023245

    Which consists of the date, time position, speed course etc.
    What I now need to do is calculate a checksum and send the data in binary format to a modem.

    Can I just send the string out on the serial port? At present the message in ASCII is 37 bytes, one for each character, will sending them out as is result in the binary represenation of the ASCII code for that number being transmitted?

    If so, this is not what I want, I would like to convert the string into BCD [I am under the impression that this should basically halve the payload] calculate a checksum, count the bytes [checksum included] and send it to
    the modem in binary format.

    Sorry if this is obvious but I am at a loss here.

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Did you search here? It must've been covered some time or other. However, the easy way is to convert the ascii to packed BCD values and then simply add the values of each such byte into a 16 bit variable. At the end of processing, you will have your LRC or checksum.

    So, 032008091422 (12 bytes)
    would become
    03 20 08 09 14 22 (6 bytes)

    Now, you add 03+20+08+09+14+22 to get the 16 bit checksum.

  3. #3
    Join Date
    Apr 2009
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Hi Jerson

    Thanks for the reply!

    I did do a search, but nothing seemed to go into enough detail, I am still rather new at this
    I understand the concept, but when it comes to implementing it, I get lost.

    The initial string is 37 bytes long, can I do the procedure with such a long string, or am I better off splitting the string into sections?

    So do I take 032008091422
    Break the string up like so 03 20 08 09 14 22
    Convert the ascii in decimal equivalent
    Convert decimal into BCD [How do I do this?]
    and then add to get the checksum?

    Thanks again

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    If you're not too keen on saving up on the transmitted bytes, this is the easy way out.

    Code:
    MyString:    var   byte[30]           ' this is where the string will lie
    CheckSum:  var   word               ' quite obvious, eh?
    
    LocalCnt:     var   byte                ' a counter
    
    Checksum_it:
              CheckSum = 0                  ' start with 0
              for LocalCnt = 1 to 30       ' change this to suit the length of string 
                                                       ' you will parse
                       ' add the value of the character you read to checksum
                       ' after removing the ascii bias of character '0'
                       Checksum = Checksum + MyString[LocalCnt]-$30
              next
              return                            ' your checksum is cooked and ready to eat :)
    
    main:
              gosub CheckSum_it
              gosub  SendTheString           ' you might be having this
              hserout [Checksum]              ' send the checksum
     
              goto main

    This code is a concept - not tested or certified. It may not work as I am rusty these days. Too little rain with high humidity does this often

    Use this as a guide to refine your thoughts
    Good luck

  5. #5
    Join Date
    Apr 2009
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Thank you!

Similar Threads

  1. NMEA CheckSum Routine
    By Tom Gonser in forum Code Examples
    Replies: 2
    Last Post: - 6th June 2015, 22:24
  2. 4 Bytes one button press
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th January 2010, 22:36
  3. Checksum
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th October 2009, 04:09
  4. Creating Checksum in PBP
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 16th March 2005, 04:49
  5. Checksum problem!
    By atomski in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 3rd November 2004, 07:21

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