Esp32 ModbusMaster Arduino IDE

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

Esp32 ModbusMaster Arduino IDE

Postby girasera » Thu Jul 09, 2020 2:48 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(20);
      resultMain = node.readHoldingRegisters(0x2103 - 1, 4); // Realiza a leitura do registrador 9 ate 14 (0x0E)
      delay(20);
      
                                                                           
      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;
}

u063096
Posts: 34
Joined: Wed Feb 13, 2019 3:27 pm

Re: Esp32 ModbusMaster Arduino IDE

Postby u063096 » Fri Jul 10, 2020 1:27 pm

What does 'resultMain' say after the readHoldungRegisters call? Is the address you are attempting to read valid on your frequency inverter?

Second, pins 0 and 14 look odd for the ESP32's UART2, especially the 0. I know of no ESP32 that has a serial line on pin 0, since that is used for boot etc. normally.

ORSO2001
Posts: 5
Joined: Fri Jan 03, 2020 2:48 pm

Re: Esp32 ModbusMaster Arduino IDE

Postby ORSO2001 » Mon Jul 13, 2020 6:50 am

Hi girasera,

In the past I have had similar problem; I have search in the net for some cause/solution...at the end I found that the ESP32 "close" the transimition too fast....but also, using a signal analizer, I found that the quality of the data exchange was not good...so i tried the MAX3485 module and all gone fine...in my case seemed that the system was not able to difine an "idle" state...this was fixed using the MAX3485.

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

Re: Esp32 ModbusMaster Arduino IDE

Postby girasera » Mon Jul 13, 2020 11:48 am

u063096 wrote:
Fri Jul 10, 2020 1:27 pm
What does 'resultMain' say after the readHoldungRegisters call? Is the address you are attempting to read valid on your frequency inverter?

Second, pins 0 and 14 look odd for the ESP32's UART2, especially the 0. I know of no ESP32 that has a serial line on pin 0, since that is used for boot etc. normally.
Hi,

'resultaMain' returns: ku8MBResponseTimeout. All parameters for communication are correct (adress, baudrate, parity, etc) and I also tested others.


I know that it is not common to use pins 0 and 14 for serial communication, but it was necessary because my hardware is a module with an ESP32 wrover integrated into a sim800L, then the main communication pins are already in use. I tested pins 0 and 14 in a simple serial communication with an arduino to see if they will be able to work, it had no problem.

Satyampol
Posts: 1
Joined: Wed Jul 15, 2020 10:43 am

Re: Esp32 ModbusMaster Arduino IDE

Postby Satyampol » Wed Jul 15, 2020 11:13 am

Informative share!

Who is online

Users browsing this forum: Baidu [Spider] and 28 guests