I am using the Serial Event example from Arduino Reference to receive the output from a Neo6m GPS module to a Nano. With the unmodified example sketch the sentences are received intact and complete.
When I add a few statements in the loop data is lost. I'd like to understand why that happens and how to fix it.
The physical setup is:
GPS VCC to Nano 5v
GPS GND to Nano GND
GPS TX to Nano RX (GPS RX unconnected)
Nano connected to laptop via USB cable
9600 baud rate at both GPS and monitor (they have to be the same as they are both connected to the same hardware serial)
Using hardware serial, not software serial. So the serial is serving two separate devices -- the GPS module and the IDE monitor. Am I asking for problems doing that? The sketch does not send anything to the GPS and does not receive anything from the monitor.
Below is the sketch with my addition of some steps to measure the duration between successive received complete sentences and the length of each sentence. In the first case those statements are commented out and so are not effective. The data appears on the monitor perfectly (first image from monitor).
When I un-comment those statements, some data is lost (second image from monitor). There should be three GSV sentences, but the second and third are stitched together. This is regular -- same output on the monitor every GPS cycle.
Is it possible the data is lost between the Nano and the monitor rather than between the GPS and the Nano?
I have read robin2's excellent tutorial on serial comms.
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and clears it.
A good test for this is to try it with a GPS receiver that sends out
NMEA 0183 sentences.
NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
other ATmega32U4 based boards.
created 9 May 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/SerialEvent
*/
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
int mark1, mark2;
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
// mark2 = millis();
Serial.print(inputString);
// Serial.print(mark2-mark1);
// Serial.print("ms; ");
// Serial.print(inputString.length());
// Serial.println("bytes");
// clear the string:
inputString = "";
stringComplete = false;
// mark1 = mark2;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Monitor image when my statements are commented out (no lost data):
Monitor when my statements are not comments (lost data):