I got this source code straight from the manufacturer and I cannot get it to compile. I always get a EOF in file error.
I am using Pelles C to compile on Windows 10 X64. It seems that it should be pretty straight forward but I cant figure it out. It should be a program that I supply a string and it should print a crc
int main(){
str scan = "";
printf("Enter String");
fgets(scan, 10, stdin);
calc_crc_half(scan, 4);
return(0);
}
INT16U cal_crc_half(INT8U far *pin, INT8U len){
INT16U crc;
INT8U da;
INT8U far *ptr;
INT8U bCRCHign;
INT8U bCRCLow;
INT16U crc_ta[16]={
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef
};
ptr=pin;
crc=0;
while(len--!=0)
{
da=((INT8U)(crc>>8))>>4;
crc<<=4;
crc^=crc_ta[da^(*ptr>>4)];
da=((INT8U)(crc>>8))>>4;
crc<<=4;
crc^=crc_ta[da^(*ptr&0x0f)];
ptr++;
}
bCRCLow = crc;
bCRCHign= (INT8U)(crc>>8);
if(bCRCLow==0x28||bCRCLow==0x0d||bCRCLow==0x0a){
bCRCLow++;
}
if(bCRCHign==0x28||bCRCHign==0x0d||bCRCHign==0x0a){
bCRCHign++;
}
crc = ((INT16U)bCRCHign)<<8;
crc += bCRCLow;
printf(crc);
return(crc);
}
I expect the output to be a four character string.
Since you say that you got this code "straight from the manufacturer", presumably you need the CRC's generated using this specific implementation for a particular purpose. This code will work. If you can't get it to compile, you're not doing something correctly in the Pelles C IDE. If that's the case, you might try using a wizard to generate a simple "Hello World" program, and when you get it to compile and run, replace that code with this.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
unsigned short cal_crc_half(unsigned char *pin, size_t len){
unsigned short crc;
unsigned char da;
unsigned char *ptr;
unsigned char bCRCHign;
unsigned char bCRCLow;
unsigned short crc_ta[16]={
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef
};
ptr=pin;
crc=0;
while(len--!=0)
{
da=((unsigned char)(crc>>8))>>4;
crc<<=4;
crc^=crc_ta[da^(*ptr>>4)];
da=((unsigned char)(crc>>8))>>4;
crc<<=4;
crc^=crc_ta[da^(*ptr&0x0f)];
ptr++;
}
bCRCLow = (unsigned char)crc;
bCRCHign= (unsigned char)(crc>>8);
if(bCRCLow==0x28||bCRCLow==0x0d||bCRCLow==0x0a){
bCRCLow++;
}
if(bCRCHign==0x28||bCRCHign==0x0d||bCRCHign==0x0a){
bCRCHign++;
}
crc = ((unsigned short)bCRCHign)<<8;
crc += bCRCLow;
return crc;
}
int main(int argc, char* argv[])
{
char word[256];
unsigned short crc;
puts("Type each word and hit 'Enter'. Enter 'quit' to exit.\n");
while (fgets(word, 256, stdin) != NULL) {
if (strcmp(word, "quit\n") == 0) break;
crc = cal_crc_half(word, strcspn(word, "\n"));
printf("%X\n", crc);
}
return 0;
}