What is the Problem with my c code ?

Discussion in 'C' started by Eng_Girl, Mar 22, 2006.

  1. Eng_Girl

    Eng_Girl New Member

    Joined:
    Mar 22, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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???????? :confused: :confused:


    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. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    What do you mean by did not run? I got the output of the printf statements. I guess you have the conditions in the #if instead of if and so probably it just goes to the end of the main where printf's are there.
     
  3. Eng_Girl

    Eng_Girl New Member

    Joined:
    Mar 22, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Coderzone what do u mean???

    which program you used?????
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    What I meant is You have the condition like
    #if charRead=='$'
    But I think it should be
    if(charRead=='$')
    Because I dont think MACRO if will be true at any point.

    Of course the one you have given here.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice