Skip to content

Commit ff8b978

Browse files
committed
Merge branch 'bugfix/tcp_driver_fix_connection_issues' into 'main'
fix master-slave connection repair issues See merge request idf/esp-modbus!118
2 parents 0f01533 + 6f01b85 commit ff8b978

File tree

26 files changed

+261
-225
lines changed

26 files changed

+261
-225
lines changed

Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ menu "Modbus configuration"
1616

1717
config FMB_TCP_PORT_MAX_CONN
1818
int "Maximum allowed connections for TCP stack"
19-
range 1 8
19+
range 1 LWIP_MAX_SOCKETS
2020
default 5
2121
depends on FMB_COMM_MODE_TCP_EN
2222
help
@@ -28,7 +28,7 @@ menu "Modbus configuration"
2828
config FMB_TCP_CONNECTION_TOUT_SEC
2929
int "Modbus TCP connection timeout"
3030
range 1 7200
31-
default 20
31+
default 2
3232
depends on FMB_COMM_MODE_TCP_EN
3333
help
3434
Modbus TCP connection timeout in seconds.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
dependencies:
22
idf: ">=5.0"
33
espressif/esp-modbus:
4-
version: "^2.0.0"
4+
version: "^2"
55
override_path: "../../../../"
66
mb_example_common:
77
path: "../../../mb_example_common"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
dependencies:
22
idf: ">=5.0"
33
espressif/esp-modbus:
4-
version: "^2.0.0"
4+
version: "^2"
55
override_path: "../../../../"
66
mb_example_common:
77
path: "../../../mb_example_common"

examples/serial/mb_serial_slave/sdkconfig.defaults

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#
22
# Modbus configuration
33
#
4-
CONFIG_MB_COMM_MODE_ASCII=y
4+
CONFIG_MB_COMM_MODE_ASCII=n
5+
CONFIG_MB_COMM_MODE_RTU=y
56
CONFIG_MB_SLAVE_ADDR=1
67
CONFIG_MB_UART_BAUD_RATE=115200
78
CONFIG_FMB_TIMER_USE_ISR_DISPATCH_METHOD=y

examples/tcp/mb_tcp_master/main/idf_component.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ dependencies:
22
idf:
33
version: ">=5.0"
44
espressif/esp-modbus:
5-
version: "^2.0.0"
5+
version: "^2"
66
override_path: "../../../../"
77
espressif/mdns:
8-
version: "^1.0.0"
8+
version: "^1"
99
mb_example_common:
1010
path: "../../../mb_example_common"
1111
protocol_examples_common:

examples/tcp/mb_tcp_slave/main/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
dependencies:
22
idf: ">=5.0"
33
espressif/esp-modbus:
4-
version: "^2.0.0"
4+
version: "^2"
55
override_path: "../../../../"
66
espressif/mdns:
77
version: "^1.0.0"

examples/tcp/mb_tcp_slave/main/tcp_slave.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define MB_REG_HOLDING_START_AREA0 (HOLD_OFFSET(holding_data0))
3737
#define MB_REG_HOLDING_START_AREA0_SIZE ((size_t)((HOLD_OFFSET(holding_data4) - HOLD_OFFSET(holding_data0)) << 1))
3838
#define MB_REG_HOLDING_START_AREA1 (HOLD_OFFSET(holding_data4))
39-
#define MB_REG_HOLDING_START_AREA1_SIZE ((size_t)((HOLD_OFFSET(holding_area1_end) - HOLD_OFFSET(holding_data4)) << 1))
39+
#define MB_REG_HOLDING_START_AREA1_SIZE ((size_t)((HOLD_OFFSET(holding_area1_end) - HOLD_OFFSET(holding_data4)) << 1) + 4)
4040
#define MB_REG_HOLDING_START_AREA2 (HOLD_OFFSET(holding_u8_a))
4141
#define MB_REG_HOLDING_START_AREA2_SIZE ((size_t)((HOLD_OFFSET(holding_area2_end) - HOLD_OFFSET(holding_u8_a)) << 1))
4242

@@ -327,7 +327,7 @@ static esp_err_t slave_init(mb_communication_info_t *pcomm_info)
327327
reg_area.type = MB_PARAM_HOLDING; // Set type of register area
328328
reg_area.start_offset = MB_REG_HOLDING_START_AREA1; // Offset of register area in Modbus protocol
329329
reg_area.address = (void*)&holding_reg_params.holding_data4; // Set pointer to storage instance
330-
reg_area.size = sizeof(float) << 2; // Set the size of register storage instance
330+
reg_area.size = MB_REG_HOLDING_START_AREA1_SIZE; // Set the size of register storage instance
331331
reg_area.access = MB_ACCESS_RW;
332332
err = mbc_slave_set_descriptor(slave_handle, reg_area);
333333
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "2.0.2"
1+
version: "2.1.0"
22
description: ESP-MODBUS is the official Modbus library for Espressif SoCs.
33
url: https://github.com/espressif/esp-modbus
44
dependencies:

modbus/mb_controller/tcp/mbc_tcp_master.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,12 @@ static esp_err_t mbc_tcp_master_get_parameter(void *ctx, uint16_t cid, uint8_t *
338338

339339
error = mbc_tcp_master_set_request(ctx, cid, MB_PARAM_READ, &request, &reg_info);
340340
if ((error == ESP_OK) && (cid == reg_info.cid) && (request.slave_addr != MB_SLAVE_ADDR_PLACEHOLDER)) {
341-
mb_uid_info_t *paddr_info = mbm_port_tcp_get_slave_info(mbm_controller_iface->mb_base->port_obj,
341+
mb_uid_info_t *paddr_info = mbm_port_tcp_get_slave_info(mbm_controller_iface->mb_base->port_obj,
342342
request.slave_addr, MB_SOCK_STATE_CONNECTED);
343-
MB_RETURN_ON_FALSE((paddr_info), ESP_ERR_NOT_FOUND, TAG,
344-
"mb can not send request for cid #%u with uid = %d.",
345-
(unsigned)reg_info.cid, (int)request.slave_addr);
343+
if (!paddr_info) {
344+
ESP_LOGW(TAG, "Try to send request for cid #%u with uid = %d, node is disconnected.",
345+
(unsigned)reg_info.cid, (int)request.slave_addr);
346+
}
346347
MB_MASTER_ASSERT(xPortGetFreeHeapSize() > (reg_info.mb_size << 1));
347348
// alloc buffer to store parameter data
348349
pdata = calloc(1, (reg_info.mb_size << 1));
@@ -394,8 +395,10 @@ static esp_err_t mbc_tcp_master_get_parameter_with(void *ctx, uint16_t cid, uint
394395
// check that the requested uid is connected (call to port iface)
395396
mb_uid_info_t *paddr_info = mbm_port_tcp_get_slave_info(mbm_controller_iface->mb_base->port_obj,
396397
uid, MB_SOCK_STATE_CONNECTED);
397-
MB_RETURN_ON_FALSE((paddr_info), ESP_ERR_NOT_FOUND, TAG,
398-
"mb can not send request for cid #%u with uid=%d.", (unsigned)reg_info.cid, (int)uid);
398+
if (!paddr_info) {
399+
ESP_LOGW(TAG, "Try to send request for cid #%u with uid = %d, node is disconnected.",
400+
(unsigned)reg_info.cid, (int)request.slave_addr);
401+
}
399402
if (request.slave_addr != MB_SLAVE_ADDR_PLACEHOLDER) {
400403
ESP_LOGD(TAG, "%s: override uid %d = %d for cid(%u)",
401404
__FUNCTION__, (int)request.slave_addr, (int)uid, (unsigned)reg_info.cid);
@@ -449,11 +452,12 @@ static esp_err_t mbc_tcp_master_set_parameter(void *ctx, uint16_t cid, uint8_t *
449452

450453
error = mbc_tcp_master_set_request(ctx, cid, MB_PARAM_WRITE, &request, &reg_info);
451454
if ((error == ESP_OK) && (cid == reg_info.cid) && (request.slave_addr != MB_SLAVE_ADDR_PLACEHOLDER)) {
452-
mb_uid_info_t *paddr_info = mbm_port_tcp_get_slave_info(mbm_controller_iface->mb_base->port_obj,
455+
mb_uid_info_t *paddr_info = mbm_port_tcp_get_slave_info(mbm_controller_iface->mb_base->port_obj,
453456
request.slave_addr, MB_SOCK_STATE_CONNECTED);
454-
MB_RETURN_ON_FALSE((paddr_info), ESP_ERR_NOT_FOUND, TAG,
455-
"mb can not send request for cid #%u with uid=%d.",
456-
(unsigned)reg_info.cid, (int)request.slave_addr);
457+
if (!paddr_info) {
458+
ESP_LOGW(TAG, "Try to send request for cid #%u with uid = %d, node is disconnected.",
459+
(unsigned)reg_info.cid, (int)request.slave_addr);
460+
}
457461
MB_MASTER_ASSERT(xPortGetFreeHeapSize() > (reg_info.mb_size << 1));
458462
pdata = calloc(1, (reg_info.mb_size << 1)); // alloc parameter buffer
459463
if (!pdata) {
@@ -503,9 +507,10 @@ static esp_err_t mbc_tcp_master_set_parameter_with(void *ctx, uint16_t cid, uint
503507
// check that the requested uid is connected (call to port iface)
504508
mb_uid_info_t *paddr_info = mbm_port_tcp_get_slave_info(mbm_controller_iface->mb_base->port_obj,
505509
uid, MB_SOCK_STATE_CONNECTED);
506-
MB_RETURN_ON_FALSE((paddr_info), ESP_ERR_NOT_FOUND, TAG,
507-
"mb can not send request for cid #%d with uid=%d.",
508-
(unsigned)reg_info.cid, (int)uid);
510+
if (!paddr_info) {
511+
ESP_LOGW(TAG, "Try to send request for cid #%u with uid = %d, node is disconnected.",
512+
(unsigned)reg_info.cid, (int)request.slave_addr);
513+
}
509514
if (request.slave_addr != MB_SLAVE_ADDR_PLACEHOLDER) {
510515
ESP_LOGD(TAG, "%s: override uid %d = %d for cid(%u)",
511516
__FUNCTION__, (int)request.slave_addr, (int)uid, (unsigned)reg_info.cid);

modbus/mb_objects/common/mb_port_types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ typedef enum _addr_type_enum {
6161
MB_IPV6 = 2 /*!< TCP IPV6 addressing */
6262
} mb_addr_type_t;
6363

64-
6564
struct port_common_opts_s {
6665
mb_mode_type_t mode; /*!< Modbus communication mode */
6766
uint16_t port; /*!< Modbus communication port (UART) number */

0 commit comments

Comments
 (0)