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]);;
}
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:
How it repair? Check comments again.