Send values back to Slaves from the Master

Hello,

I am using Arduino Mega 2560’s with RS485’s for communication with Software Serial library. I have one master communicating with two slaves. The master sends request to slaves repeatedly and receives values from all slaves. Finally, I want the master to send back the received values to all the slaves in the network. How can I achieve this?

Since I will be using two digital write’s for sending in Master and two digital writes for receiving in slave’s. As I don’t want both digital writes to send/receive interchangeable data’s.

Master code:

void setup() {
  // put your setup code here, to run once:
  for(uint8_t i=1;i<3;i++)
    a[i][0] = i;
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT);
Serial.begin(9600);
}


void loop() {
    char req[2];

    req[0] = 'R';
    req[1] = n + '0';
    digitalWrite (ENABLE_PIN, HIGH);
    rs485.write(req);
    Serial.println("requesting from slave ");
    Serial.println(n);
    
    digitalWrite (ENABLE_PIN, LOW);
    delay(1000);
    
  while( rs485.available())
  {
    digitalWrite (ENABLE_PIN, LOW);
 while((ch = rs485.read()) != 'E'){

uint8_t b[4];
 b[0] = ch;
 b[1] = rs485.read();
 b[2] = rs485.read();
 b[3] = rs485.read();

long  bignum = *((long *)b);

    Serial.println("Value received:");
    Serial.println(bignum);
    delay(1000);

    data_s[n] = bignum;
  }
    if(ch == 'E'){
    Serial.println("Value received from slave:");
    Serial.println(n);
      n++;
      if(n>2) n=1;
     delay(1000);
     ch = 0;
    } 
  }
  
}

Slave 1:

void setup()
{

randomSeed(analogRead(0));
Serial.begin(9600);
rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);
}

void loop()
{
   while(rs485.available())
  {
    char ch = rs485.read();
    if(ch == 'R')
    {
      //we are recieiving request
      if(rs485.read() != '1')
        return;       
      Serial.println("Request received");
      requested = true;
    }
    else
    {
      
    }
  }
  if(requested)
  {

      digitalWrite (ENABLE_PIN, HIGH);  
      
      long n = 837426840;
      uint8_t buf[4];
for(int i=0; i<2; i++){
  buf[3] = (uint8_t)((n >> 24) & 0xFF);
  buf[2] = (uint8_t)((n >> 16) & 0xFF);
  buf[1] = (uint8_t)((n >> 8) & 0xFF);
  buf[0] = (uint8_t)(n & 0xFF);

   Serial.println(n); 
  rs485.write(buf, sizeof(buf)); //send to master
  n = n+100000000;

  delay(100);
}
      rs485.write('E');
      digitalWrite (ENABLE_PIN, LOW); 
      requested = false;
  }
delay(1000);
}

Thank you!

You need to post the complete programs. And please use the AutoFormat tool to lay out the code so it can be read more easily.

...R

I am re-posting my code here after auto-formatting

Master:

#include "RS485_protocol.h"
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 2;
//const byte ENABLE_PIN1 = 3;
SoftwareSerial rs485 (10, 11);

long data_s[2];

uint8_t x = 0;
uint8_t y = 0;
uint8_t a[3][2] = {0};
boolean stopp = false;
int n = 1;
uint8_t ch;
void setup() {
  // put your setup code here, to run once:
  for (uint8_t i = 1; i < 3; i++)
    a[i][0] = i;
  rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);
  Serial.begin(9600);
}


void loop() {
  char req[2];

  req[0] = 'R';
  req[1] = n + '0';
  digitalWrite (ENABLE_PIN, HIGH);
  rs485.write(req);
  Serial.println("requesting from slave ");
  Serial.println(n);

  digitalWrite (ENABLE_PIN, LOW);
  delay(1000);

  while ( rs485.available())
  {
    digitalWrite (ENABLE_PIN, LOW);
    while ((ch = rs485.read()) != 'E') {

      uint8_t b[4];
      b[0] = ch;
      b[1] = rs485.read();
      b[2] = rs485.read();
      b[3] = rs485.read();

      long  bignum = *((long *)b);

      Serial.println("Value received:");
      Serial.println(bignum);
      delay(1000);

      data_s[n] = bignum;
    }
    if (ch == 'E') {
      Serial.println("Value received from slave:");
      Serial.println(n);
      n++;
      if (n > 2) n = 1;
      delay(1000);
      ch = 0;
    }
  }

}

Slave_1:

#include "RS485_protocol.h"
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 2;
SoftwareSerial rs485 (10, 11);

//uint8_t  y=0;
uint8_t  x = 1;
uint8_t  slave;
uint8_t val;
boolean replay = true;
boolean requested = false;
boolean sendNow = false;
int id = 1;
void setup()
{

  randomSeed(analogRead(0));
  Serial.begin(9600);
  rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);
}

void loop()
{
  while (rs485.available())
  {
    char ch = rs485.read();
    if (ch == 'R')
    {
      //we are recieiving request
      if (rs485.read() != '1')
        return;
      Serial.println("Request received");
      requested = true;
    }
    else
    {

    }
  }
  if (requested)
  {

    digitalWrite (ENABLE_PIN, HIGH);

    long n = 837426840;
    uint8_t buf[4];
    for (int i = 0; i < 2; i++) {
      buf[3] = (uint8_t)((n >> 24) & 0xFF);
      buf[2] = (uint8_t)((n >> 16) & 0xFF);
      buf[1] = (uint8_t)((n >> 8) & 0xFF);
      buf[0] = (uint8_t)(n & 0xFF);
      Serial.println(n);
      rs485.write(buf, sizeof(buf));
      n = n + 100000000;
      delay(100);
    }
    rs485.write('E');
    digitalWrite (ENABLE_PIN, LOW);
    requested = false;
  }
  delay(1000);
}

Slave_2:

#include "RS485_protocol.h"
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 2;
SoftwareSerial rs485 (10, 11);

//uint8_t  y=0;
uint8_t  x = 1;
uint8_t  slave;
uint8_t val;
boolean replay = true;
boolean requested = false;
boolean sendNow = false;
int id = 2;
void setup()
{

  randomSeed(analogRead(0));
  Serial.begin(9600);
  rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);
}

void loop()
{
  while (rs485.available())
  {
    char ch = rs485.read();
    if (ch == 'R')
    {
      //we are recieiving request
      if (rs485.read() != '1')
        return;
      Serial.println("Request received");
      requested = true;
    }
    else
    {

    }
  }
  if (requested)
  {

    digitalWrite (ENABLE_PIN, HIGH);

    long n = 547536723;
    uint8_t buf[4];
    for (int i = 0; i < 2; i++) {
      buf[3] = (uint8_t)((n >> 24) & 0xFF);
      buf[2] = (uint8_t)((n >> 16) & 0xFF);
      buf[1] = (uint8_t)((n >> 8) & 0xFF);
      buf[0] = (uint8_t)(n & 0xFF);
      Serial.println(n);
      rs485.write(buf, sizeof(buf));
      n = n + 100000000;
      delay(100);
    }
    rs485.write('E');
    digitalWrite (ENABLE_PIN, LOW);
    requested = false;
  }
  delay(1000);
}

saifshk17: I am re-posting my code here after auto-formatting

Thank you. I wish others would follow your example.

I think you will find it much easier to solve the problem if you make a backup copy of your programs and then make the code as simple as possible. For example the master sends a '1' or a '2' and the appropriate slave sends back 'A' or 'B'

Then, I think that your message from the master has to contain a second character to indicate whether it is making a request or just sending data for information. So maybe the master would send "1R" or "2D" and the lave would look at the first character to determine if the message is addressed to it. And look at the second character to see if a reply is expected.

If, when sending data, you want to be able to send 1 message that is read by both slaves then maybe use '0' as the address - in which case you may not need the second character to identify a request or data.

When it all works with very simple data then expand the project to deal with the real data.

Note, also, that I am assuming you use rs485.print() rather then .write() as it is much easier to debug when you send human readable data.

You should probably also look at the examples in Serial Input Basics - simple reliable ways to receive data. I don't think your system is sufficiently robust. You are using

while ( rs485.available())

but it is more than likely that the entire message will not be ready as the Arduino is very much faster than serial data.

...R