# Want to read two serial ports on demand

**URL:** <https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948>\
**Category:** General Guidance\
**Created:** [June 17, 2014, 1:52pm UTC](https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948 "2014-06-17T13:52:42Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [June 17, 2014, 1:52pm UTC](https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948/1 "2014-06-17T13:52:42Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![HazardsMind](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hazardsmind/32/215209_2.png) [@HazardsMind](https://forum.arduino.cc/u/HazardsMind)\
**Post date:** [June 17, 2014, 2:01pm UTC](https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948/2 "2014-06-17T14:01:15Z")

</div>

> 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.

---

<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:** [June 17, 2014, 2:08pm UTC](https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948/3 "2014-06-17T14:08:02Z")

</div>

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.

---

<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:** [June 17, 2014, 2:21pm UTC](https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948/4 "2014-06-17T14:21:12Z")

</div>

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...

---

<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 5, 2021, 5:12pm UTC](https://forum.arduino.cc/t/want-to-read-two-serial-ports-on-demand/240948/5 "2021-05-05T17:12:18Z")

</div>


