# Receive data through multiple serial ports

**URL:** <https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307>\
**Category:** Networking, Protocols, and Devices\
**Created:** [November 29, 2019, 7:53am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307 "2019-11-29T07:53:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![DJMatrix-HU3](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DJMatrix-HU3](https://forum.arduino.cc/u/DJMatrix-HU3)\
**Post date:** [November 29, 2019, 7:53am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/1 "2019-11-29T07:53:38Z")

</div>

Hello people I need to receive data through multiple serial ports

I tried but when using **available ()\> 0** but it only works for serial1

how can i do this? do i need an extra library?

```cpp
#include<SoftwareSerial.h> 

#define MAX_PIN 2 //MAX485 control pin
#define MAX_PIN_2 3 //MAX485 control pin
#define MAX_PIN_3 4 //MAX485 control pin

SoftwareSerial RS485(10,11);
SoftwareSerial RS485_2(14,15);

void setup(){
  Serial.begin(9600);
  Serial1.begin(9600);
  RS485.begin(9600);
  RS485_2.begin(9600);
  
  Serial.setTimeout(100);
  Serial1.setTimeout(100);
  RS485.setTimeout(100);
  RS485_2.setTimeout(100);
  
  pinMode(MAX_PIN, OUTPUT);
  pinMode(MAX_PIN_2, OUTPUT);
  pinMode(MAX_PIN_3, OUTPUT);
}

void loop(){
  if(Serial1.available() > 0){
  String received = Serial1.readString();
  Serial.println(received);
  }
  
  if(RS485.available() > 0){
  String received = RS485.readString();
  Serial.println(received);
  }

  if(RS485_2.available() > 0){
  String received = RS485_2.readString();
  Serial.println(received);
  }
}

```

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/f/4/7/f4764a7282054ae41c92e8eea921a84be9819529.jpeg)  
[image](https://forum.arduino.cc/uploads/short-url/ySBWg8cCYmb81pZlZ7mYxMlYuZX.jpeg)

as you can see in the picture

MEGA 2560 (master) = 2 RS485 (receive from uno / send to datacenter)  
UNO (slave) = 1 RS485 that (Sends when Receives master request)

problem is occurring in the datacenter, I need to receive information from several masters at the same time

currently slave and master work perfectly

the only solution was to use multiple rs485 to receive but at the moment I can only receive from one.

datacenter needs to receive information from all master

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [November 29, 2019, 8:39am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/2 "2019-11-29T08:39:13Z")

</div>

SoftwareSerial is really only suitable for a single instance. If you have multiple instances only one can work at any one time.

If you need multiple serial ports use an Arduino Mega which has 3 spare HardwareSerial ports.

If you are using RS485 why do you need more than one serial port?

...R  
[Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data.

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [November 29, 2019, 8:40pm UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/3 "2019-11-29T20:40:12Z")

</div>

You are between a Rock and a Hard Place, I just went through this process. The software serial gave me an advantage that I could turn the transmitter off after the message was sent. I was not able to do that with the mega, the information that the last bit has been sent is not available. I use Nano, Uno and Mega on my network but with different purposes. What I wound up doing is modifying the eBay cheap RS484 boards so I had auto TX enable, this is working great at 19200 baud, however the boards are SMD. What I did since my system is multi master is have each unit monitor its own transmission then check if it was valid. The Mega was nice in that it would buffer the whole packet then when finished I could just compare the sent packet to what I received. The remotes would send an ACK if they received a good packet. I have the ACK but I have not done anything with them yet. Since this in a non arbitrating bus my packets are about 13 bytes in length so they go fast. I do test to see if the buss is busy, if so pause a short time.This response is to help you get started in solving your problem, not solve it for you.  
Good Luck & Have Fun!  
Gil

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [November 30, 2019, 1:57am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/4 "2019-11-30T01:57:42Z")

</div>

You need to call

```cpp
serialport.listen()

```

to "switch" softwareserial ports.

Also, consider using a [robust serial transfer library](https://github.com/PowerBroker2/SerialTransfer) to automatically packetize and parse data between Arduinos. The library can be found in the Arduino IDE's Libraries Manager and is super easy to use:

Example code:

TX Arduino:

```cpp
#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

```

RX Arduino:

```cpp
#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

```

---

<div class="post-metadata">

**Author:** ![DJMatrix-HU3](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DJMatrix-HU3](https://forum.arduino.cc/u/DJMatrix-HU3)\
**Post date:** [November 30, 2019, 5:20am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/5 "2019-11-30T05:20:08Z")

</div>

Thanks for all the answers! I will explain my problem better

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/f/4/7/f4764a7282054ae41c92e8eea921a84be9819529.jpeg)  
[image](https://forum.arduino.cc/uploads/short-url/ySBWg8cCYmb81pZlZ7mYxMlYuZX.jpeg)

as you can see in the picture

MEGA 2560 (master) = 2 RS485 (receive from uno / send to datacenter)  
UNO (slave) = 1 RS485 that (Sends when Receives master request)

problem is occurring in the datacenter, I need to receive information from several masters at the same time

currently slave and master work perfectly

the only solution was to use multiple rs485 to receive but at the moment I can only receive from one.

datacenter needs to receive information from all master

---

<div class="post-metadata">

**Author:** ![mikb55](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikb55/32/1149025_2.png) [@mikb55](https://forum.arduino.cc/u/mikb55)\
**Post date:** [November 30, 2019, 7:06am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/6 "2019-11-30T07:06:44Z")

</div>

If you have exactly 4 masters then you can use 1 software serial and the 3 hardware serial ports on a Mega2560. Look for the Tx/Rx labels next to pins 14-19.

If you have more than 4 masters then you will need to use multiple software serial ports and enforce 'round robin' coordination so that only one master is sending data at any point in time. You could use 'flow control' messages or hardware outputs to signal when each master is allowed to send data.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [November 30, 2019, 8:21am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/7 "2019-11-30T08:21:15Z")

</div>

> DJMatrix-HU3:  
> problem is occurring in the datacenter, I need to receive information from several masters at the same time

Why not have the datacentre poll the Megas one at a time to collect their latest data?

OR

Why not have an RS485 link between the datacentre and the other Megas?

OR

Why not connect each master to the PC with a USB cable and dispense with the datacentre?

...R

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [November 30, 2019, 9:05am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/8 "2019-11-30T09:05:22Z")

</div>

> DJMatrix-HU3:  
> problem is occurring in the datacenter, I need to receive information from several masters at the same time

Possibly when you say info from several masters at the same time you mean simultaneous? With very limited info, yes you can.

Mega2560 has many ports that you can read 8 bits simultaneous in a fraction of a microsecond and process those with bit logic, but is that 8 masters w/1 bit each or 4 w/2 bits or can multiple ports be read quick enough to call "at the same time", is maybe 4 microsecs to get 32 bits info fast enough? Direct port read is 5x faster than Arduino digitalRead(), works in Arduino IDE.

All ATmega chip UARTs are capable of being full speed master-mode SPI ports. The Mega can keep Serial and still host 4 SPI buses to hang the slaves on but use small boards with header pins or custom PCBs, not Unos.

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [November 30, 2019, 4:56pm UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/9 "2019-11-30T16:56:54Z")

</div>

As a former systems analyst, I have to ask about the VALUE of the data you are dealing with? If data is missing or corrupted or incomplete, is anything of value lost? Your system seems to give NO value to the data. Is this the case?

Paul

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [December 1, 2019, 1:18am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/10 "2019-12-01T01:18:44Z")

</div>

> problem is occurring in the datacenter, I need to receive information from several masters **_all on USB_** at the same time

Simple as get a powered USB HUB?

You have 2 USB ports on your PC? Plug an Uno into each and open Arduino IDE twice. Associate each to a different board/serial port. Your PC program can read them both, I hear that Python is suited.

---

<div class="post-metadata">

**Author:** ![DJMatrix-HU3](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DJMatrix-HU3](https://forum.arduino.cc/u/DJMatrix-HU3)\
**Post date:** [December 2, 2019, 2:48am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/11 "2019-12-02T02:48:52Z")

</div>

> Robin2:  
> Why not have the datacentre poll the Megas one at a time to collect their latest data?
> 
> OR
> 
> Why not have an RS485 link between the datacentre and the other Megas?
> 
> OR
> 
> Why not connect each master to the PC with a USB cable and dispense with the datacentre?
> 
> ...R

Wow, a solution was in front of me and I didn't realize

I could just use the same slave and master connection logic for datacenter

Thank you all for helping me come up with the solution!

in this case the connection would look like this:

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/4/e/e/4eee4d0eee03089ed282ed5246a1b2d2483cec82.png)

thanks for everyone's answers

---

<div class="post-metadata">

**Author:** ![DJMatrix-HU3](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DJMatrix-HU3](https://forum.arduino.cc/u/DJMatrix-HU3)\
**Post date:** [December 2, 2019, 3:03am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/12 "2019-12-02T03:03:27Z")

</div>

> Paul\_KD7HB:  
> As a former systems analyst, I have to ask about the VALUE of the data you are dealing with? If data is missing or corrupted or incomplete, is anything of value lost? Your system seems to give NO value to the data. Is this the case?
> 
> Paul

yes, the data has a value and is of fundamental importance

on my master / slave test system there was no byte loss ... all slaves are mapped to c ++ software and there is a timeout that detects when a slave is not responding

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [December 2, 2019, 3:09am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/13 "2019-12-02T03:09:35Z")

</div>

> DJMatrix-HU3:  
> yes, the data has a value and is of fundamental importance
> 
> on my master / slave test system there was no byte loss ... all slaves are mapped to c ++ software and there is a timeout that detects when a slave is not responding

Thanks for answering. Then you really need to be able to detect one or two or more bit errors in your messages. When everything is perfect during testing, then you need to be able to discover if an error ever occurs.

Paul

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [December 2, 2019, 1:02pm UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/14 "2019-12-02T13:02:13Z")

</div>

> DJMatrix-HU3:
> 
> ![](https://europe1.discourse-cdn.com/arduino/original/4X/4/e/e/4eee4d0eee03089ed282ed5246a1b2d2483cec82.png)

Have you ever used SPI bus? The masters would all connect to the center directly on the bus at 512KB/s.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 11:18am UTC](https://forum.arduino.cc/t/receive-data-through-multiple-serial-ports/623307/15 "2021-05-06T11:18:12Z")

</div>

Closed on May 6, 2021, 11:18am UTC


