We are working on communication between AVR and a Crous PTZ.
We found its protocol and now we can turn it on and so.
We have problem with its CRC. This is Crous protocol doc (Got it here), at end of it, CRC algorithm is described (see image below), but seems it's wrong because the counter parameter will never be 8 but it is checked to see if it is 8 or not.
Our coder has wrote the following code for that algorithm , but returned CRC is not the same as the CRC returned by Crous device.
#include <stdint.h>
unsigned int crc_chk(unsigned char dta, unsigned char length)
{
int16_t j;
uint16_t a,b,c;
uint16_t reg_crc=0x0000;
for( c = 0; c < length; c++ )
{
a = reg_crc / 256;
a ^= test_crc[dta];
dta++;
a *= 256;
b = reg_crc&0xff;
reg_crc = a | b;
for( j = 0; j < 8; j++ )
{
if ( reg_crc & 0x8000 )
reg_crc = ( reg_crc * 2 ) ^ 0X8005; /* LSB(b0)=1 */
else
reg_crc = reg_crc * 2;
}
}
return reg_crc; // the value that sent back to the CRC register finally
}
He found true CRC in device respond, for example , In this device respond
01 BF 15 0F 94 95 96 97 9F 9E A2 A0 98 99 72 6B 6A 88 89 64 62 63 65 66 03 2D AC
and according to page 45 of The protocol Doc, CRC of
0F 94 95 96 97 9F 9E A2 A0 98 99 72 6B 6A 88 89 64 62 63 65 66
it should be 2DAC, 03 shows end of data, and 15 is size of data that when we calculate it in decimal, will be 21 that you see we have 21 bytes too.
What is the correct form of this algorithm?
Best Answer
At a glance, it looks like the "counter = 8?" decision block should probably have a "NO" path that feeds back to, probably, "the bit 15 CRC = 1?" decision block input. And the existing output of the "counter = 8?" decision block should be labelled "YES"...
As for the implementation, I'm having some trouble making sense of it. What is
test_crc
and where is it defined? Shouldn'tdta
be a pointer in the function signature? I made some updates to the code in your post to improve readability.I faithfully implemented a function based on the block diagram and my assumption and ran it in IDEOne and get the result I think you expected... here's my code.
It outputs: