What is causing delay in my program? Reading serial data and parsing. (ESP32: Ar

In my test program, I have an unexpected delay (approx 100 ms). I am trying to read data from GPS (as NMEA sentence) and parse it. Currently, the program reads the data and if it is NMEA sentence, it try to parse it. The problem is, that I get a delay of approx 100ms between some of the messages.

The baud rate of the GPS is 9600, could this cause the problems?

My approach is to parse GGA sentence, and resend to different serial port (USB) a modified custom sentence with added sensor data as soon as possible (should looks like originally from GPS - so synchronized)

My output looks like this:

The first sentence is printed directly after the byte has been received. The second sentence is printed as a String after the full sentence has been received.

Here is my code:

#define RXD2 13
#define TXD2 12
#define SERIAL_SIZE_RX  1024    // used in Serial.setRxBufferSize()

bool bNMEAstarted = false;
String NMEAsent;

unsigned long currentTime, lastTime;

void setup() {
  Serial.begin(115200);
  delay(15);

  //GPS serial
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  delay(150);
  //Serial2.setRxBufferSize(SERIAL_SIZE_RX);  //increase buffer size to 1024 bytes
  currentTime = millis();
  lastTime = currentTime;
}

void loop() {
  receiveFromGPS();
}


void receiveFromGPS(){
  byte incomByte1 = 0;
  if (Serial2.available()>0){
    incomByte1 = Serial2.read();
    Serial.write(incomByte1);
    
    // Parsing NMEA sentence
    if (incomByte1 == '

) { //means it is NMEA sentece
bNMEAstarted = true;
NMEAsent = ""; //empty the string
}

if (bNMEAstarted) {
  NMEAsent += (char)incomByte1;       //add every character to NMEA sentence
  if (incomByte1 == 10) {             //ASCII(10) <LF> (Linefeed) ends the message
    bNMEAstarted = false;
    //do parsing after the end on sentence
    parseNMEA(NMEAsent);
  }
}

}
}

void parseNMEA(String sNMEA) {
//if (sNMEA.substring(3,6) == "GGA") { // GGA Message found

//only print every NMEA sentence and time between each measurements
Serial.print(sNMEA);
currentTime = millis();
Serial.println(currentTime-lastTime);

//}
lastTime = currentTime;
}

I'd guess it's the baud rate. An NMEA sentence can be up to ~80 characters. At 9600 baud, that'll take at least 80 mS, so that accounts for most of it. Your other sentences with no data in will be a bit faster of course.

My tutorial on Arduino Serial I/O for the Real World goes into this type of problem in detail
and includes a detailed example of reading and parsing GPS data and checking the time involved and steps to take to avoid the problems of processing/serial I/O delays.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.