Post topics, source code that relate to the Arduino Platform

User avatar
By ardubit
#87100 Platform
Hardware: Wemos D1 Mini
SoftwareSerial D3 -> RX, D4 -> TX

Problem Description
I am using SoftwareSerial to serve a GPS module which is sending data continuously at various time intervals. I am polling SSerial in the main loop and trying to serve the main loop as faster as I can to do. But, I am losing the data from SSerial or even can't catch when the buffer has data to read. SSerial buffer is not overflowed it's just become empty when pollGPS() is calling.

Many examples working pretty good if the main loop has only a reading SoftwareSerial routine, but... (if there are buttons, display) and I do not use delays or while loops.

I stuck with the problem. I am thinking about a solution: if data arrived, immediately call a function and parse it or maybe copy it into the buffer and set a flag. Asking for friendly help from the community. I played with interrupts (set hardware interrupt on RX pin, yes it's crazy) and it's crashes everything on esp8266. Ticker lib is not helping. It would be great to have a callback function or any solution that would help.

There is a prototype inside of SoftwareSerial.h:

Code: Select all /// Set an event handler for received data.
void onReceive(std::function<void(int available)> handler);
but can't see how to use it, it is very close to my problem.


Code: Select allvoid loop()
{
  if (ssPort.available() > 0)
  {
    pollGPS();
  }

btn.poll();
// etc


Code: Select allvoid pollGPS()
{
  while (gps.available(ssPort))
  {
    fix = gps.read(); // When a fix is finally completed, gps.available will return true

    if (fix.valid.location)
    { // Only save data if we have a fix
      statusPollGPS = true;
      lat = fix.latitude();
      lng = fix.longitude();
    }
  }
}


Thx so much for your help.