SlashDevin / NeoGPS

NMEA and ublox GPS parser for Arduino, configurable to use as few as 10 bytes of RAM
GNU General Public License v3.0
707 stars 195 forks source link

wouldn't if(gps.available(gpsPort)) be better then while(gps.available(gpsPort))? #111

Open marcuscbehrens opened 5 years ago

marcuscbehrens commented 5 years ago

I assume many people use this basic loop:

void loop() { while (gps.available( gpsPort )) { fix = gps.read(); work1(); } work2(); }

If work1() takes too long after getting a complete fix, and a new set of characters has lined up in the serial buffer then there is a danger that work2() never gets executed anymore in between.

To mitigate this using an if would guarantee that for every fix work1 and work2 would both be executed (and in all other cases work2 would be iterated on). I do not see any disadvantage of the if vs the while here.

Of course there is always the danger, that with work2() taking too long we will miss characters picked up by the gps.available(). But this danger exists in both cases.