Understanding NeoSWSerial for multiple Serial instances

Hello,

I want to use the library NeoSWSerial to create two serial instances on pin (8,9) and (10,11) but when i try, i only receive on one of them and not both.

Do you know if it’s possible to create multiple instances on NeoSWSerial or not ? And is there a specific method to use like listen or end ?

PS: Sorry for my english since it’s not my native language

#include <NeoSWSerial.h>
#define baudrate 9600

NeoSWSerial nord(10,11);
NeoSWSerial sud(8,9);

int envoi=0;

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

void loop() {
  if(envoi==0)
  { 
    Serial.println("nord");
    nord.begin(baudrate);
    nord.print("nord");
    nord.end();
    envoi=1;
  }
  else if(envoi==1)
  {
    Serial.println("sud");
    sud.begin(baudrate);
    sud.print("sud");
    sud.end();
    envoi=0;
  }
}

this, above, is my “sending” code and this is to receive :

#include <NeoSWSerial.h>
#define baudrate 9600

NeoSWSerial nord(10,11);

static void nord_int( uint8_t c ) {Serial.println("n");}

void setup() {
  Serial.begin(9600);
  nord.attachInterrupt( nord_int );
  nord.begin(baudrate);
}

void loop() {
  
}

Thank you

Do you know if it’s possible to create multiple instances on NeoSWSerial or not ?

Yes, but you can only listen to one at a time. No data will be received or stored from the other instance.

And is there a specific method to use like listen or end ?

Yes, either nord.listen() or sud.listen().

However, it would be better to use Serial for one of the two instances. You would also see the messages on your Serial Monitor.

And it would be better to use AltSoftSerial on pins 8 & 9. You should use this on the receiver, too.

Could you do this on the sender:

#include <AltSoftSerial.h>
#define BAUDRATE 9600

#define nord Serial
AltSoftSerial sud;  // always on 8 & 9 on an UNO

int envoi=0;

void setup() {
  nord.begin( BAUDRATE );  // same as Serial
  sud .begin( BAUDRATE );
}

void loop() {
  if(envoi==0)
  { 
    nord.print("nord");
    envoi=1;
  }
  else if(envoi==1)
  {
    sud.print("sud");
    envoi=0;
  }
}

You should not try to print from the attachInterrupt function. Could you try this on the receiver:

#include <AltSoftSerial.h>
#define baudrate 9600

AltSoftSerial nord; // always 8 & 9 on an UNO

void setup() {
 Serial.begin(9600);
 nord.begin(baudrate);
}

void loop() {
 if (nord.available()) {
   char c = nord.read();
   Serial.print( c );
 }
}

When you use Serial for one of the devices on the sender, you must disconnect pin 0 (RX) from the device to upload new sketches over USB.

Could you use Serial for one of the devices on the sender?

Thank you for your fast answer. In fact, for my project, I need 4 serial instances so i need to create at least 3 myself if I use the serial hardware as the forth one.
I will try and do as you say asap and tell you how it went!
Thank you very much again