Usage

Include the header file to use the serialMux library.

#include <serialMux.h>

The library provides the SerialMux and VSerial classes, which are used to create virtual serial devices.

First create a serial multiplexer.

SerialMux mux(Serial);

The serial multiplexer can be used to create multiple virtual serial devices.

VSerial serialA(mux);
VSerial serialB(mux);

The physical serial device must be configured as usual.

void setup(void) {
  Serial.begin(9600);
}

Virtual serial devices can be used just like the familiar physical serial device.

void loop(void) {
  serialA.println("Virtual serial device A.");
  serialB.println("Virtual serial device B.");
  delay(1000);
}

Hard coded Serial workaround

Some libraries will write debugging or other information directly to the physical serial device. To force this library to use one of the virtual serial devices, the following lines can be added before the library is included.

HardwareSerial& masterSerial = Serial;
SerialMux mux(masterSerial);
VSerial serialA(mux)
#define Serial serialA

#include <hardcodedSerialUsingLib.h>

Note that any communication with the physical serial device must now be done using masterSerial instead of Serial.

void setup(void) {
  masterSerial.begin(9600);
}