Hello, im using the example by Serial Input Basics - updated - Introductory Tutorials - Arduino Forum to read a NMEA strings WIMDA and GPRMC from GPS unit, it send me the 2 strings every 1 second.
////////////////////////////////////////////////////////////
#include <SoftwareSerial.h>
SoftwareSerial airmar(10,11);
// Example 2 - Receive with an end-marker
const byte numChars = 72;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(19200);
airmar.begin(4800);
Serial.println("");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
airmar.listen();
while (airmar.available() > 0 && newData == false) {
rc = airmar.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("Dato_leido");
Serial.println(receivedChars);
newData = false;
}
}
And in the Serial terminal
Dato_leido$WIMDA,29.9818,I,1.0153,B,23.7,C,,,,,,,323.1,T,311.6,M,0.2,N,0.1,M2C
Dato_leido$GPRMC,002411.20,A,3151.7113,N,11640.1045,W,0.0,268.4,270517,11.5,E,D1
Dato_leido$WIMDA,29.9877,I,1.0155,B,23.7,C,,,,,,,323.2,T,311.7,M,0.3,N,0.2,M23
Dato_leido$GPRMC,002412.20,A,3151.7113,N,11640.1045,W,0.0,291.2,270517,11.5,E,D1
Dato_leido$WIMDA,29.9788,I,1.0152,B,23.7,C,,,,,,,323.1,T,311.6,M,0.2,N,0.1,M2B
Dato_leido$GPRMC,002413.20,A,3151.7113,N,11640.1045,W,0.0,259.8,270517,11.5,E,D1
Dato_leido$WIMDA,29.9847,I,1.0154,B,23.7,C,,,,,,,6.0,T,354.5,M,0.3,N,0.2,M*24
So it's ok reading the data from GPS unit
//////////////////////////////////
but if i put a delay (100, 1000, 2000, etc....)
void loop() {
recvWithEndMarker();
showNewData();
delay(200);
}
at the begin or end of loop, the serial terminal send:
Dato_leido$GPRMC,002624.20,A,3151.7118,N,11640.1043,W,0.0,170.7,270517,11$GPRMC,0
Dato_leido$WIMDA,29.981$GPRMC,002631.20,A,3151.7118,N,11640.1044,W,0.0,16$GPRMC,0
Dato_leido$GPRMC,002645.20,A,3151.7118,N,11640.1044,W,0.0,33.1,270517,1$G$GPRMC,0
Dato_leido$GPRM$GPRMC,002654.20,A,3151.7117,N,11640.1045,W,0.0,332.8,2705$GPRMC,0
Dato_leido$WIMDA,29.9818,I,1.0$GPRMC,002701.20,A,3151.7117,N,11640.1045,W$GPRMC,0
Dato_leido$GPRMC,002715.20,A,3151.7116,N,11640.1045,W,0.1,213$GPRMC,00271$GPRMC,0
Dato_leido$WIMDA,29.9759,I,1.0151,B,23.6,C,,,,,,,54.3,T,42.8,M,0.2,N,0.1,$GPRMC,0
Dato_leido$WIMDA,29.9788,I,1.0152$GPRMC,002731.20,A,3151.7116,N,11640.104$GPRMC,0
So the NMEA strings are mixed one in another.
Whats is my error?
Isn't suppose that while serial.available are for to read any NMEA string until \n char and not mix with the other?
how i can resolve that?
Thank you so much.