1

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.

Tim Yorba
  • 13
  • 3
  • This code doesn't open or read a file. You need to post the code that is generating the error. – Mark Benningfield Mar 30 '19 at 02:32
  • This is the code i got from the manufacturer, i just formatted it so that its easier to read. I get the error when I try to compile it with "pomake.exe makefile" . – Tim Yorba Mar 30 '19 at 02:37
  • 2
    Forgive me, but you realize that this is not an executable program? This is the code for a function. You do have an executable program with a `main` function, right? – Mark Benningfield Mar 30 '19 at 02:43
  • I added a main() function. I'll update the original post. – Tim Yorba Mar 30 '19 at 02:57
  • Is `printf()` in this environment a different function than the standard one? I'd expect it to not compile because of that at the very least... – Shawn Mar 30 '19 at 03:00
  • printf() should be standard in my environment. I installed pelles-C and tried to compile. I made no changes to my environment. – Tim Yorba Mar 30 '19 at 03:07
  • I removed the printf() line and I still get the error while compiling – Tim Yorba Mar 30 '19 at 03:10
  • Also, what's the exact error message? – Shawn Mar 30 '19 at 03:12
  • 1
    And what is this `str` type? How can it work with `fgets()`, which expects a char array with at least 10 elements the way you're using it? – Shawn Mar 30 '19 at 03:17
  • The error says "Fatal Error: Unexpected EOF in inline file". C is not a language I am very familiar with. I just threw the main() function together real quick, I just want it to compile so I can get my CRC's – Tim Yorba Mar 30 '19 at 03:35
  • What headers do you include? Did you write any of them? The term 'inline file' is unusual; I don't think it has a standard meaning, so it may be a term used only by your Pelles compiler. – Jonathan Leffler Mar 30 '19 at 06:38

2 Answers2

1

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;
}
Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
  • Any particular reason for `da=((unsigned char)(crc>>8))>>4; crc<<=4;` vs. `da = (crc >> 8) & 0xF0;`? Oh, I see that's what OP was doing. – chux - Reinstate Monica Mar 30 '19 at 13:24
  • Suggest `cal_crc_half(unsigned char *pin, size_t len)` --> `cal_crc_half(const unsigned char *pin, size_t len)` – chux - Reinstate Monica Mar 30 '19 at 13:25
  • @chux: The only changes I made to OP's CRC implementation were a couple of type-casts to eliminate compiler warnings, and to eliminate the header-defined types in favor of native types. Good catch on stripping the newline from input, though. Can't believe I forgot to do that. – Mark Benningfield Mar 30 '19 at 14:21
  • @MarkBenningfield Thank You for your response. Your code works perfectly. I can now calculate the CRC's for my serial commands for my solar charge controller. Incase anyone else needs it, this is the CRC source code for the MPP Solar LV2424. – Tim Yorba Mar 30 '19 at 16:42
  • @MarkBenningfield Is it possible to convert this to python code? I've been learning to code, but I am used to scripting languages like Javascript and Python, I just don't know enough to know whether or not I can implement this into python. – Tim Yorba Mar 30 '19 at 16:52
  • @TimYorba: I assume it's possible, but not being experienced in Python, I couldn't begin to tell you how to do it. If you are familiar with Python, go ahead and try. What's the worst that could happen? – Mark Benningfield Mar 30 '19 at 17:25
0

Code lacks a proper buffer for reading. Attempting to write to the string literal """ is undefined behavior (UB). @Shawn

int main() {
  // str scan = ""; 
  char scan[11];
  printf("Enter String");
  fgets(scan, 10, stdin);
  calc_crc_half(scan, 4);

The code will calculate the CRC based on input potentially including the '\n'.


I expect OP needs a front end more like

int main(void) {
  char scan[100];  // be more generous in input possibilities.
  printf("Enter String\n");
  if (fgets(scan, sizeof scan, stdin)) {
    scan[strcspn(scan, "\n")] = '\0'; // lop off potential \n
    calc_crc_half(scan, strlen(scan));
  }
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256