Thread: what is wrong with my C code, PLZ help!!!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    Question what is wrong with my C code, PLZ help!!!

    Hi, I need help from anyone who knows, this code is a test for GPS receiver compiled in ICC11 without any errors ,but when I want to run it ,it didn't run why????????

    Code:
    #include < stdio.h > 
    #include < ctype.h > 
    #include < stdlib.h > 
    #include < string.h > 
    #include < math.h > 
    
    #define MAXSIZE 100 
    
    // char read from COM port 
    char  charRead[]="$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A"; 
    
    
    //    Buffer collects chars read from GPS 
       unsigned char  stringRead[MAXSIZE]; 
    
    //    Set an array for each attribute 
     unsigned char  tempString[MAXSIZE]; 
     unsigned char  timeString[12]; 
     unsigned char  latitudeString[11]; 
     unsigned char  latitudeCardinalString[3]; 
     unsigned char  longitudeString[12]; 
     unsigned char  longitudeCardinalString[3]; 
     unsigned char  speedString[11]; 
     unsigned char  dateString[11]; 
    
    //    Declare pointer 
     unsigned char  *pChar; 
    
    //    Coordinated Universal Time(GMT) & Eastern Standard time 
    unsigned  long utcTime, estTime; 
    unsigned  long utcHour, estHour; 
    unsigned  long utcMinutes, estMinutes; 
    unsigned  long utcSeconds, estSeconds; 
    unsigned  long date; 
    unsigned  long day,month,year; 
    
    //    Sets the last comma position 
     unsigned  char lastCommaPosition; 
    
    //    Sets the Latitude & Longitude 
     float          latitude; 
     int         latDegrees; 
     float         latMinutes; 
    
     float     longitude; 
     int        longDegrees; 
     float        longMinutes; 
     float      speed; 
    
    
    //    dummy variable 
       unsigned int   j, k; 
    
    //    Number of chars read per GPS message string 
       unsigned int     i; 
    //    Number of GPS strings read 
       unsigned int     numLinesRead; 
    
    //    initializing the serial ports SCI0 & SCI1 
    
    //    Enter a loop for reading char from serial port(Data Reg.) 
    
    //do { 
    
    void main(void){ 
    
        // checks if GPS messages start with $  char 
    #if charRead=='$' 
         i = 0; 
         numLinesRead++; 
         stringRead[i] = charRead;  /*  include each char read to the array 
    
    /* By this point, a complete GPS string has been read so save it         /* Append the null terminator to the string read */ 
         stringRead[i+1] = '\0'; 
    
         /* Analyze string that we collected */ 
          j = 0; 
         pChar = stringRead; /*     put a pointer for the stringRead   array*/ 
         while(*(pChar+j) != COMMA) {       /* COMMA  0x2C */ 
    
              tempString[j] = *(pChar+j); /*store the string before the comma */ 
              j++; 
         } 
    
         tempString[j] = '\0';   /* append the null to the end 
    
         /* Check if string we collected is the $GPRMC message */ 
         #if tempString[3] == 'R' && tempString[4] == 'M' && tempString[5] == 'C' 
    
    /* Found GPRMC string. It has 11 commas total. Its NMEA sentence structure is: 
    
    $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A 
    
    Where: 
        RMC          Recommended Minimum sentence C 
        123519       Fix taken at 12:35:19 UTC 
        A            Status A=active or V=Void. 
        4807.038,N   Latitude 48 deg 07.038' N 
        01131.000,E  Longitude 11 deg 31.000' E 
        022.4        Speed over the ground in knots 
        084.4        Track angle in degrees True 
        230394       Date - 23rd of March 1994 
        003.1,W      Magnetic Variation 
        *6A          The checksum data, always begins with * 
    */ 
    
       pChar = stringRead;  /* pointer to the String read 
    
    /* Get UTC time */ 
    
             j = 7;  /* start of time field */ 
             k = 0; 
    
             while(*(pChar+j) != COMMA) { //loop until a comma is found 
              timeString[k] = *(pChar+j);   //save in the time string 
                  j++; 
                  k++; 
             } 
    
           lastCommaPosition = j; //  save the position of the last comma 
           timeString[k] = '\0'; // append the null to the end 
    
          sscanf(timeString, "%ld", &utcTime); //save the string to the variable 
    
           utcHour = (utcTime/10000); //extract Hours from long 
           utcMinutes = (utcTime - (utcHour*10000))/100;  //extract minutes from long 
           utcSeconds = utcTime - (utcHour*10000) -    (utcMinutes*100);                     /* extract seconds from long */ 
    
             #if(utcHour >= 0 && utcHour <= 20) 
             estHour = utcHour + 3; 
             #endif 
    
             #elseif estHour = utcHour - 21; 
             estMinutes = utcMinutes; 
             estSeconds = utcSeconds; 
             #endelseif 
    
    
         /* Get lattitude: ddmm.mmmm */ 
    
             pChar = stringRead; 
             j = lastCommaPosition + 1; //start from the next comma 
             k = 0; 
             while(*(pChar+j) != COMMA) { 
              latitudeString[k] = *(pChar+j); //save in the latitude string 
              j++; 
              k++; 
             } 
             lastCommaPosition = j; 
             latitudeString[k] = '\0'; 
    
             sscanf(latitudeString, "%f", &latitude); 
             latDegrees = (int)(latitude/100); 
             latMinutes = (float)(latitude - latDegrees*100); 
    
             /* Get lattitude Cardinal direction */ 
    
             pChar = stringRead; 
             j = lastCommaPosition + 1; 
             k = 0; 
    
             while(*(pChar+j) != COMMA) { 
              latitudeCardinalString[k] = *(pChar+j); 
              j++; 
              k++; 
             } 
             lastCommaPosition = j; 
             latitudeCardinalString[k] = '\0'; 
    
    
    /* Get longitude: dddmm.mmmm */ 
    
             pChar = stringRead; 
             j = lastCommaPosition + 1; 
             k = 0; 
    
             while(*(pChar+j) != COMMA) { 
              longitudeString[k] = *(pChar+j); 
              j++; 
              k++; 
             } 
    
             lastCommaPosition = j; 
             longitudeString[k] = '\0'; 
    
             sscanf(longitudeString, "%f", &longitude); 
             longDegrees = (int)(longitude/100); 
             longMinutes = (float)(longitude - longDegrees*100); 
    
    /* Get Speed in Knots */ 
                   pChar = stringRead; 
    
             j = lastCommaPosition + 1; 
             k = 0; 
    
             while(*(pChar+j) != COMMA) { 
              speedString[k] = *(pChar+j); 
              j++; 
              k++; 
             } 
    
             lastCommaPosition = j; 
             speedString[k] = '\0'; 
    
             sscanf(speedString, "%f", &speed); 
             speed = speed * 1.852; 
    
    /* Get date  */ 
    
             pChar = stringRead; 
    
             j = lastCommaPosition + 2; // skip the Track angle 
             k = 0; 
    
             while(*(pChar+j) != COMMA) { 
              dateString[k] = *(pChar+j); 
              j++; 
              k++; 
             } 
    
             lastCommaPosition = j; 
             dateString[k] = '\0'; 
    
             sscanf(dateString, "%ld", &date); 
    
            day = (date/10000); //extract day from long 
            month = (date - (day*10000))/100;  // extract month from long 
            year = date - (day*10000) - (month*100); // extract year from long 
    
    
    //else not a GPRMC sentence 
    #endif 
    
    // otherwise not a $ character... so loop back until one arrives 
    #endif 
    //  } while(1); 
    printf("The Result ....\n"); 
    printf("%02ld:%02ld:%02ld UTC = %02ld:%02ld:%02ld EST", utcHour, utcMinutes, utcSeconds, estHour, estMinutes, estSeconds); 
    printf("\t%02d DEG\t%2.4f MIN", latDegrees, latMinutes); 
    printf(" %s", latitudeCardinalString); 
    printf("\t%03d DEG\t%2.4f MIN", longDegrees, longMinutes); 
    
    
    
    } /* end of main */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #if tempString[3] == 'R' && tempString[4] == 'M' && tempString[5] == 'C'
    You're confusing the pre-processor with the compiler

    You probably want
    if ( tempString[3] == 'R' && tempString[4] == 'M' && tempString[5] == 'C' )
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void main(void){
    ->
    Code:
    int main(void) {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Um...that do-while loop looks pretty important; you might want to un-comment them. The 'do' part needs to be moved back into main, though.

    [edit]Wow...that code's so jumbled up I'm not even going to try any more. You never get the whole string into stringRead (in fact, I'm suprised it even compiles; isn't assigning a pointer to a char an error?).
    Quote Originally Posted by Title
    what is wrong with my C code, PLZ help!!!
    You sure this is YOUR C code?
    Last edited by pianorain; 03-24-2006 at 06:40 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    In addition to what others have said, here are some other observations.

    Code:
        
        // checks if GPS messages start with $  char 
    #if charRead=='$' 
         i = 0; 
         numLinesRead++; 
         stringRead[i] = charRead;  /*  include each char read to the array
    Wouldn't have strtok() worked?

    Code:
    sscanf(timeString, "%ld", &utcTime);
    You might want to consider having sscanf() check for arbritrary input

    Code:
    sscanf(latitudeString, "%f", &latitude); 
    latDegrees = (int)(latitude/100); 
    latMinutes = (float)(latitude - latDegrees*100);
    Typecasting in this case is bad.

    You might want to considering using functions. I didn't look at the code closer because
    it's pretty ugly.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't need pChar. Every time you use it
    Code:
    (*(pChar+j)
    you could instead use
    Code:
    stringRead[j]
    Replace all your
    Code:
    #if something
    
    #endif
    with
    Code:
    if(something) {
    
    }
    as Salem mentioned.

    This shouldn't have compiled.
    Code:
             #if(utcHour >= 0 && utcHour <= 20) 
             estHour = utcHour + 3; 
             #endif 
    
             #elseif estHour = utcHour - 21; 
             estMinutes = utcMinutes; 
             estSeconds = utcSeconds; 
             #endelseif
    Make it into this:
    Code:
             if(utcHour >= 0 && utcHour <= 20) {
             estHour = utcHour + 3; 
             }
             else if(estHour == utcHour - 21) {
             estMinutes = utcMinutes; 
             estSeconds = utcSeconds; 
             }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. plz help wid this tic tac toe code
    By sudoku in forum C Programming
    Replies: 2
    Last Post: 10-27-2008, 02:15 PM
  3. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM