el_supremo
Well-known member
I've set up my Adafruit GPS on Serial3 and have TinyGPS parsing the NMEA sentences when I use Serial3.available() in loop().
I tried changing the code so that it used serialEvent3 to read the chars from the GPS but the serialEvent3 function is never executed.
In the code below the BASIC_TEST shows that the GPS is sending data.
If serialEvent3 executes it should light LED13 and print the chars to Serial but nothing happens.
How do I get that to work?
Pete
I tried changing the code so that it used serialEvent3 to read the chars from the GPS but the serialEvent3 function is never executed.
In the code below the BASIC_TEST shows that the GPS is sending data.
If serialEvent3 executes it should light LED13 and print the chars to Serial but nothing happens.
How do I get that to work?
Pete
Code:
// tiny_gps_event
// Use serial event to read Serial3 and pass the characters
// to the TinyGPS code. When it indicates a decoded NMEA sentence
// print some info.
#include "TinyGPS.h"
TinyGPS gps;
int string_complete = false;
void setup(void)
{
Serial.begin(115200);
while(!Serial);
Serial3.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
int c;
long lat, lon;
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long fix_age, time, date, speed, course;
char ostring[128];
//#define BASIC_TEST
#ifdef BASIC_TEST
// See if there's anything at all coming from the GPS
while(Serial3.available()) {
Serial.write(Serial3.read());
}
return;
#endif
// Just give up if we haven't got a complete string yet
if(!string_complete)return;
// Now decode and print the info
// string_complete = false;
gps.crack_datetime(&year, &month, &day,
&hour, &minute, &second, &hundredths, &fix_age);
sprintf(ostring,"%04d/%02d/%02d %02d:%02d:%02d.%02d",
year,month,day,hour,minute,second,hundredths);
Serial.print(ostring);
// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);
sprintf(ostring," %d %d (%d)",lat,lon,fix_age);
Serial.println(ostring);
}
/*
SerialEvent3 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 serialEvent3() {
char c;
while (Serial3.available()) {
digitalWrite(13,1);
// get the new byte:
c = (char)Serial3.read();
// debug
Serial.write(c);
if(gps.encode(c)) {
string_complete = true;
}
return;
}
// digitalWrite(13,0);
}