Skip to content

dev-embarcados/mb_slave

Repository files navigation

Simulador Modbus para Inversor Danfoss (ESP-IDF)

Este projeto implementa um simulador de escravo Modbus RTU/ASCII para inversores Danfoss, rodando em ESP32 via RS485. O software apresenta contexto de variáveis e registradores compatíveis com o protocolo Modbus do inversor Danfoss (parâmetros de frequência, torque, status, erros, etc.), facilitando testes de integração e validação de sistemas de automação.

Funcionalidade principal

  • Simulador de inversor Danfoss como escravo Modbus, com mapa de registradores customizável.
  • Modo escravo Modbus com endereço configurável (padrão: 1).
  • Suporte a leitura/escrita de registradores (holding/input) e coils/entradas discretas.
  • Atualização dinâmica de parâmetros em main/parametersfv51.h / main/slave.c.
  • Configuração de comunicação em Kconfig (CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD, CONFIG_MB_UART_RTS, CONFIG_MB_COMM_MODE).
  • Exemplo de integração com modbus_master para validação em bancada.

Pré-requisitos

  1. Espressif ESP-IDF v5.x configurado no sistema.
  2. Placa ESP32 (todas as variantes listadas no cabeçalho do ESP-IDF são suportadas).
  3. Conversor USB-RS485 (ex: MAX485) com conexão correta de TX/RX/RTS/GND.

Como usar

  1. Abrir terminal no diretório do projeto.
  2. Executar idf.py menuconfig e ajustar:
    • Example Configuration > Modbus slave address.
    • Component config > Modbus configuration > Modbus communication mode (RTU/ASCII).
    • GPIO do UART (CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD, CONFIG_MB_UART_RTS).
  3. Compilar e gravar:
    • idf.py -p COMx clean build flash monitor.
  4. Com o monitor ativo, testar com cliente Modbus (Modbus Poll, QModMaster, etc.).

Exemplo de saída

I (13941) SLAVE_TEST: INPUT READ ..., ADDR:1, TYPE:8, SIZE:2
I (13951) SLAVE_TEST: HOLDING READ ..., ADDR:1, TYPE:2, SIZE:2
...
I (14001) SLAVE_TEST: Modbus controller destroyed.

Configure the application

Start the command below to show the configuration menu:

idf.py menuconfig

Select Modbus Example Configuration menu item. Configure the UART pins used for modbus communication using the command and table below.

  ------------------------------------------------------------------------------------------------------------------------------
  |  UART Interface       | #define            | Default pins for      | Default pins for ESP32-S2 | External RS485 Driver Pin |
  |                       |                    | ESP32 (C6, P4)        | (S3, C3, C2, C5, H2, C61) |                           |
  | ----------------------|--------------------|-----------------------|---------------------------|---------------------------|
  | Transmit Data (TxD)   | CONFIG_MB_UART_TXD | GPIO23                | GPIO9                     | DI                        |
  | Receive Data (RxD)    | CONFIG_MB_UART_RXD | GPIO22                | GPIO8                     | RO                        |
  | Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18                | GPIO10                    | ~RE/DE                    |
  | Ground                | n/a                | GND                   | GND                       | GND                       |
  ------------------------------------------------------------------------------------------------------------------------------

Note: Each target chip has different GPIO pins available for UART connection. Please refer to UART documentation for selected target for more information.

Define the Modbus communiction mode for slave in Kconfig - CONFIG_MB_COMM_MODE (must be the same for master and slave application). Set Modbus slave address for the example application (by default for example script is set to 1). The communication parameters of freemodbus stack (Component config->Modbus configuration) allow to configure it appropriately but usually it is enough to use default settings. See the help strings of parameters for more information.

Setup external Modbus master software

Option 1: Configure the external Modbus master software according to port configuration parameters used in application. As an example the Modbus Poll application can be used with this example. Option 2: Setup ESP32 based board and set modbus_master example configuration as described in its README.md file. Setup one or more slave boards with different slave addresses and connect them into the same Modbus segment (See configuration above). Note: The Modbus communiction mode parameter must be the same for master and slave example application to be able to communicate with each other.

Build and flash software

Build the project and flash it to the board, then run monitor tool to view serial output:

idf.py -p PORT flash monitor

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Example Output

Example output of the application:

I (13941) SLAVE_TEST: INPUT READ (13651163 us), ADDR:1, TYPE:8, INST_ADDR:0x3ffb2fd0, SIZE:2
I (13951) SLAVE_TEST: HOLDING READ (13656431 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2fe0, SIZE:2
I (13961) SLAVE_TEST: INPUT READ (13665877 us), ADDR:3, TYPE:8, INST_ADDR:0x3ffb2fd4, SIZE:2
I (13971) SLAVE_TEST: HOLDING READ (13676010 us), ADDR:3, TYPE:2, INST_ADDR:0x3ffb2fe4, SIZE:2
I (13981) SLAVE_TEST: INPUT READ (13686130 us), ADDR:5, TYPE:8, INST_ADDR:0x3ffb2fd8, SIZE:2
I (13991) SLAVE_TEST: HOLDING READ (13696267 us), ADDR:5, TYPE:2, INST_ADDR:0x3ffb2fe8, SIZE:2
I (14001) SLAVE_TEST: COILS READ (13706331 us), ADDR:0, TYPE:32, INST_ADDR:0x3ffb2fcc, SIZE:8
I (14001) SLAVE_TEST: Modbus controller destroyed.

The output lines describe type of operation, its timestamp, modbus address, access type, storage address in parameter structure and number of registers accordingly.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages