/*
Forum: https://forum.arduino.cc/t/serial-readbytes-and-settimeout/1158069/5
WOKWI: https://wokwi.com/projects/373030858704846849
*/
#include <SoftwareSerial.h>
SoftwareSerial Serial1(4,5);
char printbuffer[32];
unsigned int rbufLen; //how many bytes received at once
char rbuf[32];
void setup()
{
Serial.begin(9600);
while (!Serial) { ; }
Serial1.begin(9600);
Serial1.setTimeout(10); // 10ms timeout
Serial.println(
"\nType '1' to read data"
"\n '2' for ?"
);
}
void loop()
{
if (Serial.available()) {
char c = Serial.read();
if (c =='1') {
sprintf(printbuffer, "Rx: %d \n", rbufLen);
Serial.print(printbuffer);
}
}
if (Serial1.available()) {
rbufLen=Serial1.readBytes(rbuf,32); //Serial1.flush();
}
}