Unable to write to the modbus slave register

We are using Modbus implementation and we got the library on Arduino IDE for Reading the slave register but we are unable to write to slave register using that library.

Meanwhile we are also trying and written one code which reads the data successfully, write operation does not generate any error but it doesn’t write into slave register.

i am attaching the library link that we are using currently and the modified code.

Master - ESP32
Slave - Energy meter

Modified Code

#include <ModbusRTU.h>
ModbusRTU mb;

//#include <SoftwareSerial.h>
int DE_RE = 4; //D4  For MAX485 chip
#define RXPIN GPIO_NUM_16
#define TXPIN GPIO_NUM_17
#include "IntToFloat.h"

//SoftwareSerial S(RX, TX);//D6/D7  (RX , TX)

uint16_t Mread0[2];

bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) {
  Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap());
  return true;
}

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N2, RXPIN, TXPIN);
  mb.begin(& Serial2, DE_RE); //Assing Software serial port to Modbus instance for MAX485 chip having DI,DE,RE,RO Pin at TTL side
  mb.master(); //Assing Modbus function as master
  Serial.println(); //Print empty line
  Serial.println(sizeof(Mread0)); //Reaing size of first array
}
void loop() {
  if (!mb.slave()) {
    mb.readHreg(12, 0x0020, Mread0, 4 , cbWrite);  //(SlaevID,Address,Buffer,Range of data,Modus call)
    Serial.println(InttoFloat(Mread0[0], Mread0[1]));
  }
  mb.task();
  delay(1000);
  yield();
}

Header file

float InttoFloat(uint16_t Data0,uint16_t Data1) {
  float x;
  unsigned long *p;
  p = (unsigned long*)&x;
  *p = (unsigned long)Data0 << 16 | Data1; //Big-endian
  return(x);
}

I moved your topic to an appropriate forum category @manjunath_s.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.