Two SoftwareSerial ports

I´m using a ESP8266 module and want to receive data from 2 SoftwareSerial ports.
If i´m using just one SerialPort everything works fine.
With two serial ports it crashes when it receives data on port one.
Normally it prints out “loop” and then on data receive there is a wdt reset.
I attached the log of Serial(); What am i doing wrong here?

#include <SoftwareSerial.h> //https://github.com/plerup/espsoftwareserial

SoftwareSerial readerSerialOne(5, SW_SERIAL_UNUSED_PIN); // RX, TX
SoftwareSerial readerSerialTwo(4, SW_SERIAL_UNUSED_PIN); // RX, TX

void setup() 
{
  Serial.begin(115200);
  delay(100);
  readerSerialOne.begin(9600);
  delay(100);
  readerSerialTwo.begin(9600);  
  delay(100);
}

void loop() 
{   
      Serial.println("loop");   
      while(readerSerialOne.available() > 0) // Is data on Serial 1 available?
      {  
         Serial.println(readerSerialOne.read());
      }
      while(readerSerialTwo.available() > 0)  // Is data on Serial 2 available?
      {  
         Serial.println(readerSerialTwo.read());
      }
}

SerialLog.JPG

You are aware that only one instance of SoftwareSerial can be listening at a time, right?

Didn´t know you can only listen at a one port at a time.
I found that there is a command "listen()" but not for the ESP8266-SoftwareSerial :frowning:
Alright so can i somehow disable the readerSerialOne and listen on readerSerialTwo and the other way round?
Or what is the best way to handle this problem?

Best regards

Alright so can i somehow disable the readerSerialOne and listen on readerSerialTwo and the other way round?

What are you planning to randomly listen to?

If the ESP8266-SoftwareSerial has been crippled by removing the listen() method, then I don't see how you can change the active instance.

Unfortunately multiple copies of SoftwareSerial are not very practical.

You might like to try my Yet Another Software Serial alongside one copy of SoftwareSerial. However I have not tested that and it may not work.

The real answer is to use a Mega which has 3 spare hardware serial ports or a Leonardo or Micro which has 1 spare hardware serial port.

...R

I have 2 RFID readers connected to those 2 SoftwareSerial ports where i want to listen on.
Thx for your Code Robin but can i use it for the ESP8266 or does it need to be adapted?
Would the code below work?

#include <SoftwareSerial.h> //https://github.com/plerup/espsoftwareserial

int count = 0;

void setup() 
{
  Serial.begin(115200);
  //readerSerialOne.begin(9600);
  //readerSerialTwo.begin(9600);  
}

void loop() 
{            
      if(count==0)
      {
        SoftwareSerial readerSerial1(5, SW_SERIAL_UNUSED_PIN); // RX, TX
        readerSerial1.begin(9600);
        while(readerSerial1.available() > 0) // Is data on Serial 1 available?
        {  
          Serial.println(readerSerial1.read());
        }
        count=1;
      }
      
      if(count==1)
      {
        SoftwareSerial readerSerial2(4, SW_SERIAL_UNUSED_PIN); // RX, TX
        readerSerial2.begin(9600);
        while(readerSerial2.available() > 0)  // Is data on Serial 2 available?
        {  
           Serial.println(readerSerial2.read());
        }
        count=0;
      }      
}

Would the code below work?

For some definition of work, maybe.

What will happen if someone waves an RFID tag by reader two while reader one is being listened to?

To do two ports at the same time is going to take
some actual low level programming.
One might use two interrupt ports and snap shot the
edges for processing in the main loop.
The Adruinos have enough processing speed to keep up
but it is necessary to actually write embedded code,
rather than plug and play someone else's library, unless
you get lucky and find that someone did it for you
and it is compatible with the rest of your code.
Dwight

dawinci:
I have 2 RFID readers connected to those 2 SoftwareSerial ports where i want to listen on.
Thx for your Code Robin but can i use it for the ESP8266 or does it need to be adapted?
Would the code below work?

It is safest to assume that 2 SoftwareSerial ports won't work - unless you are sufficiently competent to understand and work around the limitations. Even then they may not be suitable.

I think my code should be able to work with any serial device but I wrote it mainly for people who are interested to study how it works. I am not in a position to provide any detailed technical support. You need to "suck-it-and-see" and "the ones with the teeth marks have the hard centres"

...R

I have successfully received two serial inputs simultaneously, at 9600 baud. I used AltSoftSerial and gSoftSerial. The first uses the hardware input capture mechanism and timer 1 (on an Atmega328) and is very tolerant of interrupt latency. The second library can use any pin, doesn't monopolize a timer and has an ISR that is only a little more sensitive to timing. Each of these libraries supports just a single connection but they worked okay together -- at 9600 baud.

The way I tested them was to connect the serial output of a GPS running at 9600 baud to two pins, one for AltSoftSerial and the second for gSoftSerial. So the data was literally streaming to both simultaneously. It is only because at 9600 baud there is so much time between data bits that the ISRs of each of these libraries had time to process the data with dropping characters.

If your sketch has other interrupts it could screw this up. Also, gSoftSerial only works with ASCII data.

Just found on GitHub - plerup/espsoftwareserial: Implementation of the Arduino software serial for ESP8266

Implementation of the Arduino software serial library for the ESP8266

Same functionality as the corresponding AVR libarary but several instances can be active at the same time. Speed up to 115200 baud is supported. The constructor also has an optional input buffer size.

Regarding to the readme of espsoftwareserial it should work i guess.
Did i miss something or why is the code on my first post not working?

I'm not at all familiar with the ESP8266, but it looks like it is able to listen to more than one serial channel at a time. However, when it detects an incoming byte it appears to disable GPIO interrupts which I think would mean you can't receive two inputs simultaneously.

// Disable GPIO interrupts when sampling the incoming byte
   ETS_GPIO_INTR_DISABLE();
   swSerObj->rxRead();
   ETS_GPIO_INTR_ENABLE();

dawinci:
Regarding to the readme of espsoftwareserial it should work i guess.
Did i miss something or why is the code on my first post not working?

Did you download and use the library from Github in the code in your first Post?

It is very unfortunate that the Github code uses the same file names as the normal SoftwareSerial library. I would change the file names if I was using it.

I have no experience of the library on the Github page. If it does what it says it may be very useful. I will bookmark it in the hope that I get time to test it.

…R

I used the SoftwareSerial from GitHub - plerup/espsoftwareserial: Implementation of the Arduino software serial for ESP8266
I added the folder "espsoftwareserial-master" to \arduino-1.6.5-r5\libraries
If i delete this folder it won´t compile. So the SoftwareSerial.h should be the right one.

But it´s still not running for 2 ports.
Can i somehow get the AltSoftSerial.h to run on ESP8266.
Because there i got a compile error that it only works for AVR boards

When I posted about AltSoftSerial I had momentarily forgotten that you're using an ESP8266. You'd have to port it yourself unless you can find someone else who has already done it. The same is true with gSoftSerial or Robin's Yet Another Software Serial. Neither will work without changes.

I don't know anything about the ESP8266 architecture but I'd be very surprised if it couldn't be made to work for two channels at 9600 baud.

My idea was to rename all things with “SoftwareSerial” to “SoftwareSerial1” in the SoftwareSerial.cpp and SoftwareSerial.h files.
So i would have under \arduino-1.6.5-r5\libraries two folders.
One for SoftwareSerial and another one for SoftwareSerial1. (Two independend Serial drivers)
But i cant #include SoftwareSerial.h → fatal error: SoftwareSerial.h: No such file or directory

If i set in the keywords.txt -->SoftwareSerial1 KEYWORD1 back to SoftwareSerial KEYWORD1
Then i can #include SoftwareSerial.h but #include SoftwareSerial1.h doesn´t work then.

Won´t my idea ever get to work?
I´m not a pro in driver programming so i need to use what is available unless someone can help me

espsoftwareserial-master1.zip (4.96 KB)

I tried now the normal Serial and SoftwareSerial. The problem here is i get different data on the Serial ports for the same Tag.
Tag Data1: 631662302301533023062502422022142420
Tag Data2: 6316623023015150204463838412142420

I checked the RFID-reader data on a Terminal program. (See attachment)
There i get the same data as for Tag Data2 (SoftwareSerial is ok)
So it seems the Hardware Serial has an issue or is my code crap?

 while(Serial.available() > 0) // Is data on Serial 1 available?
        {  
          delay(2);
          if(deleteTagID == true)
          {
            TagData1 =""; 
            deleteTagID = false;
          }
          byte incomingData1 = Serial.read();
          TagData1 = TagData1 + String(incomingData1);
          readingFinished = true;
        } 
                  
        while(readerSerial2.available() > 0)  // Is data on Serial 2 available?
        {  
          delay(2);
          if(deleteTagID == true)
          {
            TagData2 =""; 
            deleteTagID = false;   
          }
          byte incomingData2 = readerSerial2.read();
          TagData2 = TagData2 + String(incomingData2);
          readingFinished = true;
        }
               
      if(readingFinished == true)
      {        
        ESP.wdtFeed(); // Reset the WatchDog
        Serial.print("Tag Data1: ");
        Serial.println(TagData1);
        Serial.print("Tag Data2: ");
        Serial.println(TagData2);
        readingFinished = false;
        deleteTagID = true;
      }

Serial-USB_Converter.JPG

Prompted by Reply #14, are you trying to run the code directly on an ESP8266?

OR, is your ESP8266 connected to an Arduino and you want the code to run on the Arduino? (I have been assuming this is what you are trying to do).

…R

My code is running directly on the ESP8266.
I want to receive data from 2 serial ports there.

dawinci:
My code is running directly on the ESP8266.
I want to receive data from 2 serial ports there.

Then please disregard all of my suggestions - if they are valid it is just by accident. I know nothing about programming the ESP8266.

Just out of curiosity, what version of the ESP8266 are you using and how many I/O pins does it have?

...R