Unable to communicate with two esp32-wroom-32 controller using modbus protocal via rs485converter while sending serial data

Iam trying to write data from one esp32-wroom-32 controller to other esp32-wroom-32controller using modbus protocal via rs485 ttl converter ,but iam facing error like unable to make communication, getting 226 error message i tried with pooling method also same issue iam facing
please help me with this ,iam little bit new to this protocal sorry if i make any mistake in this writing message
if anyone have some example codes to test please reply or send me i dont know y my code is not working please help me with this

i have used uart0 pins: gpio-3(RX) and gpio-1(TX) or
uart 2 pins: gpio-16(RX) and gpio 17(TX), and DE and RE pins connected to gpio 4.
The board iam selected esp32-dev module and also tried with esp32-wrrom dev module

issues:-iam unable to write data its getting error(226) fail to write data in receive also same message read fail 226 error
Thank you in advance

***This is my Slave code:***

#include <ModbusMaster.h>

// Instantiate ModbusMaster object
ModbusMaster node;

// Define the slave ID
const uint8_t SLAVE_ID = 1;

// Define GPIO pin for controlling DE and RE
const int DE_RE_PIN = 4;

void setup() {
  Serial.begin(9600, SERIAL_8O1);
 // Serial.begin(9600); // Set baud-rate to 9600 for debugging
 // Serial1.begin(9600, SERIAL_8E1); // Initialize Serial1 for Modbus communication
  node.begin(SLAVE_ID, Serial); // Begin Modbus communication with Serial1

  pinMode(DE_RE_PIN, OUTPUT); // Set DE/RE pin as output

  // Initially set DE/RE pin to LOW (Receive mode)
  digitalWrite(DE_RE_PIN, HIGH);
}

void loop() {
  //uint16_t dataToSend = 12; // Data to be sent

  // Enable transmission by setting DE/RE pin to HIGH
 // digitalWrite(DE_RE_PIN, HIGH);

  // Write the data to a single holding register at address 10
  uint8_t result = node.writeSingleRegister(0x0000, 2);

  // Disable transmission by setting DE/RE pin to LOW
 // digitalWrite(DE_RE_PIN, LOW);

  // Check if the write was successful
  if (result == node.ku8MBSuccess) {
    Serial.println("Data sent successfully");  
  } else {
    Serial.print("Failed to send data. Error code: ");
    Serial.println(result);
  }

  delay(1000);
}
***This is my master code:***

#include <ModbusMaster.h>

// Define the slave ID
const uint8_t SLAVE_ID = 1;

// Define the GPIO pin for DE/RE (using pin 4)
const int DE_RE_PIN = 4;

// Instantiate ModbusMaster object
ModbusMaster node;
/*
void preTransmission() {
  digitalWrite(DE_RE_PIN, HIGH); // Enable transmission mode
}

void postTransmission() {
  digitalWrite(DE_RE_PIN, LOW); // Enable receive mode
}
*/
void setup() {
  // Start serial communication
  Serial.begin(9600);

  // Initialize Modbus communication
  Serial1.begin(9600, SERIAL_8E1);
  node.begin(SLAVE_ID, Serial1);

  // Set up callbacks to handle transmission
 // node.preTransmission(preTransmission);
  //node.postTransmission(postTransmission);
    pinMode(DE_RE_PIN, OUTPUT); // Set DE/RE pin as output
digitalWrite(DE_RE_PIN, LOW);
}

void loop() {
  // Read data from holding register
  uint8_t result = node.readHoldingRegisters(0x8A, 1);

  // Check if the read was successful
  if (result == node.ku8MBSuccess) {
    // Print received data
    Serial.print("Received data: ");
    Serial.println(node.getResponseBuffer(0));
  } else {
    // Print error if read failed
    Serial.print("Failed to read data. Error code: ");
    Serial.println(result,HEX);
  }

  delay(1000);
}

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