SoftwareSerial in a typdef struct?

Hello,

I have a project (based on Uno) with a bunch of SoftwareSerial ports defined. I want to send a given piece of output to a given SoftwareSerial port, based on program logic. At the moment I am doing this with a switch case statement, and a case for each port, but this seems clunky and inelegant, and I wonder if there is a better way?

I am wondering whether I can wrap SoftwareSerial in a typedef struct, then create an array of the new data type. Then I could choose a port based on the array index. I'm aiming at something a bit like this (I know this code is wrong!)

typedef struct {
  SoftwareSerial mySerial();
} SerialWrapper;

SerialWrapper SerialPorts[4];

int PortNum = 1;
char SomeData = 'X'

SerialPorts[PortNum].write('SomeData');

But then I get lost in how to actually instantiate the ports with Rx/Tx pins, .begin, and how to call the writes (I'm not interested in reading from the ports, BTW).

Any hints would be much appreciated!

Andrew

Defining a typedef is completely unnecessary. Just create an array of pointers.

SoftwareSerial *ports[] = { new SoftwareSerial(1, 2), new SoftwareSerial(3, 4), new SoftwareSerial(5, 6) };
int currentPort = 1;

...

ports[currentPort]->print("Hello, world!\n");

Regards,
Ray L.

Thank you, that's much simpler! I'll have a play with that.

Andrew

I'll have a play with that.

Don't use pin 1, though.