Help implement RS-485 Modbus on Schneider PM800 on ESP32

Hi, I have the following problem and I've been looking for examples on the web for a long time but none seems to work for me.

I would like to know how to read the values ​​of the Schneider PM800 controller via RS-485 ModBus on my ESP32.

I have the following:

#include <ModbusMaster.h>
#include <HardwareSerial.h>

#define RS485_DE_RE_PIN 2
#define RS485_RX_PIN 16
#define RS485_TX_PIN 17

// Crear un objeto Modbus
ModbusMaster node;

void setup() {
  // Inicializar el puerto serie para la comunicación RS485
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);

  // Configurar el pin de habilitación (DE/RE) como salida
  pinMode(RS485_DE_RE_PIN, OUTPUT);

  // Iniciar la comunicación Modbus
  node.begin(1, Serial1); // Dirección del dispositivo y objeto Serial1

  // Iniciar en el modo de recepción
  digitalWrite(RS485_DE_RE_PIN, LOW);
}

void loop() {
  // Cambiar al modo de transmisión
  digitalWrite(RS485_DE_RE_PIN, HIGH);
  delayMicroseconds(500); // Esperar un tiempo para la estabilización del bus

  // Leer registros del controlador PM800
  uint8_t result = node.readInputRegisters(0x10, 2); // Dirección del registro y número de registros a leer

 // Cambiar al modo de recepción
  digitalWrite(RS485_DE_RE_PIN, LOW);
  delay(1000); // Esperar antes de la siguiente lectura

  // Verificar si la lectura fue exitosa
  if (result == node.ku8MBSuccess) {
    // Obtener los valores leídos
    uint16_t value1 = node.getResponseBuffer(0);
    uint16_t value2 = node.getResponseBuffer(1);
    
    // Hacer algo con los valores leídos
    // ...
    
    // Imprimir los valores en el monitor serie
    Serial.print("Valor 1: ");
    Serial.println(value1);
    Serial.print("Valor 2: ");
    Serial.println(value2);
  } else {
    // Hubo un error en la lectura
    Serial.print("Error de lectura: ");
    Serial.println(result);
  }

}

I need guidance with this, thank you very much.

Remove two of your entries.

https://forum.arduino.cc/t/help-implement-rs-485-modbus-on-schneider-pm800-on-esp32/1131847

I have deleted your other cross-posts @maatooh.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Ok, show us your wiring diagram of how you have connected your ESP32 to the RS485 module and the connections you have made between your RS485 module and the PM800.

Note that the RS485 module that you have uploaded images of probably has the MAX485 chip fitted and is for use with 5V systems and needs a 5V supply on the VCC pin. Whilst RE, DE & DI may work with 3.3V logic levels, the RO signal would need level shifting to avoid applying more than 3.3V to an ESP32 pin. A simple potential divider could do this.

OR, you could look for a RS485 board designed for 3.3V systems - it may have a MAX3485 chip or equivalent fitted.

It was just to use the direct serial port without HardwareSerial or Software serial and preferably use pin 3 and 1.
node.begin(1, Serial);

I work fine with 3.3v. Thank you so much.

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