0

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?

Example connection image

Incoming Datas

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);
}
  • SoftwareSerial often doesn't cope with high baud rates. Especially if you are trying to run more than one connection. Try using a hardware serial port or a much lower baud rate with software serial. – Ben T May 31 '20 at 23:07
  • How about trying a single software serial connection at a low baud rate to break the problem down and help isolate the issue? – Ben T Jun 01 '20 at 06:19
  • When I use single serial software and low baud rate it looks like work better. But I need 3 software serial what can I do about it? – Oğuz Kağan Jun 01 '20 at 06:33
  • By the wayI accidentally deleted my comment to the your first comment. It was ' **When I tried lower baud rate with software serial, ıt is getting worse. And I cannot use hardware serial port. Because I need 3 serial port but I have just 1 hardware serial.** ' – Oğuz Kağan Jun 01 '20 at 06:43
  • I would temporarily forget the '3 software serial' for the time being and figure out what your hardware is capable of first. Other than using a faster board, you can try finding the minimal baud rate that 1, 2 and 3 software serial ports works at for your specific app. Alternatively consider going half duplex for part of the setup. – Ben T Jun 01 '20 at 06:47
  • I don' t think it is about baud rate because I just tried 38400 baud rate with single software it actually worked better than 9600. But 57600 didn't work. – Oğuz Kağan Jun 01 '20 at 07:01
  • Ok - the SoftwareSerial docs clearly state only one can be active at any time. So it is possible the buffers for other SoftwareSerial connections isn't initialized. You'll need to use `listen()` to switch between them. – Ben T Jun 01 '20 at 07:36

0 Answers0