ESP32 RS485driver hardwareserial ModbusMaster

migbur
Posts: 12
Joined: Mon Mar 12, 2018 1:57 pm

Re: ESP32 RS485driver hardwareserial ModbusMaster

Postby migbur » Wed May 15, 2019 4:41 pm

Thank you very much
I will tried up as soon as possible I have one humidity sensor with RS485 and im not able to run it up

chickey
Posts: 1
Joined: Sun Oct 20, 2019 1:02 pm

Re: ESP32 RS485driver hardwareserial ModbusMaster

Postby chickey » Sun Oct 20, 2019 1:03 pm

hi would you be able to share your code, an old one i know but encountering the same issue
grasvezel wrote:
Thu Apr 12, 2018 3:36 pm
I know it has been a few weeks, but I ran into similar problems, solved them and would like to share. I use a MAX-3485. Basically a 485 but for 3.3v instead of 5v. I couldn't get anyting to work using standard libraries. I used one of the many hardware examples in wich the DE and /RE are connected. DriverEnable is active high, ReceiverEnable is active low. So, why not use one GPIO and use that as a toggle between send and receive, right? After hours of debugging and hooking up a second ESP32/MAX3485 to see what was going on on the RS485 bus, I found that the DE dropped before the transmission of the Modbus frame was complete. My guess is that the library just goes ahead and drops the DE pin after the frame is sent, but at that time the ESP32 still has some data in the transmit buffer. Maybe that is a difference with arduinos, maybe those don't have a transmit buffer?

Anyway: I solved it by modifying my hardware a bit (RE is always low now) and implementing the Modbus protocol myself instead of using a lib. The protocol is quite straightforward. The only thing I had to deal with is reading back my own transmission because the receiver is always on now. But since I know exactly what I sent out, that's not difficult to do.

Edje11
Posts: 18
Joined: Thu May 17, 2018 10:33 am
Contact:

Re: ESP32 RS485driver hardwareserial ModbusMaster

Postby Edje11 » Thu Oct 24, 2019 7:48 pm

Modbusmaster works fine with RS485 on a Esp32.
Have a look for the tip and tricks: http://evertdekker.com/?p=1307
You have to set the fucntions preTransmission() and postTransmission() correct, see the above sample

girasera
Posts: 3
Joined: Tue Jun 30, 2020 10:41 pm

Re: ESP32 RS485driver hardwareserial ModbusMaster

Postby girasera » Fri Jul 03, 2020 10:50 pm

Hi everyone,

I'm trying to do ESP32 communicate with a Freq. Inverter using MAX485 converter. I'm using the library ModbusMaster with Arduino IDE.

I tested a Arduino UNO instead the ESP32 and it worked fine.

With the ESP32 I can only write to the Inverter, but not read.

Did someone foundout the solution to use this library correctly whit ESP32?

This is my code:

Code: Select all

#include <ModbusMaster.h>

#define MAX485_RE_NEG  19

ModbusMaster node;
 

uint8_t deviceID = 1;
uint8_t resultMain;
String c, texto;
String ID, REGISTER, VALUE;
 
void preTransmission() {
  digitalWrite(MAX485_RE_NEG, HIGH);
}
 
void postTransmission() {
  digitalWrite(MAX485_RE_NEG, LOW);
}
 
void setup() {

  Serial.begin(9600);  // to write modbus
  Serial2.begin(4800, SERIAL_8N1, 0, 14); // pino 0 = RX, 14 = TX
 
  pinMode(MAX485_RE_NEG, OUTPUT);
  digitalWrite(MAX485_RE_NEG, LOW);

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

}
void loop() {
  while (Serial.available() > 0) {
 
    // Slave ID: ID 0-2 > ID1
    // Slave Register: 3-6 > 0005
    // Slave Register Value: 7-11 > 1000
 
    c = (char)Serial.read();  // read from serial monitor
 
    if (c == ":") {
      break;
    }
    else {
      texto += c; // texto = texto + c;
    }
    ID = texto.substring(2, 3);            // pega o número de ID enviado pela serial
    REGISTER = texto.substring(3, 7);      // pega o endereço da varíavel que recebera a escrita ex: No inv delta ms300 a control word é 2000H
    VALUE = texto.substring(7, 11);        // pega o valor q será escrito na variável Ex: o valor 0002H coloca o inv em RUN
    deviceID = 0;
          
  }

  switch (deviceID) {
 
    // Device ID imaginário - Serve apenas para enviar comandos ao Inversor via RS485
    case 0:
      node.begin(getInt(ID), Serial2);
      resultMain = node.writeSingleRegister(getInt(REGISTER), getInt(VALUE)); // para o inv delta a controlword é 2000H = 8192 decimal   value = 0002 run, = 0001 stop 
      
     // node.begin(1, mySerial);
     // resultMain = node.writeSingleRegister(0x2000, 2);
      
      deviceID = 1;
      texto = "";
      ID = "";
      REGISTER = "";
      VALUE = "";
      break;
 
    // Rotina que realiza a leitura dos dados pertinenetes ao inversor de frequencia
    case 1:
      // Modbus ID 1 - Inversor de Frequencia
           
      node.begin(deviceID, Serial2);
        
      node.idle(yield);
      delay(200);
      resultMain = node.readHoldingRegisters(0x2103 - 1, 4); // Realiza a leitura do registrador 9 ate 14 (0x0E)
      delay(200);
      
                                                                           
      if (resultMain == node.ku8MBSuccess) {
        Serial.print("ID1 Freq. de Comando ");
        Serial.println(node.getResponseBuffer(0x00)); // Apanha o dado alocado na posição 0 do buffer
        Serial.print("ID1 Freq. de Saída ");
        Serial.println(node.getResponseBuffer(0x01)); // Apanha o dado alocado na posição 1 do buffer
        Serial.print("ID1 Tensão do Barramento ");
        Serial.println(node.getResponseBuffer(0x03));
        
      } else {
        Serial.print("ID1MSGN");
        Serial.println("Falha de comunicacao");
      }

      delay(1000);

      
      deviceID = 1;
      break;
  }
  delay(45);
}
 
// Função que realiza a conversão de um caracter em ASCII para numero inteiro
int getInt(String texto) {
  int temp = texto.length() + 1;
  char buffer[temp];
  texto.toCharArray(buffer, temp);
  int num = atoi(buffer);
  return num;
}

Who is online

Users browsing this forum: No registered users and 28 guests