An Arduino library to wrap multiple kinds of serials.
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11); // RX, TX
void printStream(Stream* serial) {
serial->print("hi at ");
serial->println(millis());
}
void setup() {
Serial.begin(115200);
softSerial->begin(115200);
}
void loop() {
printStream(&Serial);
printStream(&softSerial);
delay(1000);
}
#include "SomeSerial.h"
For popular Arduino family.
SomeSerial someSerial(&Serial);
For official Arduino SAMD boards. (SAMD boards by Adafruit use Serial.)
SomeSerial someSerial(&SerialUSB);
SomeSerial someSerial(10, 11); // RX, TX
or
SoftwareSerial mySoftwareSerial(10, 11); //RX, TX
SomeSerial someSerial(&mySoftwareSerial);
Be careful that not all pins support SofwareSerial.
Please check or test whether using pins for SoftwareSerial work or not.
ARM and ESP32 does not support SoftwareSerial.
void setup() {
someSerial.begin(115200);
}
void loop() {
someSerial.println("Hello world!");
delay(1000);
}
if (someSerial->isHardwareSerial()) {
someSerial->println("It is HardwareSerial");
someSerial->thisHardwareSerial->println("Direct print to HardwareSerial");
}
if (someSerial->isSoftwareSerial()) {
someSerial->println("It is SoftwareSerial");
someSerial->thisSoftwareSerial->println("Direct print to SoftwareSerial");
}
if (someSerial->isSerial_()) {
someSerial->println("It is USBAPI Serial_");
someSerial->thisSerial_->println("Direct print to USBAPI Serial_");
}
MIT