user33209: How can I declare multiple serial ports for Arduino Mega as it has been in the example below?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(18,19); // RX, TX  //how this syntax can be modified for multiple Rx module?

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

void loop(){
   Serial.write(45);    // send a byte with the value 45

   int bytesSent = Serial.write(â€œhelloâ€); //send the string â€œhelloâ€ and return the length of the string.
}


user10394: If you mean the built in serial ports then just user the other objects that are pre-declared as per:
https://www.arduino.cc/en/Serial/Begin

user972: From the official docs on serial:

The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).

Which means, you use Serial1 for serial on pins 19 and 18 instead of Serial:
void setup(){
   Serial1.begin(9600);
}

void loop(){
   Serial1.write(45);    // send a byte with the value 45

   int bytesSent = Serial1.write("hello"); //send the string "hello" and return the length of the string.
}


33209: <p>How can I declare multiple serial ports for Arduino Mega as it has been in the example below?</p>

<pre><code>#include &lt;SoftwareSerial.h&gt;
SoftwareSerial mySerial(18,19); // RX, TX  //how this syntax can be modified for multiple Rx module?

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

void loop(){
   Serial.write(45);    // send a byte with the value 45

   int bytesSent = Serial.write(â€œhelloâ€); //send the string â€œhelloâ€ and return the length of the string.
}
</code></pre>
 10394: <p>If you mean the built in serial ports then just user the other objects that are pre-declared as per:</p>

<p><a href="https://www.arduino.cc/en/Serial/Begin" rel="noreferrer">https://www.arduino.cc/en/Serial/Begin</a></p>
 972: <p>From the <a href="https://www.arduino.cc/en/Reference/Serial" rel="noreferrer">official docs on serial</a>:</p>

<blockquote>
  <p>The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).</p>
</blockquote>

<p>Which means, you use <code>Serial1</code> for serial on pins 19 and 18 instead of <code>Serial</code>:</p>

<pre class="lang-cpp prettyprint-override"><code>void setup(){
   Serial1.begin(9600);
}

void loop(){
   Serial1.write(45);    // send a byte with the value 45

   int bytesSent = Serial1.write("hello"); //send the string "hello" and return the length of the string.
}
</code></pre>
