Arduino library for buffered serial line reading
Should work on most of the boards, tested on UNO and ESP32DevKitV1
#include <serial-readline.h>
void received(char*);
SerialLineReader reader(Serial, received);
void setup() {
Serial.begin(115200);
}
void received(char *line) {
Serial.println(line);
}
void loop() {
reader.poll();
}
#include <serial-readline.h>
SerialLineReader reader(Serial);
void setup() {
Serial.begin(115200);
}
void loop() {
reader.poll();
if(reader.available()) {
char text[reader.len()];
reader.read(text);
Serial.println(text);
}
}
SerialLineReader(Serial)
SerialLineReader(Serial, isr)
SerialLineReader(Serial, bufsize)
SerialLineReader(Serial, bufsize, isr)
HardwareSerial is passed by reference. Default bufsize is 100. ISR is void function with one argument of type char*. It's called when line is ready to be read.
This library allows microcontroller to achieve better performance with Serial communication based on sending lines (terminated with \n). In Arduino polling is a must, because you don't have core support for idle detection. Polling is problematic, because if you poll too fast, data comes in separate pieces and figuring out when transfer is complete is kind of tricky. This lib extends your arsenal with SerialLineReader, which have poll() method used for polling. It receives data in separate pieces, writes to buffer, slices into lines and puts into queue, from which you can take those lines nice and easy using read() method. You can also specify function which will be called, when line is ready to be received.