BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 12:40 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Reading two bytes as one Hex; Checksum
PostPosted: Sat Nov 13, 2010 8:17 pm 
Offline
New User

Joined: Sat Nov 13, 2010 8:01 pm
Posts: 2
Hi!

I'm currently able to communicate with an external device via HSERIN/OUT. The protocol I'm following sends several ASCII bytes that I receive in separate variables.

Example:
HSERIN [Start,Command1,Command2,Address1,Address2,CS1,CS2]

All variables are declared as bytes.

Now, I have to calculate a Checksum. The external device will send the Hex of the checksum, but split into two bytes. I'll try to elaborate....

Lets say the check sum was decimal 162, which is Hex 0xA2. So, I would receive two separate bytes, "A" and "2". So, CS1=A (hex 0x41) and CS2=2 (hex 0x32).

When I calculate the Checksum myself, I get 0xA2 (dec 162), which is correct. But I need to compare that value with what was received in variables CS1 & CS2. But of course, those are two separate values at this point.

So to sum it up: I need to read two bytes: "A" and "2", which I must combine somehow into a hex = 0xA2. How do I receive "A" and "2" as a single Hex value to be 0xA2? And these two bytes are being read and stored into CS1 and CS2, currently with no modifiers.

I hope that makes sense. Any help would be appreciated. And I can clarify anything as needed. Thanks!


Top
 Profile  
 
 Post subject: Re: Reading two bytes as one Hex; Checksum
PostPosted: Sun Nov 14, 2010 9:31 pm 
Offline
New User

Joined: Sat Nov 13, 2010 8:01 pm
Posts: 2
Solved it! Gotta love when you solve your own problems. Here's how "I" did it....

The 'rundown:
- The external device sends the Checksum's hex value as two separate bytes (Ex: checksum of 0xC7 is sent as ASCII bytes "C" and "7").

- My device calculates its own Checksum based on the received data. My checksum and the checksum I received should match if I got a good read. For this example, my total checksum adds to 0xC7, or decimal 199.

I receive the two checksum bytes into byte variables CS1 and CS2.

CS1 = "C" or 0x43
CS2 = "7" or 0x37

My calculated checksum:

CS_Calc = 0xC7 (199)

Now I must combine CS1 and CS2 into a single hex... I've got to take "C' and "7" and make it 0xC7. This is where all my problems were. How to do it? Here it is....

First, determine (for both CS1 & CS2) if it's a letter or number. The respective subtraction must be done accordingly....

If <= 57, subtract 48
if >= 65, subtract 55

This will convert, say CS1 = "C", which is decimal 67, to a hex = 0xC.... Since 67>65, 67-55=12, which makes CS1 = 0xC.

Do this for both CS1 & CS2.

Lastly, do some math to combine CS1 & CS2 into one byte:

CS1*16 + CS2

For our example, that would be: 12*16 + 7 = 199 = 0xC7 ... PERFECT!!!! :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO