I am trying to use several (four) software-based serial ports to talk to different peripheral hardware. However, I am unable to receive data through the SoftSerial when using more than one software serial port.
I tested each pair of pins be defining it to the first port. All pairs are working as desired. In addition, I am only interested in bridging/piping from a software serial port to the hardware serial (for debugging reasons).
My code is as follows:
#include <stdint.h>
#include <SoftwareSerial.h>
#define SOFT_SERIAL_A_RX 5
#define SOFT_SERIAL_A_TX 6
#define SOFT_SERIAL_B_RX 7
#define SOFT_SERIAL_B_TX 8
#define SOFT_SERIAL_C_RX 9
#define SOFT_SERIAL_C_TX 10
#define SOFT_SERIAL_D_RX 11
#define SOFT_SERIAL_D_TX 12
#define SERIAL_BAUD_RATE 9600
SoftwareSerial softwareSerials[] = {
SoftwareSerial(SOFT_SERIAL_A_RX, SOFT_SERIAL_A_TX),
// Unable to receive data on first software serial when un-commenting following lines.
// All pins are working when declaring each pair for first software serial (tested pins by doing this)
// SoftwareSerial(SOFT_SERIAL_B_RX, SOFT_SERIAL_B_TX),
// SoftwareSerial(SOFT_SERIAL_C_RX, SOFT_SERIAL_C_TX),
// SoftwareSerial(SOFT_SERIAL_D_RX, SOFT_SERIAL_D_TX)
};
const uint8_t numberSoftwareSerials = sizeof(softwareSerials)/sizeof(*softwareSerials);
void initHardwareSerial()
{
Serial.begin(SERIAL_BAUD_RATE);
}
void initSoftwareSerials()
{
for(uint8_t i=0; i<numberSoftwareSerials; i++) {
softwareSerials[i].begin(SERIAL_BAUD_RATE);
}
}
void setup()
{
initHardwareSerial();
initSoftwareSerials();
}
void loop()
{
// Only one software serial can be used (receive/transmit) at once.
// So just using a single software serial port (first element from array)
if (softwareSerials[0].available()) {
Serial.write(softwareSerials[0].read());
}
if (Serial.available()) {
softwareSerials[0].write(Serial.read());
}
}
How can I narrow down the issue of being unable to receive when defining several serial ports? If there is no suitable solution I might switch to an Arduino Mega with its four hardware ports (would need only one additional software serial in that case), but an Arduino Nano is strongly preferred due to smaller footprint.