1| Supported Targets | ESP32 | 2| ----------------- | ----- | 3 4# Modbus Slave Example 5 6This example demonstrates using of FreeModbus stack port implementation for ESP32. The external Modbus host is able to read/write device parameters using Modbus protocol transport. The parameters accessible thorough Modbus are located in `mb_example_common/modbus_params.h\c` files and can be updated by user. 7These are represented in structures holding_reg_params, input_reg_params, coil_reg_params, discrete_reg_params for holding registers, input parameters, coils and discrete inputs accordingly. The app_main application demonstrates how to setup Modbus stack and use notifications about parameters change from host system. 8The FreeModbus stack located in `components/freemodbus` folder and contains the `/port` folder inside with FreeModbus stack port for ESP32. There are some parameters that can be configured in KConfig file to start stack correctly (See description below for more information). 9 10The slave example uses shared parameter structures defined in `examples/protocols/modbus/mb_example_common` folder. 11 12## Hardware required : 13Option 1: 14PC + USB Serial adapter connected to USB port + RS485 line drivers + ESP32 WROVER-KIT board. 15The MAX485 line driver is used as an example below but other similar chips can be used as well. 16 17Option 2: 18The modbus_master example application configured as described in its README.md file and flashed into ESP32 WROVER-KIT board. 19Note: The ```Example Data (Object) Dictionary``` in the modbus_master example can be edited to address parameters from other slaves connected into Modbus segment. 20 21RS485 example circuit schematic: 22``` 23 VCC ---------------+ +--------------- VCC 24 | | 25 +-------x-------+ +-------x-------+ 26 RXD <------| RO | DIFFERENTIAL | RO|-----> RXD 27 | B|---------------|B | 28 TXD ------>| DI MAX485 | \ / | MAX485 DI|<----- TXD 29ESP32 WROVER KIT 1 | | RS-485 side | | Modbus master 30 RTS --+--->| DE | / \ | DE|---+ 31 | | A|---------------|A | | 32 +----| /RE | PAIR | /RE|---+-- RTS 33 +-------x--------+ +-------x-------+ 34 | | 35 --- --- 36``` 37 38## How to setup and use an example: 39 40### Configure the application 41Start the command below to show the configuration menu: 42``` 43idf.py menuconfig 44``` 45Select Modbus Example Configuration menu item. 46Configure the UART pins used for modbus communication using command and table below. 47``` 48 -------------------------------------------------------------------------------------------------------------------------- 49 | ESP32 Interface | #define | Default ESP32 Pin | Default ESP32-S2 Pins | External RS485 Driver Pin | 50 | ----------------------|--------------------|-----------------------|-----------------------|---------------------------| 51 | Transmit Data (TxD) | CONFIG_MB_UART_TXD | GPIO23 | GPIO20 | DI | 52 | Receive Data (RxD) | CONFIG_MB_UART_RXD | GPIO22 | GPIO19 | RO | 53 | Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18 | GPIO18 | ~RE/DE | 54 | Ground | n/a | GND | GND | GND | 55 -------------------------------------------------------------------------------------------------------------------------- 56``` 57Note: The GPIO22 - GPIO25 can not be used with ESP32-S2 chip because they are used for flash chip connection. Please refer to UART documentation for selected target. 58 59Define the ```Modbus communiction mode``` for slave in Kconfig - CONFIG_MB_COMM_MODE (must be the same for master and slave application). 60Set ```Modbus slave address``` for the example application (by default for example script is set to 1). 61The communication parameters of freemodbus stack (Component config->Modbus configuration) allow to configure it appropriately but usually it is enough to use default settings. 62See the help strings of parameters for more information. 63 64### Setup external Modbus master software 65Option 1: 66Configure the external Modbus master software according to port configuration parameters used in application. 67As an example the Modbus Poll application can be used with this example. 68Option 2: 69Setup ESP32 WROVER-KIT board and set modbus_master example configuration as described in its README.md file. 70Setup one or more slave boards with different slave addresses and connect them into the same Modbus segment (See configuration above). 71Note: The ```Modbus communiction mode``` parameter must be the same for master and slave example application to be able to communicate with each other. 72 73### Build and flash software 74Build the project and flash it to the board, then run monitor tool to view serial output: 75``` 76idf.py -p PORT flash monitor 77``` 78 79(To exit the serial monitor, type ``Ctrl-]``.) 80 81See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. 82 83## Example Output 84Example output of the application: 85``` 86I (13941) SLAVE_TEST: INPUT READ (13651163 us), ADDR:1, TYPE:8, INST_ADDR:0x3ffb2fd0, SIZE:2 87I (13951) SLAVE_TEST: HOLDING READ (13656431 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2fe0, SIZE:2 88I (13961) SLAVE_TEST: INPUT READ (13665877 us), ADDR:3, TYPE:8, INST_ADDR:0x3ffb2fd4, SIZE:2 89I (13971) SLAVE_TEST: HOLDING READ (13676010 us), ADDR:3, TYPE:2, INST_ADDR:0x3ffb2fe4, SIZE:2 90I (13981) SLAVE_TEST: INPUT READ (13686130 us), ADDR:5, TYPE:8, INST_ADDR:0x3ffb2fd8, SIZE:2 91I (13991) SLAVE_TEST: HOLDING READ (13696267 us), ADDR:5, TYPE:2, INST_ADDR:0x3ffb2fe8, SIZE:2 92I (14001) SLAVE_TEST: COILS READ (13706331 us), ADDR:0, TYPE:32, INST_ADDR:0x3ffb2fcc, SIZE:8 93I (14001) SLAVE_TEST: Modbus controller destroyed. 94``` 95The output lines describe type of operation, its timestamp, modbus address, access type, storage address in parameter structure and number of registers accordingly. 96 97