|
|
View previous topic :: View next topic |
Author |
Message |
jemly
Joined: 13 May 2007 Posts: 34
|
Cannot Work Out Checksum Calculation |
Posted: Mon Oct 15, 2007 8:22 am |
|
|
We are receiving data via rs232 and we know the data is for the most part good. A 4byte checksum is sent after the data we use and I am trying to write a routine to check the validity of the data sent based on the checksum - and I'm having trouble!
We are sent 36 bytes of data then a 4 byte checksum.
I store the checksum in a byte array as follows:
According to the senders of the data they exclusively OR the 1st, 4th, 8th, 12th etc..... bytes to produce the first byte of the checksum. Then they exclusively OR the 2nd, 5th, 9th, 13th etc bytes of the data to produce the 2nd byte of the checksum. And so on until all 4 bytes of the checksum are complete.
I have tried to replicate this and have simplified it for the moment to only look at whether the 2nd byte of the checksum matches the 2nd byte of my XOR - but I seem to see no checksum matches and I don't know what I'm doing wrong! Can you help? Here's my code:
Code: |
boolean ValidateCheckSum()
{
byte csCheck[4];
int h;
for (h=0;h<4;h++) {
csCheck[h] = (data[h] ^ data[h+4] ^ data[h+8] ^ data[h+12] ^ data[h+16] ^ data[h+20] ^ data[h+24] ^ data[h+28] ^ data[h+32]);
}
if(checkSum[2] == csCheck[2]) //success
{
output_bit(ERROR_LED,0);
return true;
}
else
{
output_bit(ERROR_LED,1);
return false;
}
}
|
|
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Mon Oct 15, 2007 8:49 am |
|
|
Code: | data[h] ^ data[h+4] ^ data[h+8] |
Doesn't that take bytes 1, 5, 9, etc.,?
Shouldn't it be:
Code: | data[h] ^ data[h+3] ^ data[h+7] |
But now that I think about it, their statement 'exclusively OR the 1st, 4th, 8th, 12th ' doesn't quite make sense either.
Just thinking aloud..... |
|
|
jemly
Joined: 13 May 2007 Posts: 34
|
|
Posted: Mon Oct 15, 2007 8:55 am |
|
|
Ok maybe some confusion.... the statement "exclusively OR the 1st, 4th, 8th, 12th '" should have read "exclusively OR the 0th, 4th, 8th, 12th '".
And I've looked over my code again and I'm sure that's what I'm doing.
Code: |
for (h=0;h<4;h++) {
csCheck[h] = (data[h] ^ data[h+4] ^ data[h+8] ^ data[h+12] ^ data[h+16] ^ data[h+20] ^ data[h+24] ^ data[h+28] ^ data[h+32]);
}
|
...would xor bytes:
0,4,8,12,16,20,24,28,32
....then
1,5,9,13,17,21,25,29,33
....then
2,6,10,14,18,22,26,30,34
....then
3,7,11,15,19,23,27,31,35
So I don't get why it's not working! Am I being totally blind and missing what you menat?![/code] |
|
|
Ttelmah Guest
|
|
Posted: Mon Oct 15, 2007 9:28 am |
|
|
I'd suggest perhaps posting one or two sequences of the numbers. What you do, looks 'right', assuming the data array, is declared as int8, for what you are describing.
However it may be that they are doing something different. If (for instance), the bytes selected in the first line, second, third, and fourth, advance at different rates, then the 'checksum', potentially becomes useable as a crude ECC code.
Doing the four separate bytes, really gains nothing, unless something like this is done, and I'd suspect this is the real reason for this, and that probably the advances do differ between the rows, and this is why your code doesn't agree.
Best Wishes |
|
|
jemly
Joined: 13 May 2007 Posts: 34
|
|
Posted: Mon Oct 15, 2007 10:24 am |
|
|
TTelmah - thanks and here's some clarification. The data array is declared:
BYTE data[36];
and the csCheck array is declared:
BYTE csCheck(4);
We've checked with the people who wrote the software that is sending out the data and they are sure that this should work.........! |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Oct 15, 2007 12:17 pm |
|
|
Can you give us a sample data packet with its checksum? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Oct 16, 2007 8:04 am |
|
|
And if you talked to the software people, perhaps they can give you a "known good" packet sample. |
|
|
|
|
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 vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|