Arduino mega software serial

I tried using software serial in Arduino Mega using following code to get data from RFID readers.

#include <SoftwareSerial.h>
uint32_t serialNo = 0;
uint32_t serialNo1 = 0;


SoftwareSerial Serial4 (62,63);//A8,A9
SoftwareSerial Serial5 (10,11);


void setup(){
  Serial.begin(9600); // start serial to PC
  Serial4.begin(9600);
  Serial5.begin(9600);
 
}

void loop(){

  if(Serial4.available()>0){
  reader1();
  }
  if(Serial5.available()>0){
  reader2();  
  }
  
}
void reader1(){
if (Serial4.available() > 3){
    serialNo = 0;
    for (uint8_t x = 0; x < 4; x++){
      serialNo = serialNo << 8;
      serialNo = serialNo | Serial4.read();
    }
    Serial.println(serialNo);
  }
}
void reader2(){
if (Serial5.available() > 3){
    serialNo1 = 0;
    for (uint8_t x = 0; x < 4; x++){
      serialNo1 = serialNo1 << 8;
      serialNo1 = serialNo1 | Serial5.read();
    }
    Serial.println(serialNo1);
  }  
}

when I use both Serial4 and Serial5 to read from two readers only one software serial gives the output(Serial5). When i use them individually both provides outputs. What is the reason. What have i done wrong

Read the documentation. SoftwareSerial can only listen on one at a time. You have to explicitly tell it which one you want to receive.

The other software serial variants may be able to do what you want.

Does this mean you need 6 serial ports (given that the Mega already has 4) ?

...R

Andre_Leonardo:
I tried using software serial in Arduino Mega using following code to get data from RFID readers.

#include <SoftwareSerial.h>

uint32_t serialNo = 0;
uint32_t serialNo1 = 0;

SoftwareSerial Serial4 (62,63);//A8,A9
SoftwareSerial Serial5 (10,11);

void setup(){
  Serial.begin(9600); // start serial to PC
  Serial4.begin(9600);
  Serial5.begin(9600);

}

void loop(){

if(Serial4.available()>0){
  reader1();
  }
  if(Serial5.available()>0){
  reader2(); 
  }
 
}
void reader1(){
if (Serial4.available() > 3){
    serialNo = 0;
    for (uint8_t x = 0; x < 4; x++){
      serialNo = serialNo << 8;
      serialNo = serialNo | Serial4.read();
    }
    Serial.println(serialNo);
  }
}
void reader2(){
if (Serial5.available() > 3){
    serialNo1 = 0;
    for (uint8_t x = 0; x < 4; x++){
      serialNo1 = serialNo1 << 8;
      serialNo1 = serialNo1 | Serial5.read();
    }
    Serial.println(serialNo1);
  } 
}




when I use both Serial4 and Serial5 to read from two readers only one software serial gives the output(Serial5). When i use them individually both provides outputs. What is the reason. What have i done wrong

Hello,

You have already 3 hardware serial, why do you want to use software one?

Robin2:
Does this mean you need 6 serial ports (given that the Mega already has 4) ?

...R

Yes, I need more serial ports.

Andre_Leonardo:
Yes, I need more serial ports.

I reckon you will get much better advice if you give details of the entire project, including what is connected to each of the 6 serial ports. There may be other solutions than having 6 serial ports.

You can get I2C UART modules that would effectively add additional hardware serial ports.

...R

Andre_Leonardo:
Yes, I need more serial ports.

as Robin2 said:

Robin2:
You can get I2C UART modules that would effectively add additional hardware serial ports.

...R

Here is a link for a quad UART I2C/SPI controller, this controller can do CTS/RTS or Xon/Xoff handshaking with 128byte tx/rx buffers for each channel.

MAX14830 Quad Uart

chuck.

chucktodd:
MAX14830 Quad Uart

Do you know if anyone makes a breakout board with one of these attached for easier use with an Arduino ?

I see that they claim usage in "Airplane Communication Buses". That suggests an eye-watering price. :slight_smile:

...R