This my Mod bus crc_16 embedded code. I have run this code in code block many Times. There is no error but I am not getting actual crc value. I should get crc 05C8 and I am getting 8512 right now.
I think I giving wrong input while calling CRC method. I am passing string and its length. So please help me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define POLY 0x8005
unsigned int ModRTU_CRC(unsigned char * mod_data,unsigned int length)
{
unsigned int CRC16 = 0xFFFF;
unsigned int pos=0,i=0;
for(pos=0;pos<length;pos++)
{
CRC16 ^= (unsigned int) mod_data[pos];
for(i=0;i<8;i++)
{
if((CRC16 & 0x0001)!=0)
{
CRC16 >>=1;
CRC16^=0xA001;
}
else
{
CRC16 >>=1;
}
}
}
return CRC16;
}
int main()
{
//char *frame = "010600081388";
// char *frame = "010300080001";
char frame[7];
frame[0]=0x01;
frame[1]=0x03;
frame[2]=0x00;
frame[3]=0x08;
frame[4]=0x00;
frame[5]=0x01;
frame[6]='\0';
printf("%x\n",frame);
int len = strlen(frame);
unsigned int crcv = ModRTU_CRC(frame,len);
printf("%x\n",crcv);
return 0;
}