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]);
}
}
}