mbc_master_start() can not connect to slaves
Posted: Tue Feb 11, 2025 3:24 pm
For some reasons I am working on ESP-IDF 4.4.5 and I'm trying to use esp-modbus to connect to some slaves. I did the connection with the ESP32 on serial master and it was all OK. Now I'm trying to connect the ESP32 (tcp master) with a slave simulator and a real slave device. The error that I am facing is at the mbc_master_start() function, when he can not found any slave device. I tested myself to connect to that slave simulator from other device and it works ok. The IP address is OK. I don't know why the Host IP is the same as the slave IP but I've seen in other projects in the forum they could make it work. Why mbc_master_start() is not working? Thank you
Edit:
I used "set(EXCLUDE_COMPONENTS "freemodbus")" on the project's CmakeLists.txt (I don't know if the order in that file is important.
The esp-modbus version is 1.0.17 (to make it work on ESP-IDF 4.3 and 4.4.5).
My sdkconfig related to the modbus configurated is:
I (00:00:24.525) NETWORK: ESP32 Host IP: 192.168.3.59
E (00:00:24.526) TAG: mbc_master_init OK: ESP_OK
E (00:00:24.526) TAG: mbc_master_setup OK: ESP_OK
E (00:00:24.531) TAG: mbc_master_set_descriptor OK: ESP_OK
I (00:00:24.537) MB_TCP_MASTER_PORT: TCP master stack initialized.
I (00:00:24.545) MB_TCP_MASTER_PORT: Host IP: [192.168.3.36]
I (00:00:24.551) MB_TCP_MASTER_PORT: Add slave IP: 192.168.3.36
I (00:00:24.557) MB_TCP_MASTER_PORT: Connecting to slaves...
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
E (00:01:24.563) MB_CONTROLLER_MASTER: mbc_tcp_master_start(179): mb stack could not connect to slaves for 60 seconds.
E (00:01:24.565) MB_CONTROLLER_MASTER: mbc_master_start(179): Master start failure, error=(0x103) (ESP_ERR_INVALID_STATE).
E (00:01:24.577) TAG: Error mbc_master_start: ESP_ERR_INVALID_STATE
Code: Select all
[Codebox=cpp file=Untitled.cpp]
char* slave_ip_address_table[1] = {
"192.168.3.36",
};
const size_t ip_table_sz = (size_t)(sizeof(slave_ip_address_table) / sizeof(slave_ip_address_table[0]));
esp_netif_ip_info_t ip_info;
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); // Para WiFi en modo estación
if (netif == NULL) {
ESP_LOGE("NETWORK", "No se encontró la interfaz de red.");
return;
}
esp_netif_get_ip_info(netif, &ip_info);
ip4_addr_t ip_addr;
ip_addr.addr = ip_info.ip.addr;
ESP_LOGI("NETWORK", "ESP32 Host IP: %s", ip4addr_ntoa(&ip_addr));
// Initialize and start Modbus controller
mb_communication_info_t comm1 = {};
comm1.mode = MB_MODE_TCP;
comm1.port = 502;
comm1.ip_addr_type = MB_IPV4;
comm1.ip_addr = (void*) slave_ip_address_table;
comm1.ip_netif_ptr = netif;
void* master_handler = NULL;
// mbc_master_init_iface(&master_handler);
esp_err_t err_init = mbc_master_init_tcp(&master_handler);
if (err_init != ESP_OK) {
ESP_LOGE("TAG", "Error mbc_master_init: %s", esp_err_to_name(err_init));
} else {
ESP_LOGE("TAG", "mbc_master_init OK: %s", esp_err_to_name(err_init));
}
esp_err_t err_setup = mbc_master_setup((void*)&comm1);
if (err_setup != ESP_OK) {
ESP_LOGE("TAG", "Error mbc_master_setup: %s", esp_err_to_name(err_setup));
} else {
ESP_LOGE("TAG", "mbc_master_setup OK: %s", esp_err_to_name(err_init));
}
esp_err_t err_descriptor = mbc_master_set_descriptor(&device_parameters1[0], num_device_parameters1);
if (err_descriptor != ESP_OK) {
ESP_LOGE("TAG", "Error mbc_master_set_descriptor: %s", esp_err_to_name(err_descriptor));
} else {
ESP_LOGE("TAG", "mbc_master_set_descriptor OK: %s", esp_err_to_name(err_descriptor));
}
esp_err_t err_start = mbc_master_start();
if (err_start != ESP_OK) {
ESP_LOGE("TAG", "Error mbc_master_start: %s", esp_err_to_name(err_start));
} else {
ESP_LOGE("TAG", "mbc_master_start OK: %s", esp_err_to_name(err_start));
}
[/Codebox]
I used "set(EXCLUDE_COMPONENTS "freemodbus")" on the project's CmakeLists.txt (I don't know if the order in that file is important.
The esp-modbus version is 1.0.17 (to make it work on ESP-IDF 4.3 and 4.4.5).
My sdkconfig related to the modbus configurated is:
#
# Modbus configuration
#
CONFIG_FMB_COMM_MODE_TCP_EN=y
CONFIG_FMB_TCP_PORT_DEFAULT=502
CONFIG_FMB_TCP_PORT_MAX_CONN=5
CONFIG_FMB_TCP_CONNECTION_TOUT_SEC=60
CONFIG_FMB_COMM_MODE_RTU_EN=y
CONFIG_FMB_COMM_MODE_ASCII_EN=y
CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND=1500
CONFIG_FMB_MASTER_DELAY_MS_CONVERT=200
CONFIG_FMB_QUEUE_LENGTH=20
CONFIG_FMB_PORT_TASK_STACK_SIZE=4096
CONFIG_FMB_SERIAL_BUF_SIZE=256
CONFIG_FMB_SERIAL_ASCII_BITS_PER_SYMB=8
CONFIG_FMB_SERIAL_ASCII_TIMEOUT_RESPOND_MS=1000
CONFIG_FMB_PORT_TASK_PRIO=10
# CONFIG_FMB_PORT_TASK_AFFINITY_NO_AFFINITY is not set
CONFIG_FMB_PORT_TASK_AFFINITY_CPU0=y
# CONFIG_FMB_PORT_TASK_AFFINITY_CPU1 is not set
CONFIG_FMB_PORT_TASK_AFFINITY=0x0
CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT=y
CONFIG_FMB_CONTROLLER_SLAVE_ID=0x00112233
CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT=20
CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE=20
CONFIG_FMB_CONTROLLER_STACK_SIZE=4096
CONFIG_FMB_EVENT_QUEUE_TIMEOUT=20
CONFIG_FMB_TIMER_PORT_ENABLED=y
# CONFIG_FMB_TIMER_USE_ISR_DISPATCH_METHOD is not set
# end of Modbus configuration