Hello everyone !
I have the below code which is a fragment of a bigger code. It receives NMEA from 2 GPS receivers and writes it into GPS1rcvdChars char array and GPS2rcvdChars char array respectively. I was wondering if there is an easy way to clear their contents after i am done parsing it in order to prepare it receive new data rather than overwrite it with the new data.
for example if it was a string i could do : GPS1rcvdChars = "";
Is there some similar way of doing it ?
Thanks !
const byte numChars = 100;
char GPS1rcvdChars[numChars];
char GPS2rcvdChars[numChars];
char GPS1GGA[numChars];
char GPS2GGA[numChars];
unsigned long GPS1tMark1 = 0; // Καταγραφή χρόνου του τελευταίου GSA.
unsigned long GPS1tMark2 = 0; // Χρόνος κατα τον οποίο εκτελείτε ο έλεγχος
unsigned long GPS2tMark1 = 0;
unsigned long GPS2tMark2 = 0;
const unsigned long TimeLimit = 1500; // Χρόνος μετα την πάροδο του οποίου το GPS θεωρείτε ανενεργό.
float GPS1satCount = 0.0;
float GPS2satCount = 0.0;
bool GPS1newData = false;
bool GPS2newData = false;
bool GPS1foundGGA = false;
bool GPS2foundGGA = false;
bool GPS1noGGA = false;
bool GPS2noGGA = false;
bool GPS1timeout = false;
bool GPS2timeout = false;
bool GPS1badFix = false;
bool GPS2badFix = false;
void setup()
{
Serial1.begin(4800);
Serial3.begin(4800);
}
void loop()
{
ReceiveGPS1();
ReceiveGPS2();
GPS1lookNMEA();
GPS2lookNMEA();
}
void ReceiveGPS1()
{
static bool GPS1rcvInProgress = false;
static byte GPS1ndx = 0;
char startMarker = '
;
char endMarker = '\n';
char rc;
while (Serial1.available() > 0 && GPS1newData == false)
{
rc = Serial1.read();
if (GPS1rcvInProgress == true)
{
GPS1tMark1 = millis();
GPS1timeout = false;
if (rc != endMarker)
{
GPS1rcvdChars[GPS1ndx] = rc;
GPS1ndx++;
if (GPS1ndx >= numChars)
{
GPS1ndx = numChars - 1;
}
}
else
{
GPS1rcvdChars[GPS1ndx] = '\0'; // terminate the string
GPS1rcvInProgress = false;
GPS1ndx = 0;
GPS1newData = true;
}
}
else if (rc == startMarker)
{
GPS1rcvInProgress = true;
}
}
}
void ReceiveGPS2()
{
static bool GPS2rcvInProgress = false;
static byte GPS2ndx = 0;
char startMarker = '
;
char endMarker = '\n';
char rc;
while (Serial3.available() > 0 && GPS2newData == false)
{
rc = Serial3.read();
if (GPS2rcvInProgress == true)
{
GPS2tMark1 = millis(); //Μηδενισμός χρονομέτρου Timeout για το GPS.
GPS2timeout = false;
if (rc != endMarker)
{
GPS2rcvdChars[GPS2ndx] = rc;
GPS2ndx++;
if (GPS2ndx >= numChars)
{
GPS2ndx = numChars - 1;
}
}
else
{
GPS2rcvdChars[GPS2ndx] = '\0'; // terminate the string
GPS2rcvInProgress = false;
GPS2ndx = 0;
GPS2newData = true;
}
}
else if (rc == startMarker)
{
GPS2rcvInProgress = true;
}
}
}
void GPS1lookNMEA()
{
GPS1tMark2 = millis();
if ((GPS1tMark2 - GPS1tMark1) > TimeLimit)
{
GPS1timeout = true;
}
if (GPS1newData == true)
{
if (GPS1rcvdChars[2]=='G')
{
delay (2);
if (GPS1rcvdChars[3]=='G')
{
delay (2);
if (GPS1rcvdChars[4]=='A')
delay (2);
strcpy (GPS1GGA, GPS1rcvdChars);
GPS1noGGAcount = 0;
GPS1foundGGA = true;
GPS1noGGA = false;
GPS1parseData();
}
}
if (GPS1foundGGA == false)
{
if (GPS1noGGAcount >= 0 and GPS1noGGAcount < 15) // Αν περάσουν ανω των 15 προτάσεων χωρίς GSA τότε η συσκευή
{ // δεν εκπέμπει την πρόταση GSA.
GPS1noGGAcount++ ;
}
if (GPS1noGGAcount == 15)
{
GPS1noGGA = true;
}
}
}
GPS1newData = false;
GPS1foundGGA = false;
}
void GPS2lookNMEA()
{
GPS2tMark2 = millis();
if ((GPS2tMark2 - GPS2tMark1) > TimeLimit) //Έλεγχος αν το GPS εκπέμπει πληροφορίες εντος προβλεπόμενου χρονικού πλαισίου.
{
GPS2timeout = true;
}
if (GPS2newData == true)
{
if (GPS2rcvdChars[2]=='G')
{
delay (2);
if (GPS2rcvdChars[3]=='G')
{
delay (2);
if (GPS2rcvdChars[4]=='A')
delay (2);
strcpy (GPS2GGA, GPS2rcvdChars);
GPS2noGGAcount = 0;
GPS2foundGGA = true;
GPS2noGGA = false;
GPS2parseData();
}
}
if (GPS2foundGGA == false)
{
if (GPS2noGGAcount >= 0 and GPS2noGGAcount < 15) // Αν περάσουν ανω των 15 προτάσεων χωρίς GSA τότε η συσκευή
{ // δεν εκπέμπει την πρόταση GSA.
GPS2noGGAcount++ ;
}
if (GPS2noGGAcount == 15)
{
GPS2noGGA = true;
}
}
}
GPS2newData = false;
GPS2foundGGA = false;
}
void GPS1parseData()
{
char *strtokIndx;
strtokIndx = strtok(GPS1GGA, ","); //Πεδίο 0 GPGGA.
strtokIndx = strtok(NULL, ","); //Πεδίο 1 ώρα.
strtokIndx = strtok(NULL, ","); //Πεδίο 2
strtokIndx = strtok(NULL, ","); //Πεδίο 3 Ν.
strtokIndx = strtok(NULL, ","); //Περίο 4
strtokIndx = strtok(NULL, ","); //Πεδίο 5 E
strtokIndx = strtok(NULL, ","); //Πεδίο 6 A,V
strtokIndx = strtok(NULL, ",");
GPS1satCount = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
GPS1dopVal = atof(strtokIndx);
if (GPS1satCount < 4.0)
{
GPS1badFix = true;
}
else
{
GPS1badFix = false;
}
}
void GPS2parseData()
{
char *strtokIndx;
strtokIndx = strtok(GPS2GGA, ","); //Πεδίο 0 GPGGA.
strtokIndx = strtok(NULL, ","); //Πεδίο 1 ώρα.
strtokIndx = strtok(NULL, ","); //Πεδίο 2
strtokIndx = strtok(NULL, ","); //Πεδίο 3 Ν.
strtokIndx = strtok(NULL, ","); //Περίο 4
strtokIndx = strtok(NULL, ","); //Πεδίο 5 E
strtokIndx = strtok(NULL, ","); //Πεδίο 6 A,V
strtokIndx = strtok(NULL, ",");
GPS2satCount = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
GPS2dopVal = atof(strtokIndx);
if (GPS2satCount < 4.0)
{
GPS2badFix = true;
}
else
{
GPS2badFix = false;
}
}