struct Listener {
public:
HardwareSerial& port;
char message[8 + 1];
byte currentIndex;
Listener(HardwareSerial& p) : port(p), currentIndex(0) {}
void begin() {
port.begin(115200);
}
const char * listen() {
int r = port.read();
if (r == -1) return nullptr;
message[currentIndex++] = (char) r;
if (currentIndex >= 8) {
message[8] = '\0';
currentIndex = 0;
return message;
}
if (currentIndex >= 8) currentIndex--;
return nullptr;
}
};
Listener ports[] = {{Serial}, {Serial1}, {Serial2}, {Serial3}};
void setup() {
for (auto&p : ports) p.begin();
Serial.println("Ready");
}
void loop() {
static uint32_t t0;
static uint32_t count = 0;
const char * m;
for (auto&p : ports) {
m = p.listen();
if (m != nullptr) Serial.println(m);
}
count++;
if (millis() - t0 >= 1000) {
Serial.println(count);
count = 0;
t0 = millis();
}
}