mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-02 21:48:13 +00:00
freemodbus: check/fix reinitialization issues (tcp master and slave examples)
This commit is contained in:
@@ -365,6 +365,7 @@ static void master_destroy_slave_list(char** table)
|
||||
for (int i = 0; ((i < MB_DEVICE_COUNT) && table[i] != NULL); i++) {
|
||||
if (table[i]) {
|
||||
free(table[i]);
|
||||
table[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -500,48 +501,47 @@ static void master_operation_func(void *arg)
|
||||
}
|
||||
ESP_LOGI(MASTER_TAG, "Destroy master...");
|
||||
vTaskDelay(100);
|
||||
ESP_ERROR_CHECK(mbc_master_destroy());
|
||||
}
|
||||
|
||||
// Modbus master initialization
|
||||
static esp_err_t master_init(void)
|
||||
static esp_err_t init_services(mb_tcp_addr_type_t ip_addr_type)
|
||||
{
|
||||
esp_err_t result = nvs_flash_init();
|
||||
if (result == ESP_ERR_NVS_NO_FREE_PAGES || result == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
result = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(result);
|
||||
esp_netif_init();
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
MASTER_CHECK((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"nvs_flash_init fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
result = esp_netif_init();
|
||||
MASTER_CHECK((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"esp_netif_init fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
result = esp_event_loop_create_default();
|
||||
MASTER_CHECK((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"esp_event_loop_create_default fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
// Start mdns service and register device
|
||||
master_start_mdns_service();
|
||||
#endif
|
||||
|
||||
// This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||
// Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||
// examples/protocols/README.md for more information about this function.
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
|
||||
|
||||
mb_communication_info_t comm_info = { 0 };
|
||||
comm_info.ip_port = MB_TCP_PORT;
|
||||
#if !CONFIG_EXAMPLE_CONNECT_IPV6
|
||||
comm_info.ip_addr_type = MB_IPV4;
|
||||
#else
|
||||
comm_info.ip_addr_type = MB_IPV6;
|
||||
result = example_connect();
|
||||
MASTER_CHECK((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"example_connect fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
||||
result = esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
MASTER_CHECK((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"esp_wifi_set_ps fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
#endif
|
||||
comm_info.ip_mode = MB_MODE_TCP;
|
||||
comm_info.ip_addr = (void*)slave_ip_address_table;
|
||||
comm_info.ip_netif_ptr = (void*)get_example_netif();
|
||||
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
int res = 0;
|
||||
for (int retry = 0; (res < num_device_parameters) && (retry < 10); retry++) {
|
||||
res = master_query_slave_service("_modbus", "_tcp", comm_info.ip_addr_type);
|
||||
res = master_query_slave_service("_modbus", "_tcp", ip_addr_type);
|
||||
}
|
||||
if (res < num_device_parameters) {
|
||||
ESP_LOGE(MASTER_TAG, "Could not resolve one or more slave IP addresses, resolved: %d out of %d.", res, num_device_parameters );
|
||||
@@ -555,9 +555,40 @@ static esp_err_t master_init(void)
|
||||
ESP_LOGI(MASTER_TAG, "Configured %d IP addresse(s).", ip_cnt);
|
||||
} else {
|
||||
ESP_LOGE(MASTER_TAG, "Fail to get IP address from stdin. Continue.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t destroy_services(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
master_destroy_slave_list(slave_ip_address_table);
|
||||
#endif
|
||||
err = example_disconnect();
|
||||
MASTER_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"example_disconnect fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
err = esp_event_loop_delete_default();
|
||||
MASTER_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"esp_event_loop_delete_default fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
err = esp_netif_deinit();
|
||||
MASTER_CHECK((err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED), ESP_ERR_INVALID_STATE,
|
||||
"esp_netif_deinit fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
err = nvs_flash_deinit();
|
||||
MASTER_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"nvs_flash_deinit fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Modbus master initialization
|
||||
static esp_err_t master_init(mb_communication_info_t* comm_info)
|
||||
{
|
||||
void* master_handler = NULL;
|
||||
|
||||
esp_err_t err = mbc_master_init_tcp(&master_handler);
|
||||
@@ -567,7 +598,7 @@ static esp_err_t master_init(void)
|
||||
"mb controller initialization fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
|
||||
err = mbc_master_setup((void*)&comm_info);
|
||||
err = mbc_master_setup((void*)comm_info);
|
||||
MASTER_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"mb controller setup fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
@@ -586,15 +617,37 @@ static esp_err_t master_init(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t master_destroy(void)
|
||||
{
|
||||
esp_err_t err = mbc_master_destroy();
|
||||
MASTER_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
"mbc_master_destroy fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
ESP_LOGI(MASTER_TAG, "Modbus master stack destroy...");
|
||||
return err;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// Initialization of device peripheral and objects
|
||||
ESP_ERROR_CHECK(master_init());
|
||||
vTaskDelay(10);
|
||||
mb_tcp_addr_type_t ip_addr_type;
|
||||
#if !CONFIG_EXAMPLE_CONNECT_IPV6
|
||||
ip_addr_type = MB_IPV4;
|
||||
#else
|
||||
ip_addr_type = MB_IPV6;
|
||||
#endif
|
||||
ESP_ERROR_CHECK(init_services(ip_addr_type));
|
||||
|
||||
mb_communication_info_t comm_info = { 0 };
|
||||
comm_info.ip_port = MB_TCP_PORT;
|
||||
comm_info.ip_addr_type = ip_addr_type;
|
||||
comm_info.ip_mode = MB_MODE_TCP;
|
||||
comm_info.ip_addr = (void*)slave_ip_address_table;
|
||||
comm_info.ip_netif_ptr = (void*)get_example_netif();
|
||||
|
||||
ESP_ERROR_CHECK(master_init(&comm_info));
|
||||
vTaskDelay(50);
|
||||
|
||||
master_operation_func(NULL);
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
master_destroy_slave_list(slave_ip_address_table);
|
||||
#endif
|
||||
|
||||
ESP_ERROR_CHECK(master_destroy());
|
||||
ESP_ERROR_CHECK(destroy_services());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user