Want to read two serial ports on demand

I am working with RFID reader. I want to read values when card pass over the reader. But only one reader works at one time. Because other serial port is not initialized. For that I use port.listen() function but it didn’t help me because some time it read data and some time not. My code is given below suggest me what I have to do. My board is UNO.

#include <SoftwareSerial.h>
SoftwareSerial RFID(8, 9); // RX and TX
SoftwareSerial RFIDD(2, 3); // RX and TX

int data1;
int data2;
int data3;
int ok = -1;
int in = 13;
int out = 12;
int i;
byte byteRead;

void setup()
{
  Serial.begin(2400);  // start serial to PC 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
   RFID.begin(2400);
   RFIDD.begin(2400);
   // start serial to RFID reader

  pinMode(in, OUTPUT); // for status LEDs
  pinMode(out, OUTPUT);
}

void readvalues() {
  RFID.listen();
     if (RFID.available()>0) 
  {
      delay(400);
   for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
  {
      data2 = RFID.read();
      Serial.print(data2);
   }
      RFID.flush(); // stops multiple reads

     //Serial.print(" ");
  
  }
  RFIDD.listen();
  if (RFIDD.available()>0) {
      delay(400);
    for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
    {
      data1 = RFIDD.read();
      Serial.print(data1);
    }
      RFIDD.flush(); // stops multiple reads

     //Serial.print(" ");
  
  }

}

void loop()
{
   
  readvalues();
  
  if (Serial.available()) {
  byteRead = Serial.read();
  
  //listen for numbers between 0-9
     if(byteRead  == 49){
          //number found
      digitalWrite(in, HIGH);
      delay(1000);
      digitalWrite(in, LOW);
      
      
    }else if(byteRead == 48){
            //number found
      digitalWrite(out, HIGH);
      delay(1000);
      digitalWrite(out, LOW);
    }
  }
}

Serial.begin(2400); // start serial to PC

Why so slow? Bump it up to 115200, its better.

But only one reader works at one time.

Of course, the arduino cannot multi-thread, it can only do things one after the other. You can give the appearance of multi-threading by switching between everything very fast, and instead of using FOR loops to cycle through and collect the data, you should use IF statements and keep track of their individual counts.

SoftwareSerial can only receive from one serial port at a time - it's not really suitable for this project. You might be able to bodge something together using AltSoftSerial for the second input, but a far better solution would be to move to an Arduino platform with multiple hardware serial ports. Alternatively, connect each reader to a TTL serial-to-USB adapter and put your code to handle them on the PC.

Thanks PeterH. Your method is very helpful for me. AltSoftSerial is working fine for me. Thanks once again. And also thanks to HazardsMind because now system is working fast...