Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I have pin 2 and 3 used as my serial input from a GPS module. I have everything set up working correctly, however, I want to perform more tasks than just reading a GPS module.

  void setup() {
    Serial.begin(9600);
    ///////////////////////////////////
    // 9600 NMEA is the default baud rate
    mySerial.begin(9600);
    Serial.println("NMEA Sentences");
    // uncomment this line to turn on only the "minimum recommended" data for high update rates!
    mySerial.println(PMTK_SET_NMEA_OUTPUT_RMCONLY);

    // uncomment this line to turn on all the available data - for 9600 baud you'll want 1 Hz rate
  //    mySerial.println(PMTK_SET_NMEA_OUTPUT_ALLDATA);

    // Set the update rate
    // 1 Hz update rate
    mySerial.println(PMTK_SET_NMEA_UPDATE_1HZ);
    // 5 Hz update rate- for 9600 baud you'll have to set the output to RMC only (see above)
    //   mySerial.println(PMTK_SET_NMEA_UPDATE_5HZ);
    // 10 Hz update rate - for 9600 baud you'll have to set the output to RMC only (see above)
    //  mySerial.println(PMTK_SET_NMEA_UPDATE_10HZ);
    //////////////////////////////////

  }

Assuming I have all the setup etc correct in my code which I do, if I have this

void loop() {
  if(mySerial.available()) {
     Serial.print((char)mySerial.read());
  }
}

and the NMEA sentence displays just fine. However I want to perform more tasks, and so I need to perform something like this

void loop() {
  // perform lots of other functions here
  while(!otherFunctionsComplete) ... // Very time consuming code
  // just want to read 1 full nmea sentence from gps
  getGPSData();

}
void getGPSData() {
  // wait here until i get the GPRMCA sentence

  while (mySerial.available()) {
      Serial.print((char)mySerial.read());
    }
}

The problem is I only get the buffer size 64 bytes of data using the second method. I want to wait and read the incoming bytes on the serial until i get a '$GPRMCA' and then store all the rest of the bytes. Any help with this?

share|improve this question
add comment

1 Answer

What about the usual way?

Define a buffer (make it a large enough array of chars), then read all bytes into that buffer (this is the mySerial.read() call but you do not output it, you write it into a buffer), if additional bytes arrive, copy the new bytes to the end of the buffer and so on. After each iteration check if your condition matches and perform the required tasks (you can also discard the bytes if you do not need them). A ringbuffer is a very nice approach to a problem like this (but not necessary in this easy case).

share|improve this answer
    
I was thinking about suggesting the same, although after a look the library doesn't seem to be interrupt driven so you would have to call mySerial.available at least every 50uS or so. But that may be an option from within a timer interrupt with a buffer. –  PeterJ Jun 3 '13 at 10:45
    
The library is interrupt driven but its results may not be. Still you could modify it to drop what is not wanted and keep what is, or to have a larger internal buffer, but remember system RAM is quite limited. –  Chris Stratton Jul 3 '13 at 11:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.