RS 485 library NIck Gamman data receiving problem

Hi everyone,

I am trying to make a project where master and slave configuration is needed so, I am using RS 485 to accomplish this.

Currently I am using a RS 485 library from Nick Gammon website Gammon Forum : Electronics : Microprocessors : RS485 communications

The problem is that along with RS 485 code i am also using ultrasonic sensor code for a slot distance measuring at the salves end and with master i am using ethernet shield code. So, when I ask for data from slave through master the data from slaves get mix i.e slave1’s data is replaced with slave2’s data.
Below are the codes please help.

Slave code

#include <NewPing.h>
#include <RS485_protocol.h>
#include <SoftwareSerial.h>

#define TRIGGER_PIN  11  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     10  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

byte Dis; 
//=============
const byte ENABLE_PIN = 4;
const byte LED_PIN = 13;
boolean con1;
boolean con2;

SoftwareSerial rs485 (5, 3);  // receive pin, transmit pin

// callback routines
//=============  
void fWrite (const byte what)
  {
  rs485.write (what);  
  }
  
int fAvailable ()
  {
  return rs485.available ();  
  }

int fRead ()
  {
  return rs485.read ();  
  }
//===============

void setup() {
  Serial.begin(9600);
  rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
  pinMode (LED_PIN, OUTPUT);  // built-in LED
  con1=true;
  con2=true;
}

//==============================

void loop()
{
  int DistanceS = sonar.ping_cm();
  if (DistanceS < 50)
    {Distance();}
  else 
    {Distancei();}
//=====================
  byte buf [10];
  
  byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));

  if (received)
    {
    if (buf [0] != 1)
      {return;}  // not my device 
      
    byte msg [] = {
       0,  // device 0 (master)
       Dis,  // turn light on command received
    };
    
    delay (1);  // give the master a moment to prepare to receive
    digitalWrite (ENABLE_PIN, HIGH);  // enable sending
    sendMsg (fWrite, msg, sizeof msg);
    digitalWrite (ENABLE_PIN, LOW);  // disable sending
       }  // end if something received
}  // end of loop

//====================

void Distance()
{
  if (con1 == true)
   {for (int time = 0; time < 10; time++) { 
    delay(1000); 
    int Distance1 = sonar.ping_cm();
    if (Distance1 > 50)
     {return;}
    }
    con1 = false;
    con2 = true;
    Dis=1;
   }   
}
//=====================
void Distancei()
{
 if (con2 == true)
 { for (int time = 0; time < 10; time++) { 
    delay(1000); 
    int Distance2 = sonar.ping_cm();
    if (Distance2 < 50)
     {return;}
    }
  con2 = false;
  con1 = true;
  Dis = 0;
} 
}
//======================

Master Code

#include <Ethernet.h>
#include <RS485_protocol.h>
#include <SoftwareSerial.h>
#include <SPI.h>
//========================
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x00
};

EthernetClient client;

const byte numSlaves = 2;
String Status[numSlaves]= { "vacant","vacant"};

//=========================

SoftwareSerial rs485 (2, 3);  // receive pin, transmit pin
const byte ENABLE_PIN = 4;
const byte LED_PIN = 13;
void fWrite (const byte what)
  {
  rs485.write (what);  
  }
  
int fAvailable ()
  {
  return rs485.available ();  
  }

int fRead ()
  {
  return rs485.read ();  
  }
  
void setup()
{ 
  Serial.begin(9600);
  rs485.begin (28800);
  Ethernet.begin(mac);
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
}
//=======================

void loop()
{
  
 for (byte n = 0; n < numSlaves; n++) {
 byte buf [10]={0};
   int Slave = n+1;
Serial.println(Slave);
  // assemble message
  byte msg [] = { 
    Slave    // devices
  };

  // send to slave  
  digitalWrite (ENABLE_PIN, HIGH);  // enable sending
  sendMsg (fWrite, msg, sizeof msg);
  digitalWrite (ENABLE_PIN, LOW);  // disable sending
  delay(1);
  // receive response  
  
  byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
  digitalWrite (LED_PIN, received == 0);  // turn on LED if error    
  delay(5000);
  if (received)
  { if (buf[0] != 0){
     return;}
  if(buf[1] == 1)   
  {Status[n] = "occupied";}
  else
  { Status[n] = "vacant";}
 }
 Serial.println(Status[n]);
  delay(5000);
 }
 EthernetPost();
}


//===============

void EthernetPost()
{ 
  
  if( client.connect("192.168.1.3",81)) 
  { 
    client.print( "GET /Slaves1add.php?");
    client.print("st1=");
    client.print( Status[0] );
    client.print("&&");
    client.print("st2=");
    client.print( Status[1] );
    client.println( " HTTP/1.1");
    client.println( "Host: 192.168.1.3" );
    client.println( "Content-Type: application/x-www-form-urlencoded" );
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
    
    Serial.println("yes");
  }
   if (client.connected()) { 
		client.stop();	// DISCONNECT FROM THE SERVER
	} 
}

//=========================