# Reading from Two ports with SoftwareSerial probelm

**URL:** <https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511>\
**Category:** Networking, Protocols, and Devices\
**Created:** [April 18, 2020, 9:32am UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511 "2020-04-18T09:32:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![valerig](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@valerig](https://forum.arduino.cc/u/valerig)\
**Post date:** [April 18, 2020, 9:32am UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511/1 "2020-04-18T09:32:16Z")

</div>

Hi,

I am trying to read data from RFID reader and send data using Bluetooth. When I am trying to do it separately, everything works fine, but when I am trying to combine 2 ports, I have problems. I tried to read in multiple forums for a while and it didn't help much. Here is my code:

```cpp
#include <SoftwareSerial.h>

unsigned char TAG_1[38]={10,85,51,48,48,48,69,50,48,48,48,48,49,68,56,54,48,50,48,49,50,48,50,49,57,48,53,53,49,48,57,53,67,69,13,10,10};
unsigned char TAG_2[38]={10,85,51,48,48,48,51,48,48,56,51,51,66,50,68,68,68,57,48,49,52,48,48,48,48,48,48,48,48,48,51,57,66,66,13,10,10};
                      

SoftwareSerial RFIDSerial (8,9);
SoftwareSerial blueTooth (2,3);
unsigned char incomingByte;

void sendIdentifyCmd ()
{
  RFIDSerial.write (0x0A);    
  RFIDSerial.write (0x55);
  RFIDSerial.write (0x0D);
}

void copy_str(unsigned char *str_src, unsigned char *str_dest, int len){
  for(int i=0; i<len; i++){
    str_dest[i]=str_src[i];
  }
}

bool str_cmp(unsigned char *str1, unsigned char *str2, int len=37){
      for(int i=0; i<len; i++){
        if(str1[i]!=str2[i]){
          return false;
        }
      }
      return true;
}

void setup ()
{
  Serial.begin (9600);
  RFIDSerial.begin (38400);
  blueTooth.begin(38400);
  Serial.println ("begin initial Serial!\n");
 // blueTooth.println("lets bt\n");
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
}

void loop ()
{
  int i=0;
  unsigned char str[700]="";
  RFIDSerial.listen();
  if (RFIDSerial.isListening()) {  
    Serial.println("RFIDSerial is listening!"); //i am and getting this message
    }else{
      Serial.println("RFIDSerial is not listening!");
    }
  sendIdentifyCmd ();
  delay (2);
  
  while(RFIDSerial.available () > 0) //this loop is never reached because "he is available" is not printed
  {
    Serial.println("he is available");
    incomingByte=RFIDSerial.read();
    str[i]=incomingByte;

    Serial.print (str[i]);
    Serial.print (' ');
    i++;

  }
  unsigned char *temp_str=(unsigned char*)malloc((i+1)*sizeof(unsigned char));
  copy_str(str, temp_str, i);
  blueTooth.listen();
  blueTooth.println("hi");//i am and getting this message
  if(str_cmp(TAG_1, temp_str)){
  Serial.println();
  Serial.print("Hello Tag1\n");
  blueTooth.print("Hello Tag1 from bt");
  }
  if(str_cmp(TAG_2,temp_str)){
    Serial.println();
    Serial.print("Hello Tag2\n");
    blueTooth.print("Hello Tag2 from bt\n");
  }
  Serial.println();
  free(temp_str);
  delay (1000);
}

```

I put the method listen() in order to use different ports separately. As you can see, when I am trying to listen to RFID, I am getting the message that RFID is listening, and I even send the command to the reader to read tags. I can see on my module a blue led blinking which means that the reader is reading, but I am not able to read the data that comes from the reader. At the same time, I am sending data via the Bluetooth port when I am listening to it.

Now I am wondering who is the listen() method listens to.  
So the questions are:

1. Why RFIDSerial is not available and how to make it available?
2. who is the listen() method listens to?

Thanks in advance!

---

<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:** [April 18, 2020, 9:40am UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511/2 "2020-04-18T09:40:53Z")

</div>

It is just not practical to use two instances of SoftwareSerial. Only one of them can be active at any one time. If you need multiple serial ports you should use a Mega which has 3 spare HardwareSerial ports.

...R

---

<div class="post-metadata">

**Author:** ![valerig](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@valerig](https://forum.arduino.cc/u/valerig)\
**Post date:** [April 18, 2020, 11:51am UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511/3 "2020-04-18T11:51:31Z")

</div>

i understand that Mega is easier to use in this case. But there must be a solution for Uno. I am managing to send data to the RFID, but not able to receive from it

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [April 18, 2020, 11:58am UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511/4 "2020-04-18T11:58:54Z")

</div>

Maybe that delay(2) isn't long enough. It's dodgy to rely on a fixed delay to solve Serial problems anyway, better to read until you have all the characters you expect, timing out when it's obvious they're not all coming.

As a short term test though, I'd substantially increase the delay until you get something from the RFID.

---

<div class="post-metadata">

**Author:** ![valerig](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@valerig](https://forum.arduino.cc/u/valerig)\
**Post date:** [April 18, 2020, 12:09pm UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511/5 "2020-04-18T12:09:20Z")

</div>

I cant believe that the solution was so simple. Thank you so much!!!!!

---

<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:19am UTC](https://forum.arduino.cc/t/reading-from-two-ports-with-softwareserial-probelm/649511/6 "2021-05-06T11:19:58Z")

</div>


