SerialBufferRK intermittently stops reading serial port

I am using SerialBufferRK on monitor one to read in serial data from Serial1. After random amounts of time (sometimes a couple of hours, sometimes 20 minutes the available() function returns 0 despite serial data still being transmitted to it. It seems like maybe the thread is being cancelled but I am unsure how to tell if that is the case, here is my code:

#include "Particle.h"
#include "edge.h"
#include "SerialBufferRK.h"
#include <set>

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

SerialBuffer<12288> serBuf(Serial1);

STARTUP(
  Edge::startup();
);

void setup() {
  Serial1.begin(115200, SERIAL_8N1);
  serBuf.setup();

  serBuf.readClear();
  Edge::instance().init();
}

void loop() {
if (serBuf.available() >= 24) {
    std::string data;
    while (serBuf.available() > 0) {
      char c = serBuf.read();
      if (c == '\n') break;
      data+= c;
    }
  Edge::instance().loop();
}

The data is then published to particle in a set

There is really no reason to use that library. You can resize the serial buffer in Device OS 3.2.0 and later.

But in any case, you shouldn't buffer until available is a certain number. Instead, grab every character into a buffer and process it when there's enough data.

1 Like

Thankyou, I wasn't aware that you could resize the buffer. I saw a comment in the code where someone had said the serial buffer size was stuck at 64 bytes and I trusted it when I guess I shouldn't have!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.