c - How to calculate CRC using this code? -


i'm trying going radio communication between device , server. data device looks this:

fa-00-01-09-16-aa-00-14-10-01-01-00-01-16-ff-ff-ff-ff-ff-ff-cb-14 

where last 2 bytes crc. need send reply device , calculate crc well. device's data sheet doesn't , provides c function calculate it, have hard time understanding should feed function

unsigned int crc_check(unsigned char *uccrc_buf, unsigned char ucbuflength) {     unsigned int uix, uiy, uicrc;     unsigned char ucstart = 0;     uicrc = 0xffff; //set 1      if (ucbuflength <= 0 || ucstart > ucbuflength)     {         uicrc = 0;     }     else     {         ucbuflength += ucstart;          (uix = (unsigned int)ucstart; uix < ucbuflength; uix++)         {             uicrc = (unsigned int)(uicrc ^ uccrc_buf[uix]);              (uiy = 0; uiy <= 7; uiy++)             {                 if((uicrc & 1) != 0)                     uicrc = (unsigned int)((uicrc >> 1)^0xa001);                 else                     uicrc = (unsigned int)(uicrc >> 1);             }         }     }      return uicrc; } 

data sheet says "crc check conducted on data packet excluding start code & check code" means first byte (0xfa) , last 2 bytes (the crc).

now i'm trying understand expects unsigned char *uccrc_buf , unsigned char ucbuflength. main i'm trying do:

unsigned int crc = crc_check("00010916aa0014100101000116ffffffffffff",38); printf("%d\n", crc); 

which same string in beginning (without first , last 2 bytes) , i'm expecting cb14 (hex). gives different number (54016 (dec) d300 (hex)).

any idea i'm doing wrong?

probably have pass bytes themselves, not hex-representation.

change

unsigned int crc=crc_check("00010916aa0014100101000116ffffffffffff",38); 

to

unsigned int crc=crc_check("\x00\x01\x09\x16\xaa\x00\x14\x10\x01\x01\x00\x01\x16\xff\xff\xff\xff\xff\xff", 19); 

note there 19 bytes. (thanks @alk)


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -