#include <string.h>
#include <ctype.h>
intledPin = 13; // LED test pin
intrxPin = 0; // RX PIN
inttxPin = 1; // TX TX
intbyteGPS=-1;
charlinea[300] ="";
charcomandoGPR[7] ="$GPRMC";
intcont=0;
intbien=0;
intconta=0;
intindices[13];
voidsetup() {
pinMode(ledPin, OUTPUT); // Initialize LED pin
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(38400);
for(inti=0;i<300;i++){ // Initialize a buffer for received data
linea=' ';
}
}
voidloop()
{
if(Serial.available()){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
byteGPS=Serial.read();
} // Read a byte of the serial port
if(byteGPS == -1) { // See if the port is empty yet
delay(100);
}else{
// note: there is a potential buffer overflow here!
linea[conta]=byteGPS; // If there is serial port data, it is put in the buffer
conta++;
Serial.write(byteGPS);
if(byteGPS==13){ // If the received byte is = to 13, end of transmission
// note: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
digitalWrite(ledPin, LOW);
cont=0;
bien=0;
// The following for loop starts at 1, because this code is clowny and the first byte is the <LF> (0x10) from the previous transmission.
for(inti=1;i<7;i++){ // Verifies if the received command starts with $GPR
if(linea==comandoGPR[i-1]){
bien++;
}
}
if(bien==6){ // If yes, continue and process the data
for(inti=0;i<300;i++){
if(linea==','){ // check for the position of the "," separator
// note: again, there is a potential buffer overflow here!
indices[cont]=i;
cont++;
}
if(linea=='*'){ // ... and the "*"
indices[12]=i;
cont++;
}
}
Serial.println(""); // ... and write to the serial port
Serial.println("");
Serial.println("---------------");
for(inti=0;i<12;i++){
switch(i){
case0 :Serial.print("Time in UTC (HhMmSs): ");break;
case1 :Serial.print("Status (A=OK,V=KO): ");break;
case2 :Serial.print("Latitude: ");break;
case3 :Serial.print("Direction (N/S): ");break;
case4 :Serial.print("Longitude: ");break;
case5 :Serial.print("Direction (E/W): ");break;
case6 :Serial.print("Velocity in knots: ");break;
case7 :Serial.print("Heading in degrees: ");break;
case8 :Serial.print("Date UTC (DdMmAa): ");break;
case9 :Serial.print("Magnetic degrees: ");break;
case10 :Serial.print("(E/W): ");break;
case11 :Serial.print("Mode: ");break;
case12 :Serial.print("Checksum: ");break;
}
for(intj=indices;j<(indices[i+1]-1);j++){
Serial.print(linea[j+1]);
}
Serial.println("");
}
Serial.println("---------------");
}
conta=0; // Reset the buffer
for(inti=0;i<300;i++){ //
linea=' ';
}
}
}
}