user58652: In my use case I have multiple devices connected to the serial ports of an arduino. Currently I write to them using Serial1.write(), Serial2.write(), etc. 
How can I choose the port programmatically, for example:
for(int i = 0; i < numDevices; i++){
  Serial[i].write();
}


user7508: You may use an array of pointers to the actual serial ports. For
example, on an Arduino Mega:
const size_t PORTS_COUNT = 4;
const HardwareSerial *ports[PORTS_COUNT] = {
    &Serial, &Serial1, &Serial2, &Serial3
};

void setup() {
    for (size_t i = 0; i < PORTS_COUNT; i++)
        ports[i]->begin(9600);
}


58652: <p>In my use case I have multiple devices connected to the serial ports of an arduino. Currently I write to them using <code>Serial1.write()</code>, <code>Serial2.write()</code>, etc. </p>

<p>How can I choose the port programmatically, for example:</p>

<pre><code>for(int i = 0; i &lt; numDevices; i++){
  Serial[i].write();
}
</code></pre>
 7508: <p>You may use an array of pointers to the actual serial ports. For
example, on an Arduino Mega:</p>

<pre class="lang-cpp prettyprint-override"><code>const size_t PORTS_COUNT = 4;
const HardwareSerial *ports[PORTS_COUNT] = {
    &amp;Serial, &amp;Serial1, &amp;Serial2, &amp;Serial3
};

void setup() {
    for (size_t i = 0; i &lt; PORTS_COUNT; i++)
        ports[i]-&gt;begin(9600);
}
</code></pre>
