Skip to content

Commit 7e454ba

Browse files
committed
add modbus gateway approach (slave tcp - master serial)
1 parent 9fc77d5 commit 7e454ba

19 files changed

Lines changed: 1467 additions & 0 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
set(EXCLUDE_COMPONENTS examples test_app test freemodbus)
6+
7+
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
8+
list(APPEND EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
9+
10+
# Include parameters from common modbus folder
11+
set(MB_PARAMS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../mb_example_common")
12+
list(APPEND EXTRA_COMPONENT_DIRS "${MB_PARAMS_DIR}")
13+
14+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
15+
project(modbus_tcp_master)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := modbus_tcp_master
7+
8+
EXTRA_COMPONENT_DIRS := ../../../
9+
EXTRA_COMPONENT_DIRS += ../../mb_example_common
10+
EXTRA_COMPONENT_DIRS += $(IDF_PATH)/examples/common_components/protocol_examples_common
11+
EXCLUDE_COMPONENTS := test freemodbus
12+
13+
include $(IDF_PATH)/make/project.mk
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
3+
4+
# Modbus TCP Slave to Modbus Serial gateway example
5+
6+
This example demonstrates using of FreeModbus stack port implementation for ESP32 targets as a TCP gateway device.
7+
This implementation is able to read/write requests from external TCP Master and translate them to the slave devices connected into Modbus segment. The modbus data dictionary is not used but the mapping areas are defined in the Modbus TCP to reflect the values in its memory (this can be disabled).
8+
9+
The Gateway gets requests from Slave TCP and translates the request to installed Modbus Master instance previously configured.
10+
The translation and mapping teqnique uses the approach with wrapped callback read/write functions to make the adapter to translate the data between instances. This example is prepared from scratch to just demonstrate possible approach for the gateway. Other approaches can be used to override the component sources and realize the gateway object for data translation. The adapter functionality is located in mb_lib library which exposes some internals of installed modbus library.
11+
12+
STATUS: WIP, only holding registers are supported (functionality will be extended later).
13+
14+
The instances for the modbus parameters are common for master and slave examples and located in `examples/protocols/modbus/mb_example_common` folder.
15+
16+
The Kconfig ```Modbus slave address``` - CONFIG_MB_SLAVE_ADDR parameter in slave example can be configured to create Modbus multi slave segment.
17+
18+
Simplified Modbus connection schematic for example test:
19+
```
20+
MB_DEVICE_ADDR1
21+
------------- -------------
22+
| | Network | |
23+
| Slave 1 |---<>--+---<>---| Master |
24+
| | | |
25+
------------- -------------
26+
```
27+
Modbus multi slave segment connection schematic:
28+
```
29+
MB_DEVICE_ADDR1
30+
-------------
31+
| |
32+
| Slave 1 |---<>--+
33+
| | |
34+
------------- |
35+
MB_DEVICE_ADDR2 |
36+
------------- | -------------
37+
| | | | |
38+
| Slave 2 |---<>--+---<>---| Master |
39+
| | | | |
40+
------------- | -------------
41+
MB_DEVICE_ADDR3 |
42+
------------- Network (Ethernet or WiFi connection)
43+
| | |
44+
| Slave 3 |---<>--+
45+
| |
46+
-------------
47+
```
48+
49+
## Hardware required :
50+
Option 1:
51+
PC (Modbus TCP Slave application) + ESP32 based development board with modbus_tcp_slave example.
52+
53+
Option 2:
54+
Several ESP32 based boards flashed with modbus_tcp_slave example software to represent slave devices. The IP slave addresses for each board have to be configured in `Modbus Example Configuration` menu according to the communication table of example.
55+
One ESP32 based development board should be flashed with modbus_master example and connected to the same network. All the boards require configuration of network settings as described in `examples/common_components/protocol_examples_common`.
56+
57+
## How to setup and use an example:
58+
59+
### Configure the application
60+
Start the command below to setup configuration:
61+
```
62+
idf.py menuconfig
63+
```
64+
65+
The communication parameters of Modbus stack allow to configure it appropriately but usually it is enough to use default settings.
66+
See the help string of parameters for more information.
67+
There are three ways to configure how the master example will obtain slave IP addresses in the network:
68+
* Enable CONFIG_MB_MDNS_IP_RESOLVER option allows to query for modbus services provided by Modbus slaves in the network and automatically configure IP table. This requires to activate the same option for each slave with unique modbus slave address configured in `Modbus Example Configuration` menu.
69+
70+
### Setup external Modbus slave devices or emulator
71+
Option 1:
72+
Configure the external Modbus master software according to port configuration parameters used in the example. The Modbus Slave application can be used with this example to emulate slave devices with its parameters. Use official documentation for software to setup emulation of slave devices.
73+
74+
Option 2:
75+
Other option is to have the modbus_slave example application flashed into ESP32 based board and connect boards together as showed on the Modbus connection schematic above. See the Modbus slave API documentation to configure communication parameters and slave addresses as defined in "Example parameters definition" table above.
76+
77+
### Build and flash software of master device
78+
Build the project and flash it to the board, then run monitor tool to view serial output:
79+
```
80+
idf.py -p PORT flash monitor
81+
```
82+
83+
(To exit the serial monitor, type ``Ctrl-]``.)
84+
85+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
86+
87+
### Connect to the device IP from external TCP Master
88+
89+
Connect the external Modbus TCP Master software to the slave IP address showed in the log. The mDNS service can be used to connect to the device.
90+
91+
## Example Output
92+
Example output of the application:
93+
```
94+
I (4463) esp_netif_handlers: example_netif_sta ip: 192.168.88.247, mask: 255.255.255.0, gw: 192.168.88.1
95+
I (4463) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 192.168.88.247
96+
I (4473) example_common: Connected to example_netif_sta
97+
I (4473) example_common: - IPv4 address: 192.168.88.247,
98+
I (4483) wifi:Set ps type: 0, coexist: 0
99+
100+
I (4483) MB_TCP_SLAVE_PORT: Socket (#54), listener on port: 1502, errno=0
101+
I (4493) MB_TCP_SLAVE_PORT: Protocol stack initialized.
102+
I (4553) uart: queue free spaces: 20
103+
I (4553) SLAVE_TEST: Modbus master stack initialized...
104+
I (4553) SLAVE_TEST: Modbus slave stack initialized.
105+
I (4553) SLAVE_TEST: Start modbus test...
106+
I (11883) wifi:<ba-add>idx:0 (ifx:0, 64:d1:54:1a:23:5b), tid:0, ssn:5, winSize:64
107+
I (12923) MB_TCP_SLAVE_PORT: Socket (#55), accept client connection from address: 192.168.88.249
108+
W (13233) port_stub: callback __wrap_eMBRegHoldingCB, 0x3ffcd975, 1, 5
109+
W (13233) GW_TCP_BUF: 54 f6 00 00 00 06 01 03 0a 00 00 05
110+
I (13283) GW: Received response from serial slave.
111+
I (13283) SLAVE_TEST: HOLDING READ (12756870 us), ADDR:0, TYPE:2, INST_ADDR:0x3ffb4ec4, SIZE:5
112+
W (14503) port_stub: callback __wrap_eMBRegHoldingCB, 0x3ffcd975, 1, 5
113+
W (14503) GW_TCP_BUF: 54 f7 00 00 00 06 01 03 0a 00 00 05
114+
I (14533) GW: Received response from serial slave.
115+
I (14543) SLAVE_TEST: HOLDING READ (14010727 us), ADDR:0, TYPE:2, INST_ADDR:0x3ffb4ec4, SIZE:5
116+
W (15753) port_stub: callback __wrap_eMBRegHoldingCB, 0x3ffcd975, 1, 5
117+
W (15753) GW_TCP_BUF: 54 f8 00 00 00 06 01 03 0a 00 00 05
118+
I (15813) GW: Received response from serial slave.
119+
I (15813) SLAVE_TEST: HOLDING READ (15284289 us), ADDR:0, TYPE:2, INST_ADDR:0x3ffb4ec4, SIZE:5
120+
W (17023) port_stub: callback __wrap_eMBRegHoldingCB, 0x3ffcd975, 1, 5
121+
W (17023) GW_TCP_BUF: 54 f9 00 00 00 06 01 03 0a 00 00 05
122+
I (17073) GW: Received response from serial slave.
123+
I (17073) SLAVE_TEST: HOLDING READ (16544129 us), ADDR:0, TYPE:2, INST_ADDR:0x3ffb4ec4, SIZE:5
124+
W (18293) port_stub: callback __wrap_eMBRegHoldingCB, 0x3ffcd975, 1, 5
125+
W (18293) GW_TCP_BUF: 54 fa 00 00 00 06 01 03 0a 00 00 05
126+
I (18343) GW: Received response from serial slave.
127+
I (18353) SLAVE_TEST: HOLDING READ (17818814 us), ADDR:0, TYPE:2, INST_ADDR:0x3ffb4ec4, SIZE:5
128+
W (18663) port_stub: callback __wrap_eMBRegHoldingCB, 0x3ffcd979, 1, 2
129+
W (18663) GW_TCP_BUF: 54 fb 00 00 00 0b 01 10 00 00 00 02 04 00 00 42
130+
W (18673) GW_TCP_BUF: 5c
131+
I (18713) GW: Received response from serial slave.
132+
I (18723) SLAVE_TEST: HOLDING WRITE (18192158 us), ADDR:0, TYPE:1, INST_ADDR:0x3ffb4ec4, SIZE:2
133+
I (18723) SLAVE_TEST: Modbus controller destroyed.
134+
```
135+
The example reads the characteristics from serial slave device(s) over TCP. The output line describes the characteristics read from serial slave device and reflected in this gateway.
136+
137+
Modbus Serial Slave Log:
138+
Rx:003002-01 03 00 01 00 05 D4 09
139+
Tx:003003-01 03 0A 5C 42 55 AA 55 AA 55 AA 55 AA 9A BC
140+
Rx:003004-01 03 00 01 00 05 D4 09
141+
Tx:003005-01 03 0A 5C 42 55 AA 55 AA 55 AA 55 AA 9A BC
142+
Rx:003006-01 03 00 01 00 05 D4 09
143+
Tx:003007-01 03 0A 5C 42 55 AA 55 AA 55 AA 55 AA 9A BC
144+
Rx:003008-01 03 00 01 00 05 D4 09
145+
Tx:003009-01 03 0A 5C 42 55 AA 55 AA 55 AA 55 AA 9A BC
146+
Rx:003010-01 03 00 01 00 05 D4 09
147+
Tx:003011-01 03 0A 5C 42 55 AA 55 AA 55 AA 55 AA 9A BC
148+
149+
Modbus TCP Master log:
150+
Tx:007118-56 CF 00 00 00 06 01 03 00 00 00 05
151+
Rx:007119-56 CF 00 00 00 0D 01 03 0A 14 7A 40 FE D7 0A 40 23 EB 85
152+
Tx:007120-56 D0 00 00 00 06 01 03 00 00 00 05
153+
Rx:007121-56 D0 00 00 00 0D 01 03 0A A3 D7 41 10 D7 0A 40 23 EB 85
154+
Tx:007122-56 D1 00 00 00 0B 01 10 00 00 00 02 04 00 00 42 30
155+
Rx:007123-56 D1 00 00 00 06 01 10 00 00 00 02
156+
Tx:007124-56 D2 00 00 00 06 01 03 00 00 00 05
157+
Rx:007125-56 D2 00 00 00 0D 01 03 0A CC CD 3F 8C D7 0A 40 23 EB 85
158+
Tx:007126-56 D3 00 00 00 06 01 03 00 00 00 05
159+
Rx:007127-56 D3 00 00 00 0D 01 03 0A CC CD 40 0C D7 0A 40 23 EB 85
160+
Tx:007128-56 D4 00 00 00 06 01 03 00 00 00 05
161+
Rx:007129-56 D4 00 00 00 0D 01 03 0A 33 34 40 53 D7 0A 40 23 EB 85
162+
163+
164+
165+
166+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(PROJECT_NAME "gw_slave_tcp_master_serial")
2+
3+
idf_component_register(SRCS "gw_slave_tcp_master_serial.c"
4+
INCLUDE_DIRS ".")
5+
6+
add_subdirectory(mb_lib)
7+
target_link_libraries(${COMPONENT_LIB} PUBLIC mb_lib)
8+
9+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
menu "Modbus TCP over RTU gateway example Configuration"
2+
3+
config MB_SLAVE_ADDR
4+
int "Modbus slave address"
5+
range 1 247 if !FMB_TCP_UID_ENABLED
6+
range 0 247 if FMB_TCP_UID_ENABLED
7+
default 1
8+
help
9+
This is the Modbus slave address in the network.
10+
The address is used as an index to resolve slave ip address.
11+
12+
config MB_MDNS_IP_RESOLVER
13+
bool "Resolve slave addresses using mDNS service"
14+
default y
15+
help
16+
This option allows to use mDNS service to resolve IP addresses of the Modbus slaves.
17+
If the option is disabled the ip addresses of slaves are defined in static table.
18+
19+
config MB_UART_PORT_ONE
20+
bool
21+
default y
22+
depends on (ESP_CONSOLE_UART_NUM !=1) && (SOC_UART_NUM > 1)
23+
24+
config MB_UART_PORT_TWO
25+
bool
26+
default y
27+
depends on (ESP_CONSOLE_UART_NUM !=2) && (SOC_UART_NUM > 2)
28+
29+
config MB_UART_PORT_NUM
30+
int "UART port number"
31+
range 0 2 if MB_UART_PORT_TWO
32+
default 2 if MB_UART_PORT_TWO
33+
range 0 1 if MB_UART_PORT_ONE
34+
default 1 if MB_UART_PORT_ONE
35+
help
36+
UART communication port number for Modbus example.
37+
38+
config MB_UART_BAUD_RATE
39+
int "UART communication speed"
40+
range 1200 115200
41+
default 115200
42+
help
43+
UART communication speed for Modbus example.
44+
45+
config MB_UART_RXD
46+
int "UART RXD pin number"
47+
range 0 34 if IDF_TARGET_ESP32
48+
range 0 23 if IDF_TARGET_ESP32C6
49+
range 0 56 if IDF_TARGET_ESP32P4
50+
default 22 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C6 || IDF_TARGET_ESP32P4
51+
range 0 46 if IDF_TARGET_ESP32S2
52+
range 0 47 if IDF_TARGET_ESP32S3
53+
range 0 19 if IDF_TARGET_ESP32C3
54+
range 0 20 if IDF_TARGET_ESP32C2
55+
range 0 27 if IDF_TARGET_ESP32H2
56+
default 8 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
57+
default 8 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32H2
58+
help
59+
GPIO number for UART RX pin. See UART documentation for more information
60+
about available pin numbers for UART.
61+
62+
config MB_UART_TXD
63+
int "UART TXD pin number"
64+
range 0 34 if IDF_TARGET_ESP32
65+
range 0 23 if IDF_TARGET_ESP32C6
66+
range 0 56 if IDF_TARGET_ESP32P4
67+
default 23 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C6 || IDF_TARGET_ESP32P4
68+
range 0 46 if IDF_TARGET_ESP32S2
69+
range 0 47 if IDF_TARGET_ESP32S3
70+
range 0 19 if IDF_TARGET_ESP32C3
71+
range 0 20 if IDF_TARGET_ESP32C2
72+
range 0 27 if IDF_TARGET_ESP32H2
73+
default 9 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
74+
default 9 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32H2
75+
help
76+
GPIO number for UART TX pin. See UART documentation for more information
77+
about available pin numbers for UART.
78+
79+
config MB_UART_RTS
80+
int "UART RTS pin number"
81+
range 0 34 if IDF_TARGET_ESP32
82+
range 0 23 if IDF_TARGET_ESP32C6
83+
range 0 56 if IDF_TARGET_ESP32P4
84+
default 20 if IDF_TARGET_ESP32P4
85+
default 18 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C6
86+
range 0 46 if IDF_TARGET_ESP32S2
87+
range 0 47 if IDF_TARGET_ESP32S3
88+
range 0 19 if IDF_TARGET_ESP32C3
89+
range 0 20 if IDF_TARGET_ESP32C2
90+
range 0 27 if IDF_TARGET_ESP32H2
91+
default 10 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
92+
default 10 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32H2
93+
help
94+
GPIO number for UART RTS pin. This pin is connected to
95+
~RE/DE pin of RS485 transceiver to switch direction.
96+
See UART documentation for more information about available pin
97+
numbers for UART.
98+
99+
choice MB_COMM_MODE
100+
prompt "Modbus communication mode"
101+
default MB_COMM_MODE_RTU if CONFIG_FMB_COMM_MODE_RTU_EN
102+
help
103+
Selection of Modbus communication mode option for Modbus.
104+
105+
config MB_COMM_MODE_RTU
106+
bool "RTU mode"
107+
depends on FMB_COMM_MODE_RTU_EN
108+
109+
config MB_COMM_MODE_ASCII
110+
bool "ASCII mode"
111+
depends on FMB_COMM_MODE_ASCII_EN
112+
113+
endchoice
114+
115+
endmenu
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#
2+
# "main" pseudo-component makefile.
3+
#
4+
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

0 commit comments

Comments
 (0)