Modbus RTU how to receive data from the slave

Hello guys, some time ago, I tried to work with modbus in a way that I understood

Finally I found a simple code that turns the slave’s leds on / off … everything works fine

I would like to know how to send data from the slave to the master

example: when the slave receives something, send the master 550.

the goal is to treat the value in the master to light up leds or something like that …

thank you!

Master::: - github.com/4-20ma/ModbusMaster
Slave:: - github.com/smarmengol/Modbus-Master-Slave-for-Arduino
Master:

#include <ModbusMaster.h>               //Library for using ModbusMaster

#define MAX485_DE      3
#define MAX485_RE_NEG  2

ModbusMaster node;                    //object node for class ModbusMaster

void preTransmission()            //Function for setting stste of Pins DE & RE of RS-485
{
  digitalWrite(MAX485_RE_NEG, 1);             
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(115200);             //Baud Rate as 115200
  Serial.setTimeout(50);
  node.begin(1, Serial);            //Slave ID as 1
  node.preTransmission(preTransmission);         //Callback for configuring RS-485 Transreceiver correctly
  node.postTransmission(postTransmission);
}

void loop()
{
  node.writeSingleRegister(0x40000,1);        //Writes value to 0x40000 holding register
  node.writeSingleRegister(0x40001,1);
  node.writeSingleRegister(0x40000,0);        //Writes value to 0x40000 holding register
  node.writeSingleRegister(0x40001,0);
}

Slave:

#include<ModbusRtu.h>       //Library for using Modbus in Arduino

#define led1 A0              //Define as led1 
#define led2 A1              //Define as led2

Modbus bus;                          //Define Object bus for class modbus 
uint16_t modbus_array[] = {0,0,0};    //Array initilized with three 0 values
                      
void setup()
{
  Serial.begin(9600);
  pinMode(led1,OUTPUT);           //Led1 set as OUTPUT
  pinMode(led2,OUTPUT);           //Led2 set as OUTPUT
  bus = Modbus(1,1,2);            //Modbus slave ID as 1 and 1 connected via RS-485 and 4 connected to DE & RE pin of RS-485 Module 
  bus.begin(115200);               //Modbus slave baudrate at 115200
}

void loop()

{
   bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0]));       //Used to receive or write value from Master 
  
  if (modbus_array[0] == 0)    // Address 0
  {
    digitalWrite(led1,LOW);    //LED OFF if 0
  }
  else
  {
     digitalWrite(led1,HIGH);  //LED ON if value other than 0      
  } 

 if (modbus_array[1] == 0)    // Address 1 
  {
    digitalWrite(led2,LOW);   //LED OFF if 0
  }
  else
  {
     digitalWrite(led2,HIGH);  //LED ON if value other than 0
  }           
  delay(200); 
}

I think you can use Read Input Registers (FC 04). Or you can refer to a project on Arduino Project Hub

In modbus, you can't send data from the slave to the master in the way that I think you mean as Modbus RTU is a command - response type of system.

As a slave device, you can prepare your data but the master has to request it from you.

markd833:
In modbus, you can't send data from the slave to the master in the way that I think you mean as Modbus RTU is a command - response type of system.

As a slave device, you can prepare your data but the master has to request it from you.

How can you implement this using my code example?

IoT_hobbyist:
I think you can use Read Input Registers (FC 04). Or you can refer to a project on Arduino Project Hub

Is it possible to implement check registers using the same library as above?

This example may help you out.

Oh, and I just found this example that sets up an Arduino as a Modbus slave with an LED and a switch. The master device can control the state of the LED and also read the state of the switch.

markd833:
This example may help you out.

markd833:
Oh, and I just found this example that sets up an Arduino as a Modbus slave with an LED and a switch. The master device can control the state of the LED and also read the state of the switch.

Thank you very much for your help @markd833, it looks like there was a problem with the library, your identified examples worked well :smiley:

Hello I am new and I do not understand the operation of the void loop in the example how I make the master receive the data and show it through the serial port ?