Hi,
I am trying to collect the GPS data send from my GPS sensor. With a little sketch found on the internet. (tweaked it a little bit).
When I print the char C I get the following output (code below):
$GPRMC,070450.00,A,5222.02163,N,00636.04015,E,0.066,210414,A76
$GPVTG,T,M,0.066,N,0.122,K,A22
$GPGGA,070450.00,5222.02163,N,00636.04015,E,1,07,1.28,15.8,M,46.0,M,6A
$GPGSA,A,3,07,04,20,23,02,13,10,2.36,1.28,1.980F
$GPGSV,4,1,13,02,33,305,26,04,57,243,32,06,11,041,31,07,26,171,307A
$GPGSV,4,2,13,08,03,182,22,10,51,288,31,13,85,166,30,16,08,080,1477
$GPGSV,4,3,13,20,22,114,21,23,53,067,37,29,00,356,30,04,185,25*7C
$GPGSV,4,4,13,31,00,026,4D
$GPGLL,5222.02163,N,00636.04015,E,070450.00,A,A6D
This is very nice. Now i want to collect rule for rule in a char array. When I print the buffer I get the following output.
And I am struggling for days with it. Cant find what is wrong. Guess something simple. The output misses chars, has odd breaks, etc.
$GPRMC,070916.00,A,5222.02204,N,00636.41,E,0.104,210414,A78
$GPVTG,T,M,0.916.00,A,5222.02204,N,00636.41,E,0.104,210414,A78
VTG,TM,0.04,N,0.193,K,A2D
$GPGGA,070916.00,5222.022004,210414,A78
VTG,TM,0.04,N,0.193,K,A2D
PGG,0916.,5220220,07,04,20,23,02,13,08,10,2.40,1GSV,4,1,13,02,34,303,29,04,56,239,31,0507,04,20,23,02,13,08,10,2.40,1GSV,4,1,13,0,3433,2,4,5,39,105,00,291,06,12,039,0974
$GPGSV,4,2,1GSV,4,1,13,0,3433,2,4,5,39,105,00,291,06,12,039,0974
GSV42,13,07,28,171,31,08,05,182,25,10,53,288,16,13,86,13655,974
GSV42,13,07,28,171,31,08,05,182,25,10,53,288,16,13,86,165,7B
$GPGSV,4,4,13,30,05,185,2241
The code:
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial gpsSerial = SoftwareSerial(8, 7);
#define GPSRATE 9600
#define GPS_BUFFER_SIZE 128
// Buffer die we gebruiken om data te verzamelen.
// (Buffer we use to collect data)
char buffer[GPS_BUFFER_SIZE];
// Alle verzamelde data wordt in deze char array gezet totdat we iets nieuws gevonden hebben.
// (After finding a full data string, we copy it to this one)
char gps_string[GPS_BUFFER_SIZE];
// Bijhouden waar we zijn met het verzamelen van tekens.
// (Keep track of the chars in array)
int buffer_index = 0;
// De laatst gelezen character. (Last red char)
char c;
void setup()
{
// Output naar de PC. (For output to PC
Serial.begin(115200);
// Start de verbinding naar de GPS sensor.
// (Start softSerial at 9600)
gpsSerial.begin(GPSRATE);
// Laat even weten dat we gestart zijn.
// (Let us know that we are started)
Serial.print("GPS ... ");
// Alles klaarzetten. (Reset)
reset();
// Eerste beetje jibrisch negeren.
// (Ignore the first jibrish that comes in)
while (gpsSerial.available()) {
if (gpsSerial.read() == '\r')
Serial.println("gestart");
break;
}
}
void reset() {
// Buffer leegmaken voor een volgende ronde.
// (Empty the write buffer)
memset(buffer, 0, GPS_BUFFER_SIZE);
// Start index aan het eind op 0 zetten! of bij een fout.
// (Reset the index)
buffer_index = 0;
}
void loop() {
while (1) {
c = gpsSerial.read();
// Zooi filteren.
// (Ignore jibrish)
if (c == -1) {
continue;
}
//Serial.print(c);
// Laatste 2 chars in de string zijn \r\n met de \n niets doen, bij de \r is de reeks al afgerond.
// (Last 2 chars of the string are \r\n. Ignore the \n. The string is already compleated by the \r)
if (c == '\n') {
continue;
}
// Buffer overflow gevonden. Stop er mee om schrijven buiten het geheugen gebied te voorkomen.
// (Found a buffer overflow. Reset and break to prefent problems
if (buffer_index == GPS_BUFFER_SIZE - 1) {
// Resetten want we hebben een overflow
reset();
break;
}
// Laatste character in de reeks gevonden. Buffer eindigen met een \0.
// (Found the last usefull char \r. Finish the string with \0)
if (c == '\r') {
buffer[buffer_index] = '\0';
Serial.println(buffer);
// Buffer kopiëren naar een nieuwe array.
// (Copy the buffer to a new array so that this is not overwritten in the next round until its clear)
strcpy(gps_string, buffer);
// Klaarmaken voor de volgende reeks. (Resetting for the next round)
reset();
break;
}
// Er gaat wat mis. Het eerste character is altijd een $.
// (Somthing is wrong. The procol starts always with a $)
if (buffer_index == 0 && c != '
) {
//Serial.print(“ERROR Eerste char is een:”);
//Serial.println(c);
}
buffer[buffer_index++] = c;
}
}