PDA

View Full Version : Calculating a checksum



simongie
- 21st August 2009, 09:59
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.

Jerson
- 21st August 2009, 10:15
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.

simongie
- 21st August 2009, 10:25
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

Jerson
- 21st August 2009, 12:27
If you're not too keen on saving up on the transmitted bytes, this is the easy way out.




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

simongie
- 21st August 2009, 13:08
Thank you!