-3

Good afternoon. The SoftwareSerial library allows you to connect two devices and work with them in turn using listen. Each device on its pins transmits data correctly, but when I try to receive them in turn, the data is not received

Here is an example code:

#include <SoftwareSerial.h>
SoftwareSerial portOne(4, 7);
uint32_t byte_1 = 0;
uint32_t read_1[2] = {};

SoftwareSerial portTwo(2, 8);
uint32_t byte_2 = 0;
uint32_t read_2[2] = {};

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

void loop() {
one();
delay(100);
two();
delay(100);
}

void one(){
portOne.listen();
portOne.write(0x4a);
while (portOne.available() > 0) {
if (portOne.available()) { read_1[0] = (portOne.read()); }
if (portOne.available()) { read_1[1] = (portOne.read()); }
}

Serial.print("read_1[0]: "); Serial.println(read_1[0]);
Serial.print("read_1[1]: "); Serial.println(read_1[1]);
}

void two(){
portTwo.listen();
portTwo.write(0x4c);
while (portTwo.available() > 0) {
if (portTwo.available()) {read_2[0] = (portTwo.read());}
if (portTwo.available()) {read_2[1] = (portTwo.read());}
}

Serial.print("read_2[0]: "); Serial.println(read_2[0]);
Serial.print("read_2[1]: "); Serial.println(read_2[1]);;
}
itehno
  • 1
  • 1
  • `listen` clears the receive buffer so everything received in that 100 milliseconds delay – Juraj Jul 19 '23 at 08:13
  • 2
    You have read the [limitation](https://docs.arduino.cc/learn/built-in-libraries/software-serial) for SofwareSerial? - It cannot transmit and receive data at the same time. - If using multiple software serial ports, only one can receive data at a time. – Peter Plesník Jul 19 '23 at 08:39
  • @PeterPlesník they use `listen()` to switch. just they use it wrong – Juraj Jul 21 '23 at 04:39
  • @Juraj I understood now – Peter Plesník Jul 21 '23 at 06:59

1 Answers1

0

As Juraj say SoftwareSerial can be used in that way, but in this code is used wrong. Let see what is wrong. Check comments in source:

#include <SoftwareSerial.h>
SoftwareSerial portOne(4, 7);
uint32_t byte_1 = 0;
uint32_t read_1[2] = {};

SoftwareSerial portTwo(2, 8);
uint32_t byte_2 = 0;
uint32_t read_2[2] = {};

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

void loop() {
one();
delay(100);
two();
delay(100);
}

void one(){
portOne.listen(); //OK disable portTwo and enable listening on portOne
portOne.write(0x4a);  //OK send some data 
while (portOne.available() > 0) { //wrong you can't wait received data in this way
                                  //condition is immediately evaluated as false
                                  // because for data arrive need wait at least 1ms  
if (portOne.available()) { read_1[0] = (portOne.read()); }
if (portOne.available()) { read_1[1] = (portOne.read()); }
}

Serial.print("read_1[0]: "); Serial.println(read_1[0]); //print some random data
Serial.print("read_1[1]: "); Serial.println(read_1[1]);
}

void two(){
portTwo.listen();
portTwo.write(0x4c);
while (portTwo.available() > 0) { //Wrong - same as previously
if (portTwo.available()) {read_2[0] = (portTwo.read());}
if (portTwo.available()) {read_2[1] = (portTwo.read());}
}

Serial.print("read_2[0]: "); Serial.println(read_2[0]);
Serial.print("read_2[1]: "); Serial.println(read_2[1]);;
}

How it repair? Check comments again.

#include <SoftwareSerial.h>
SoftwareSerial portOne(4, 7);
uint32_t byte_1 = 0;
uint32_t read_1[2] = {};

SoftwareSerial portTwo(2, 8);
uint32_t byte_2 = 0;
uint32_t read_2[2] = {};

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

void loop() {
one();
two();
}

void one(){
  portOne.listen();
  portOne.write(0x4a);
  delay(100);  //wait 100ms, data are received to buffer in background process using interrupts

  // now it's possible that buffer contain some data
  if (portOne.available() > 0) {
    read_1[0] = (portOne.read()); 
    Serial.print("read_1[0]: "); Serial.println(read_1[0]);
    if (portOne.available()) {
      read_1[1] = (portOne.read()); 
      Serial.print("read_1[1]: "); Serial.println(read_1[1]);
    }
  }

}

void two(){
  portTwo.listen();
  portTwo.write(0x4c);
  delay(100);  //wait 100ms, data are received to buffer in background process using interrupts

  // now it's possible that buffer contain some data
  if (portTwo.available() > 0) {
    read_2[0] = (portTwo.read());
    Serial.print("read_2[0]: "); Serial.println(read_2[0]);
    if (portTwo.available()) {
      read_2[1] = (portTwo.read());
      Serial.print("read_2[1]: "); Serial.println(read_2[1]);
    }
  }
}

Peter Plesník
  • 524
  • 1
  • 2
  • 7