can you put your software serial ports in an array?

Hey guys,

Been messing about with serial communications on pi and arduino for a couple months now and have come across an idea which would make my life a lot easier. Take the following example:

#include <SoftwareSerial.h>

SoftwareSerial arduino1(4, 5);
SoftwareSerial arduino2(2, 3);
SoftwareSerial laptop(0, 1);

Arduino connected to a laptop via usb and then two other arduinos via separate rx/tx lines. This arduino is basically emulating a bus line from the laptops point of view, access to multiple arduinos from one line (I might switch to 485 or another protocol if this seams like a dead end).

/ arduino1
| laptop--arduino< |
\ arduino2 /

How could i do the equivalent of:

SoftwareSerial serialPorts[3] = {arduino1, arduino2};

so as to be able to do:

arduinoSelection = laptop.read() - 1;
//laptop.read() - 1 so i can select arduino 1 by inputting a 1 and still be selecting [ 0 ]
data = laptop.read();

serialPorts[arduinoSelection].print(data);

I haven't got to it on an arduino yet but I remember there being various data conversion steps needed to get the "read 'data' then send 'data'" part of the example above working on a pi, I'll figure that one out for myself if it is an issue, I just want to know if there's a way of using an array to select the ports. Plan b can always be a long list of if statements but that seams very uncool. Also very much open to other solutions, I am after all an arduino noob

Two questions:

  1. Why use software serial to the laptop? Why not use the USB/hardware serial for that connection?

  2. Because of the blocking issues with SoftwareSerial how reliable is it going to be when trying to talk and listen on three ports at once? Will the SoftwareSerial library overlap in any way when using more than one instance?

It may be possible to create an array of SoftwareSerial ports but because it makes no sense to have more than one SoftwareSerial port the question is only of academic interest.

For your application you need an Arduino Mega which has 3 spare HardwareSerial ports.

...R
Serial Input Basics - simple reliable ways to receive data.

adws,
(1) yeah I'm changing that, I just copied the serial setup part from another script i wrote and didn't think about it but it hasn't caused me any issues so far.
(2) the arduino's 1&2 will be slaves, only listening then responding to confirm the checksum, for now at least. I plan to at some point make them send data back to what is at the moment the laptop but again that will only be on request so there'll never be two arduino's comunicating at the same time.

Robin,
What makes you say it makes no sense to use more than one software serial port? In the setup I've described I can so far send commands to 6 arduino's from an arduino nano. Not theoretically, but tested on breadboard.

schauerquigleys:
Robin,
What makes you say it makes no sense to use more than one software serial port? In the setup I've described I can so far send commands to 6 arduino's from an arduino nano. Not theoretically, but tested on breadboard.

You did not say that you were only using them to transmit.

...R

I plan to at some point make them send data back

Be sure that you are prepared for that to NOT work worth a damn. Only one instance of SoftwareSerial can listen at a time.

At any given time, if you have seven instances, which one should be listening?

It's like having 6 babies in 6 different rooms, with baby monitors in each room, and only one receiver in the room you are in. There is NO way, with that setup, to monitor all 6 babies at the same time. Even if you could switch the receiver to listen to one room at a time, you would miss what is happening in the other 5 rooms while listening to one room.