README.md
1# Modbus Master-Slave Example
2
3## Overview
4
5These two projects illustrate the communication between Modbus master and slave device in the segment.
6Master initializes Modbus interface driver and then reads parameters from slave device in the segment.
7After several successful read attempts slave sets the alarm relay (end of test condition).
8Once master reads the alarm it stops communication and destroy driver.
9
10The examples:
11
12* `examples/protocols/modbus/serial/mb_master` - Modbus serial master ASCII/RTU
13* `examples/protocols/modbus/serial/mb_slave` - Modbus serial slave ASCII/RTU
14
15See README.md for each individual project for more information.
16
17## How to use example
18
19### Hardware Required
20
21This example can be run on any commonly available ESP32 development board.
22The master and slave boards should be connected to each other through the RS485 interface line driver.
23See the connection schematic in README.md files of each example.
24
25### Configure the project
26
27This example test requires communication mode setting for master and slave be the same and slave address set to 1.
28Please refer to README.md files of each example project for more information.
29
30## About common_component in this example
31
32The folder "mb_example_common" includes definitions of parameter structures for master and slave device (both projects share the same parameters).
33However, currently it is for example purpose only and can be modified for particular application.
34
35## Example Output
36
37Example of Slave output:
38
39```
40I (343) SLAVE_TEST: Modbus slave stack initialized.
41I (343) SLAVE_TEST: Start modbus test...
42I (81463) SLAVE_TEST: HOLDING READ (81150420 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2868, SIZE:6
43I (82463) SLAVE_TEST: HOLDING READ (82150720 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2868, SIZE:6
44I (83573) SLAVE_TEST: HOLDING READ (83260630 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2868, SIZE:6
45I (84603) SLAVE_TEST: HOLDING READ (84290530 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2868, SIZE:6
46I (85703) SLAVE_TEST: HOLDING READ (85396692 us), ADDR:1, TYPE:2, INST_ADDR:0x3ffb2868, SIZE:6
47```
48
49Example of Modbus Master output:
50
51```
52I (399) MASTER_TEST: Modbus master stack initialized...
53I (499) MASTER_TEST: Start modbus test...
54I (549) MASTER_TEST: Characteristic #0 Data_channel_0 (Volts) value = 1.230000 (0x3f9d70a4) read successful.
55I (629) MASTER_TEST: Characteristic #1 Humidity_1 (%rH) value = 12.100000 (0x4141999a) read successful.
56I (709) MASTER_TEST: Characteristic #2 Temperature_1 (C) value = 3.560000 (0x4063d70a) read successful.
57I (769) MASTER_TEST: Characteristic #3 Humidity_2 (%rH) value = 23.400000 (0x41bb3333) read successful.
58I (829) MASTER_TEST: Characteristic #4 Temperature_2 (C) value = 5.890000 (0x40bc7ae1) read successful.
59I (889) MASTER_TEST: Characteristic #5 Humidity_3 (%rH) value = 34.500000 (0x420a0000) read successful.
60E (949) MB_CONTROLLER_MASTER: mbc_master_get_parameter(111): SERIAL master get parameter failure error=(0x108) (ESP_ERR_INVALID_RESPONSE).
61E (949) MASTER_TEST: Characteristic #6 (RelayP1) read fail, err = 264 (ESP_ERR_INVALID_RESPONSE).
62E (1029) MB_CONTROLLER_MASTER: mbc_master_get_parameter(111): SERIAL master get parameter failure error=(0x108) (ESP_ERR_INVALID_RESPONSE).
63E (1029) MASTER_TEST: Characteristic #7 (RelayP2) read fail, err = 264 (ESP_ERR_INVALID_RESPONSE).
64```
65
66## Troubleshooting
67
68If the examples do not work as expected and slave and master boards are not able to communicate correctly it is possible to find the reason for errors.
69The most important errors are described in master example output and formatted as below:
70
71```
72E (1692332) MB_CONTROLLER_MASTER: mbc_master_get_parameter(111): SERIAL master get parameter failure error=(0x107) (ESP_ERR_TIMEOUT).
73```
74
75ESP_ERR_TIMEOUT (0x107) - Modbus slave device does not respond during configured timeout. Check the connection and ability for communication using uart_echo_rs485 example or increase
76Kconfig value CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND (CONFIG_FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS).
77
78ESP_ERR_NOT_SUPPORTED (0x106), ESP_ERR_INVALID_RESPONSE (0x108) - Modbus slave device does not support requested command or register and sent exeption response.
79
80ESP_ERR_INVALID_STATE (0x103) - Modbus stack is not configured correctly or can't work correctly due to critical failure.
81
82
83