I am new in esp-idf libraries. I am working on a gateway project which will communicate modbus slave devices over TCP + RTU both. I need to run RTU + TCP modbus master at the same time on my device. I tried to merge Serial master and TCP master projects.This is my master init code:
- // Modbus master initialization
- static esp_err_t master_init(mb_communication_info_t* comm_info)
- {
- void* master_handler = NULL;
- /*===================MODBUS MASTER TCP=================================*/
- 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*)eth_netif;
- /*===================MODBUS MASTER RTU=================================*/
- comm_info->port = 2;
- comm_info->mode = MB_MODE_RTU;
- comm_info->baudrate = 115200;
- comm_info->parity = MB_PARITY_NONE;
- /*===================MODBUS MASTER TCP INIT()=================================*/
- esp_err_t err = mbc_master_init_tcp(&master_handler);
- ESP_RETURN_ON_FALSE((master_handler != NULL), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller initialization fail.");
- ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller initialization fail, returns(0x%x).",
- (uint32_t)err);
- /*===================MODBUS MASTER RTU INIT()=================================*/
- err = mbc_master_init(MB_PORT_SERIAL_MASTER, &master_handler);
- ESP_RETURN_ON_FALSE((master_handler != NULL), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller initialization fail.");
- ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller initialization fail, returns(0x%x).",
- (uint32_t)err);
- // Set UART pin numbers
- err = uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
- CONFIG_MB_UART_RTS, UART_PIN_NO_CHANGE);
- err = mbc_master_setup((void*)comm_info);
- ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller setup fail, returns(0x%x).",
- (uint32_t)err);
- err = mbc_master_set_descriptor(&device_parameters[0], num_device_parameters);
- printf("num_device_parameters = %d\n", num_device_parameters);
- ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller set descriptor fail, returns(0x%x).",
- (uint32_t)err);
- debug_print("Modbus master stack initialized...");
- err = mbc_master_start();
- ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
- MASTER_TAG,
- "mb controller start fail, returns(0x%x).",
- (uint32_t)err);
- vTaskDelay(5);
- return err;
- }
- }
If l init RTU after TCP , just RTU master works. If I do the same for TCP, just TCP works. Is there any idea how to use TCP and RTU master both?
Thanks.