Receive data through multiple serial ports

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?

#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);
  }
}


image

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

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 - simple reliable ways to receive data.

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

You need to call

serialport.listen()

to "switch" softwareserial ports.

Also, consider using a robust serial transfer library 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:

#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:

#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);
  }
}

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


image

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

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.

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

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.

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

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.

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:

thanks for everyone's answers

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

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

DJMatrix-HU3:

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