I am trying to communicate a lot of arduinos with software serial. There will be one master arduino it will have 1 software serial port. And there will be lots of slave arduino and they will have 3 software serial ports. Master arduino will connect to one of the slaves. And slaves will be connected to each other. My Problem is when I send something master to slaves or slaves to master. Sometimes it is getting something what I didn't send.
I am sending this data: #1|||232|2332322|233233*
I am sometimes getting something like this: ⸮⸮H⸮f⸮|2⸮ƚ⸮&f~2͒⸮⸮H
How can I fix it?
Slave Code:
#include <SoftwareSerial.h>
#define fSRxPin 2
#define fSTxPin 3
#define sSRxPin 4
#define sSTxPin 5
#define tSRxPin 6
#define tSTxPin 7
SoftwareSerial firstSide = SoftwareSerial(fSRxPin, fSTxPin); // RX, TX
SoftwareSerial secondSide = SoftwareSerial(sSRxPin, sSTxPin);
SoftwareSerial thirdSide = SoftwareSerial(tSRxPin, tSTxPin);
char comingCharacter;
String comingData;
void setup()
{
pinMode(13, OUTPUT);
firstSide.begin(38400);
secondSide.begin(38400);
thirdSide.begin(38400);
Serial.begin(9600);
Serial.println("");
Serial.flush();
}
void loop()
{
firstSide.listen();
if (firstSide.available() > 0)
{
comingData = "";
while (firstSide.available() > 0)
{
comingCharacter = firstSide.read();
comingData += comingCharacter;
}
Serial.println("");
secondSide.println("");
Serial.println(comingData);
secondSide.print(comingData);
Serial.flush();
secondSide.flush();
}
delay(100);
}
Master Code:
#include <SoftwareSerial.h>
SoftwareSerial firstSide(2, 3); //RX,TX
SoftwareSerial secondSide(4, 5);
void startSeedCollecting()
{
firstSide.println();
}
void setup()
{
firstSide.begin(38400); // 57600 Max speed for arduino uno
Serial.begin(9600);
//delay(2000);
}
void loop()
{
firstSide.println("#1|||232|2332322|233233*");
firstSide.flush();
firstSide.listen();
if (firstSide.available() > 0)
{
String comingData = "";
while (firstSide.available() > 0)
{
char comingCharacter = firstSide.read();
comingData += comingCharacter;
}
Serial.println("");
secondSide.println("");
Serial.println(comingData);
Serial.flush();
}
delay(100);
}