Thread: Mototrola S type checksum

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Mototrola S type checksum

    Hi guys,


    i need to do calculation for Mototrola S type checksum, the the problem is i don't know how to modify my code in order to calculate sum of everyline in Ts820.mot and display it on exe prompt as 4 digit hex number. To calculate sum i take each 2 Ascii character and change to hex using sscanf. Below is my code, plz help me....


    Code:
    
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    char input_string[100] ;
    char tmp_string[2];
    unsigned int output_string[50];
     int number_of_hex_values = 0;
    
    int main(void)
    {
       FILE *file = fopen("ts820.mot", "r");
       
       if(file==NULL) 
        {
        printf("Error: can't access file.c.\n");
        return 1;
       }
    
      else {
        printf("File opened successfully.\n");
    
         
        while(!feof(file)) 
    			{ 
       unsigned int hex_num;
    
          fscanf(file, "%s", input_string); 
    
    
       for(int count = 0; count < strlen(input_string); count += 2)
          {
            tmp_string[0] = input_string[count];
            tmp_string[1] = input_string[count+1];
    
               sscanf(tmp_string, "%02X", &hex_num);
    
           
            output_string[number_of_hex_values] = hex_num;
        
            number_of_hex_values++;
            
    
    
          }
    
       fclose(file);
    
    		       }
    
              }
    
    	printf("%d\n", output_string[number_of_hex_values]);
    
     return 0;
    }

    Program flow:
    1. File input of Ts820.mot
    2. Check the record type (valid data, invalid data or end of file)
    3. Obtain the length of data
    4. Sum of hex data within the length
    5. Display the checksum (4 digits only) in the screen

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Help with what?

    Don't just dump some code and some requirements on us, and hope we'll figure out the rest for you.

    If it's a compiler error, tell us what the error is.
    It it's not producing the right output, share your analysis (however brief) with us.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    validate each type of data from file

    I'm sorry for that, i wrote the flow of the program just to explain my program clearly... The main point of my previous thread was i don't really understand algorithm to do checksum..i read on other forum there were quite a few ways to do so. But still i found it hard to understand and don't know which one to choose.

    Before i have to do checksum i've got to validate type of data and for each line in srecord first. Now my problem is regarding to validate each line. The problem with my code is it did not displayed all the lines in srecord which is almost 600 lines. instead of it displayed only line 1 - 9 and no error produce. I did not set any range for line displayed, so i wonder where the hack is wrong untill it displayed such way... If you don't mind could you help me to understand which part is wrong in my code.

    ex:line 1, st[0] is start code must equal to 'S'


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    int baris = 1;
    int flag1, flag2 = 0 ;
    
    int main(void)
    {
    
    
    
    FILE *f;
      char s[20000];
            f=fopen("ts820.mot","r");
      if (!f)
      return 1;
      while (fgets(s,20000,f)!=NULL)
       {
       
       validateStrCd(s, baris);
       validateRecty(s, baris);
       
       printf("%s",s);
    
        if ((flag1==1) || (flag2==1))
    {
       
        break; /*Or continue if you want to return to the input again*/
    }
     
        baris++;
        fclose(f);
       }
      return 0;
    }
    
    
    int validateStrCd(char *st, int line)
    {
    
       if(st[0]=='S')
    	{
         printf("Valid start code at line %d\n", line);
         printf("%c\n", st[0]);
            
    	}
       else 
             {
         printf("Invalid start code at line %d\n", line);
         printf("%c\n", st[0]);
         flag1 = 1;
        
    	}
     
        	
    return 0;
    	
    	
    }
    
    
    int validateRecty(char *str, int line)
    {
    
      if (str[1] < '0' || str[1] > '9')
        {    
         printf("Invalid start code at line %d\n", line);
         printf("%c\n", str[1]);
            flag2 = 1;
          }
    
    	
    return 0;
    	
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM