SoftwareSerial.h Communication with Multiple out puts

Hi all,
I have 2 Arduinos. 1 is Master and 2 is Slave.
Master Arduino sending first signal and after a delay sends the second signal. But Slave signal only directs the second signal. ( even if I used flush to clear the previous signal ) .

but if I send signal individually it works.
Master -> signal 1 -> Slave = Working
Master-> signal 2 -> Slave = working

Master -> Signal 1 , delay (1000) ,signal 2 -> not working. Can some one help me on this. thanks

Master Code -

#include <SoftwareSerial.h>
SoftwareSerial YRCOM(5, 4);
SoftwareSerial softSerial(9, 8);

void setup()  
{
  softSerial.begin(9600);
  YRCOM.begin(9600);
  Serial.begin(9600);
  
} 
void loop()  
{ 

   softSerial.print(1245);
softSerial.flush();
    delay(1000);
 
YRCOM.print(1000);
YRCOM.flush();
 delay(1000);   
}

Slave Code -

#include <SoftwareSerial.h>
SoftwareSerial YRCOM(51, 50);
SoftwareSerial softSerial(11, 10);

char myData[20];
float number;


char myData1[20];
float number1;

int LED = 2;

void setup()
{
  YRCOM.begin(9600);
  Serial.begin(9600);
}
void loop()
{
  if (YRCOM.read())
  {
      byte m = YRCOM.readBytesUntil('\n', myData1, 20);
      myData1[m] = '\0';  //insert null-byte
      Serial.println(number1);  
      number1 = atof(myData1);
       Serial.println("YROCM"); 
      Serial.println(number1, 3);    
      delay(1000);

  }
  if (softSerial.read())
  {
      byte m = softSerial.readBytesUntil('\n', myData, 20);
      myData[m] = '\0';  //insert null-byte
      Serial.println(number);  //shows: 251313.212

      number = atof(myData);
      Serial.println("Serial"); 
      Serial.println(number, 3);    //shows: 251313.212 accurately? No! Why not? 

      delay(1000);
  }
}

And which of the many different types of Arduino might yours be ?

And study the reference for the detail on using multiple instances of software serial on one Arduino;

https://docs.arduino.cc/learn/built-in-libraries/software-serial

None of those examples explain why is this not relieving signals. I have tried them all. it doesnt work with this. its not the hardware. its just code which I cannot figure out.

The 'Two port receive' example explains the use of the .listen() command thats needed to swap between software serial instances.

Did you change your code to include these required commands ?

Be careful, read() does not return 0 if the buffer is empty.

I used, available(), listen(), then flush. none worked.

readBytesUntil() with a length of 20, using a 20-byte array, leaves no room for the terminating null if it actually reads 20 bytes.

if ( .read() ) will be true when the buffer is empty, and will consume one of the received characters if the buffer has data. available() should be used.

Get rid of the delay(), that will throw the timing terribly.

With multiple SoftwareSerial instances, only one can be used for receive at a time. listen() is used to specify which instance is actively looking for receive data.

After you receive data on one instance of SoftwareSerial, immediately switch to listening on the other, then eliminate the delay totally. Let the loop execute continuously, looking for data available on the ports until something is received.

In the sender code, print() does not transmit a newline, so you are having to wait for readBytesUntil() to time out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.