Multiple softwareserial communication issue

Hello All, i have 2 barcode scanners with serial out connected with arduino …and single arduino with single barcode scanner works great…but when i use both of them it doesnt show anything…on serial monitor…I am using softwareserial library…

  #include <SoftwareSerial.h>  
SoftwareSerial mySerial(2, 3); // RX, TX
SoftwareSerial mySerial1(4, 5); // RX, TX

void setup()  
{
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  pinMode(4,INPUT);
  pinMode(5,OUTPUT);
  
  mySerial.begin(9600);
  mySerial1.begin(9600);

}

void loop() 
{
    if (mySerial.available()){
    char inChar = mySerial.read();
    Serial.print(inChar);}
    if (mySerial1.available()){
    char inChar1 = mySerial1.read();
    Serial.print(inChar1);}

Multiple instances of SoftwareSerial just don’t work. Only one can listen or talk at any one time.

The best solution is to get an Arduino Mega which has 3 spare HardwareSerial ports, or a Leonardo or Micro which have 1 spare port.

You might be able to get two software serial systems working if you use one of the other variants such as NeoSoftSerial or AltSoftSerial. But it will still be nowhere near as good as HardwareSerial

…R

can i use one hardwareserial(0,1) and one softwareserial(2,3)?

Robin2:
You might be able to get two software serial systems working if you use one of the other variants such as NeoSWSerial or AltSoftSerial. But it will still be nowhere near as good as HardwareSerial

+1

AltSoftSerial is the best software serial library. It will not interfere with other libraries while transmitting and/or receiving. It only works on two specific pins (8 & 9 on an UNO).

NeoSWSerial is almost as good. It does not interfere with other libraries while receiving, but it blocks everything else while transmitting. It works on any two pins.

SoftwareSerial is very inefficient because it blocks everything else while receiving or transmitting. It cannot transmit and receive at the same time.

In your case, using Serial for one reader (pins 0 & 1) and AltSoftSerial (pins 8 & 9) for the other would work pretty well. You would have to disconnect RX pin 0 from the scanner to upload a new sketch. Some people put a switch in that wire so it’s easy to upload.

If you never send anything to the scanners, you don’t have to connect Arduino TX pins to the scanner RX pins. Here is the code for that:

#define scanner1 Serial // just an alternative name for the same port

#include <AltSoftSerial.h>
AltSoftSerial scanner2;  // pins 8 & 9

void setup()
{
  Serial.begin( 9600 );
  scanner2.begin( 9600 );
     ...
}

void loop()
{
  if (scanner1.available())  // actually Serial, but this is easier to read
    Serial.write( scanner1.read() ); // echo to Serial monitor window

  if (scanner2.available())
    Serial.write( scanner2.read() ); // echo to Serial monitor window
}

Notice that you can still print debug statements with this wiring.

Serial plus NeoSWSerial is almost as good, if you are only receiving on the NeoSWSerial port:

#define scanner1 Serial // just an alternative name for the same port

#include <NeoSWSerial.h>
NeoSWSerial scanner2( 2, 3 );

I do not recommend using 2 software serial ports at the same time:

#include <AltSoftSerial.h>
AltSoftSerial scanner1;  // pins 8 & 9

#include <NeoSWSerial.h>
NeoSWSerial scanner2( 2, 3 );

It might work most of the time, but it may not be reliable when the two scanners transmit at the same time – some data might get corrupted. You’ll have to do some testing.

Both libraries are available from the Arduino IDE Library Manager, under the menu Sketch → Include Library → Manage Libraries.

ok Thanx alot...-dev...Sir

with one scanner it works very well when connect with arduino (0,1) pins to second one it starts showing garbage values continously as i open serial monitor...

and one more thing when i comment out…scanner1.begin(9600);

#include <NeoSWSerial.h>
NeoSWSerial scanner1( 2, 3 );
#include <AltSoftSerial.h>
AltSoftSerial scanner2;  // pins 8 & 9

void setup()
{
  Serial.begin( 9600 );
  scanner2.begin( 9600 );
  //scanner1.begin( 9600 );
     
}

void loop()
{
  if (scanner1.available())  // actually Serial, but this is easier to read
    Serial.write( scanner1.read() ); // echo to Serial monitor window

  if (scanner2.available())
    Serial.write( scanner2.read() ); // echo to Serial monitor window
}

means at a time i can get data from only one scanner either using AltSoftSerial.h or NeoSWSerial.h
firstly i want to try with software serial libraries…then last option would be hardware serial…