Solved

CRC-8 ? Polynomal calculation for RS-232 device!

Posted on 2004-04-19
10
2,216 Views
Last Modified: 2012-05-04
Hi!
I have a big problem because I have a heat device and I wan't to know how it generate CRC checksum.
data send/retrieve by this device are as below:
The last byte is a CRC.
7 0 0 0 105 1 51 ; CRC=51
4 0 0 16             ; CRC=16
7 2 0 0 0 4 196
8 0 0 151 4 11 2 234
7 0 0 0 108 1 57
4 0 11 27
7 0 0 0 100 1 41
4 0 5 21
7 0 0 0 33 1 163
4 0 2 18
7 0 0 0 22 3 207
6 0 2 228 0 185
7 0 0 0 23 3 205
6 0 2 228 0 185
7 0 0 0 23 3 205
6 0 2 241 0 147
7 0 0 0 25 2 208
5 0 4 176 144
7 0 0 0 1 2 224
5 0 2 249 213
7 0 0 0 4 2 234
5 0 3 44 2
7 0 0 0 14 1 253

where first byte signs that sequence has x bytes lenght. Maximum bytes lenght is 8 and minimum is 4.
I have asm code of this exe driver but there's no comment and almost 10 MB so I'll put it on my desk!
The last byte is a CRC, I almost quess how this CRC is calculated because:
for eq.:
7 0 0 0 105 1 51 : so the CRC is 51

five steps:
1. 2*7^0=14
2.2*14^0=28
3. 56^0=56
4.2*56^105=25
5. 2*25^1=51  ; CRC!!!!!

so the alghoritm for eq. in Matlab would be:

function f=countcrc(tab)
%inicjuje poczatkowa sume crc:
crc=bitxor( bitshift(tab(1),1) ,tab(2) );
N=tab(1)-3;
for i=1:N,
crc=bitxor( bitshift(crc,1) ,tab(i+2) );    
end
f=crc;

but the problem is when the  a xor b >255
8 0 0 151 4 11 2 234
 6 0 2 228 0 185
6 0 2 241 0 147
7 0 0 0 158 1 196
8 0 3 118 252 137 0 73

And here I don't know how these values where xor is greather than 255 are generated

Please help me!!!!!!!

Thank's a lot for any advice!!!!!
Sergiusz
0
Comment
Question by:sergiusz_m
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10858267
Hi sergiusz_m,
> And here I don't know how these values where xor is greather than 255
> are generated

CRC-8 only uses the lowest 8 bits, so use in your matlab notation
crc=bitand(crc,255)

In ASM you achieve the same just by storing your result as a byte, or by using the "and" instruction.

Cheers,
Stefan
0
 

Author Comment

by:sergiusz_m
ID: 10858364
I know about it but I'm not asking about it!
look at this example:
8 0 0 151 4 11 2 234

so I use 6 steps:

1. 2*8^0=16
2. 2*16^0=32
3. 2*32^151=215 ; a.... ant there is problem because 2*215=430 >255
and now I'd like to know how to go to 234?? So what next in steep 4,5,6????
4. sth ^4= sth2 ; and now if 2*sth2^4 <255 in step 5 use 2*sth2^11 and etc..
5.
6.

there are others examples that makes xor >255:
 6 0 2 228 0 185
6 0 2 241 0 147
7 0 0 0 158 1 196

for example:
7 0 0 0 158 1 196

five steps:
1.2*7^0=14
2.2*14^0=28
3.2*28^0=56
4.2*56^158=238 ; and 2*238>255 but 196 xor 1=197 so I can quess 197 in step 5
5. 197 ^1=196

but how to go to 197 ?????? xor table or what????

So the control querende would be if ( (2*a^b) &128 !=0) ....... and what here????

The solution could be also in C/C++/Java not only in matlab.

Thanks for all
sergiusz_m
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10858699
sergiusz_m,
OK. The only code I found so far was a CRC16, but it should illustrate nicely:
http://www.eagleairaust.com.au/code/crc16.htm

// Update the CRC for transmitted and received data using
// the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).

    unsigned char ser_data;
    static unsigned int crc;

    crc  = (unsigned char)(crc >> 8) | (crc << 8);
    crc ^= ser_data;
    crc ^= (unsigned char)(crc & 0xff) >> 4;
    crc ^= (crc << 8) << 4;
    crc ^= ((crc & 0xff) << 4) << 1;

...but this should give you a good first start.

Stefan
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:sergiusz_m
ID: 10861329
OK Stefan thanks, this CRC looks very nice but how and where can I use it in my code????

could you type mi sample alghoritm how to use it for this data: 7 0 0 0 158 1 196 ? how to obtain this 196 using this?

Thanks a lot and you'll take a points.

Sergiusz
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10862250
C code for crc:
http://ozma.ssl.berkeley.edu/~dbb/fuse/crc-code.htm

A description of the shifting/xoring (after the basics on bits :) )
http://www.4d.com/docs/CMU/CMU79909.HTM
0
 

Author Comment

by:sergiusz_m
ID: 10862448
twobitadder that's OK but I have my own case described below, so don't write how to write crc in C or how CRC works when I know nothing about used polyval in this chesksum and about behaviour when xor > 255. I've spend  hours sitting on it and I don't know what is "played" where xor >255 - the solution for it is my problem solution!!!!!! I only know (alghoritm in matlab see below) what if xor is lower than 255 so 50% of alghoritm.

Questionis still open!

Regards,
Sergiusz
0
 
LVL 11

Accepted Solution

by:
dimitry earned 125 total points
ID: 10865177
I think I understand this CRC, but check me, because I may be wrong.
If your current (CRC & 0x80) == 0x80 (it means that next shift will be > 255) then you need to XOR it with 0x19.
Take a look at the code:

#include <stdio.h>

#define updcrc(cp, crc) ((unsigned char)(crc << 1) ^ (unsigned char)cp)

unsigned char array[] = { 8, 0, 0, 151, 4, 11, 2, 234 };
// unsigned char array[] = { 7, 0, 0, 0, 105, 1, 51 };
// unsigned char array[] = { 7, 0, 0, 0, 1, 2, 224 };
// unsigned char array[] = { 7, 0, 0, 0, 4, 2, 234 };
// unsigned char array[] = { 7, 0, 0, 0, 158, 1, 196 };
// unsigned char array[] = { 8, 0, 3, 118, 252, 137, 0, 73 };
// unsigned char array[] = { 6, 0, 2, 241, 0, 147 };
// unsigned char array[] = { 7, 2, 0, 0, 0, 4, 196 };
// unsigned char array[] = { 5, 0, 3, 44, 2 };
int main( void )
{
  unsigned short mycrc = 0;
  int i;

  printf("Result = [%x]\n", array[0] );  
  for(i=1,mycrc=array[0];( i < (sizeof(array)-1) );i++) {
    if( mycrc & 0x80 )
      mycrc = (updcrc( array[i], mycrc )) ^ 0x19;
    else
      mycrc = updcrc( array[i], mycrc );
    printf("Result = [%x] -> %x\n", array[i], mycrc );
  }
  if( mycrc != array[i] )
    printf("Error CRC calculation %x != %x\n", mycrc, array[i]);
  printf("Result = %d\n", (unsigned short)mycrc );
  return( 0 );
}
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10876365
sergiusz_m,
Byte overflows are ignored. If you look at Dimitry's code, you'll see that all intermediate values are casted to unsigned char in the updcrc macro. That's very much like doing "& 0xff".

In other words: You just use the lower 8 bits and discard all others.

Stefan
0
 

Author Comment

by:sergiusz_m
ID: 10877506
Thanks for All
Dimitry you are genious, yes it works llike that

THANKS A LOO.......T.

Sergiusz!
0
 
LVL 11

Expert Comment

by:dimitry
ID: 10880236
You are welcome.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
x86 instruction "rdtsc" doesn't work for ARM based cpu (iPhone) 7 3,072
IT Help Desk Summit 4 248
Assembly Language in MIPS 5 655
core dump analysis 26 2,615
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question