Modbus

Master

#define MASTER_EN   8               // connected to RS485 Enable pin
#define SWITCH      3               // Declare Button pin


void setup() {
  pinMode(SWITCH , INPUT_PULLUP);   // Declare LED pin as output
  pinMode(MASTER_EN , OUTPUT);      // Declare Enable pin as output
  Serial.begin(9600);               // set serial communication baudrate 
  digitalWrite(MASTER_EN , LOW);    //Enable pin low, Receiving mode ON
}

void loop() {
  if(digitalRead(SWITCH) == 0)
  {
    delay(1000);                      // Debouncing for switch
    digitalWrite(MASTER_EN , HIGH);   // Make Enable pin high to send Data
    delay(5);                         // required minimum delay of 5ms
    Serial.print("EMBEDDED GATE*");   // Send String serially, End String with *
    Serial.flush();                   // wait for transmission of data
    digitalWrite(MASTER_EN , LOW);    // Receiving mode ON
  }
}

Slave

#define SLAVE_EN  8
#define LED       13                // Declare LED pin
String recInput;                    // Variable to store Receive string 

void setup() {
  pinMode(LED , OUTPUT);            // Declare LED pin as output
  pinMode(SLAVE_EN , OUTPUT);       // Declare Enable pin as output
  Serial.begin(9600);               // set serial communication baudrate 
  digitalWrite(SLAVE_EN , LOW);    // Enable pin low, Receiving mode ON 
}

void loop() {
  while(Serial.available())       // If serial data is available then enter into while loop
  {
    recInput = Serial.readStringUntil('*');// Receive Serial data in Variable
    Serial.print(recInput);       // Print Data
    
    if(recInput == "EMBEDDED GATE")// Compare Data
    {
      digitalWrite( LED , HIGH);   // LED ON
      delay(1000);
      digitalWrite( LED , LOW);    // LED OFF
    }
  }
}