title: Raspberry Pi C++ Read NMEA Sentences from Adafruit's Ultimate GPS Module
body: <p>I'm trying to read the GPS NMEA sentences from Adafruit's Ultimate GPS module. I'm using C++ on the raspberry pi to read the serial port connection to the module </p>
  
  <p>Here is my read function:</p>
  
  <pre><code>int Linuxutils::readFromSerialPort(int fd, int bufferSize) {
  
      /*
      Reading data from a port is a little trickier. When you operate the port in raw data mode,
      each read(2) system call will return however many characters are actually available in the
      serial input buffers. If no characters are available, the call will block (wait) until
      characters come in, an interval timer expires, or an error occurs. The read function can be
      made to return immediately by doing the following:
      fcntl(fd, F_SETFL, FNDELAY);
      The NDELAY option causes the read function to return 0 if no characters are available on the port.
      */
  
      // Check the file descriptor
      if ( !checkFileDecriptorIsValid(fd) ) {
          fprintf(stderr, "Could not read from serial port - it is not a valid file descriptor!\n");
          return -1;
      }
  
      // Now, let's wait for an input from the serial port.
      fcntl(fd, F_SETFL, 0); // block until data comes in
  
      // Now read the data
      int absoluteMax = bufferSize*2;
      char *buffer = (char*) malloc(sizeof(char) * bufferSize); // allocate buffer.
      int rcount = 0;
      int length = 0;
  
      // Read in each newline
      FILE* fdF = fdopen(fd, "r");
      int ch = getc(fdF);
      while ( (ch != '\n') ) { // Check for end of file or newline
  
          // Reached end of file
          if ( ch == EOF ) {
              printf("ERROR: EOF!");
              continue;
          }
  
          // Expand by reallocating if necessary
          if( rcount == absoluteMax ) { // time to expand ?
            absoluteMax *= 2; // expand to double the current size of anything similar.
            rcount = 0; // Re-init count
            buffer = (char*)realloc(buffer, absoluteMax); // Re-allocate memory.
          }
  
          // Read from stream
          ch = getc(fdF);
  
          // Stuff in buffer
          buffer[length] = ch;
  
          // Increment counters
          length++;
          rcount++;
  
      }
  
      // Don't care if we return 0 chars read
      if ( rcount == 0 ) {
          return 0;
      }
  
      // Stick
      buffer[rcount] = '\0';
  
      // Print results
      printf("Received ( %d bytes ): %s\n", rcount,buffer);
  
      // Return bytes read
      return rcount;
  
  }
  </code></pre>
  
  <p>So I kind of get the sentences as you can see below, the problem is I get these "repeated" portions of a complete sentence like this:</p>
  
  <pre><code>Received ( 15 bytes ): M,-31.4,M,,*61
  </code></pre>
  
  <p>Here is the complete thing:</p>
  
  <pre><code>Received ( 72 bytes ): GPGGA,182452.000,4456.2019,N,09337.0243,W,1,8,1.19,292.6,M,-31.4,M,,*61
  
  Received ( 56 bytes ): GPGSA,A,3,17,07,28,26,08,11,01,09,,,,,1.49,1.19,0.91*00
  
  Received ( 15 bytes ): M,-31.4,M,,*61
  
  Received ( 72 bytes ): GPGGA,182453.000,4456.2019,N,09337.0242,W,1,8,1.19,292.6,M,-31.4,M,,*61
  
  Received ( 56 bytes ): GPGSA,A,3,17,07,28,26,08,11,01,09,,,,,1.49,1.19,0.91*00
  
  Received ( 15 bytes ): M,-31.4,M,,*61
  
  Received ( 72 bytes ): GPGGA,182456.000,4456.2022,N,09337.0241,W,1,8,1.21,292.6,M,-31.4,M,,*64
  
  Received ( 56 bytes ): GPGSA,A,3,17,07,28,26,08,11,01,09,,,,,2.45,1.21,2.13*0C
  
  Received ( 70 bytes ): GPRMC,182456.000,A,4456.2022,N,09337.0241,W,0.40,183.74,110813,,,A*7F
  
  Received ( 37 bytes ): GPVTG,183.74,T,,M,0.40,N,0.73,K,A*34
  
  Received ( 70 bytes ): GPRMC,182453.000,A,4456.2019,N,09337.0242,W,0.29,183.74,110813,,,A*7E
  
  Received ( 37 bytes ): GPVTG,183.74,T,,M,0.29,N,0.55,K,A*3F
  
  Received ( 32 bytes ): 242,W,0.29,183.74,110813,,,A*7E
  
  Received ( 70 bytes ): GPRMC,182452.000,A,4456.2019,N,09337.0243,W,0.33,183.74,110813,,,A*75
  </code></pre>
  
  <p>Why am I getting the repeated sentences and how can I fix it? I tried flushing the serial port buffers but then things became really ugly! Thanks.</p>