Modbus RTU + TCP Master

officializm
Posts: 2
Joined: Fri Jan 21, 2022 6:25 pm

Modbus RTU + TCP Master

Postby officializm » Fri Jan 21, 2022 6:52 pm

Hello All,
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:
  1. // Modbus master initialization
  2. static esp_err_t master_init(mb_communication_info_t* comm_info)
  3. {
  4.     void* master_handler = NULL;
  5.  
  6.  
  7.  
  8. /*===================MODBUS MASTER TCP=================================*/
  9.     comm_info->ip_port = MB_TCP_PORT;
  10.     comm_info->ip_addr_type = ip_addr_type;
  11.     comm_info->ip_mode = MB_MODE_TCP;
  12.     comm_info->ip_addr = (void*)slave_ip_address_table;
  13.     comm_info->ip_netif_ptr = (void*)eth_netif;
  14.  
  15. /*===================MODBUS MASTER RTU=================================*/
  16.     comm_info->port = 2;
  17.     comm_info->mode = MB_MODE_RTU;
  18.     comm_info->baudrate = 115200;
  19.     comm_info->parity = MB_PARITY_NONE;
  20.  
  21.  
  22.  
  23. /*===================MODBUS MASTER TCP INIT()=================================*/
  24.     esp_err_t err = mbc_master_init_tcp(&master_handler);
  25.     ESP_RETURN_ON_FALSE((master_handler != NULL), ESP_ERR_INVALID_STATE,
  26.                                 MASTER_TAG,
  27.                                 "mb controller initialization fail.");
  28.     ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
  29.                             MASTER_TAG,
  30.                             "mb controller initialization fail, returns(0x%x).",
  31.                             (uint32_t)err);
  32.  
  33.  
  34.  
  35.  
  36. /*===================MODBUS MASTER RTU INIT()=================================*/
  37.     err = mbc_master_init(MB_PORT_SERIAL_MASTER, &master_handler);
  38.     ESP_RETURN_ON_FALSE((master_handler != NULL), ESP_ERR_INVALID_STATE,
  39.                                 MASTER_TAG,
  40.                                 "mb controller initialization fail.");
  41.     ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
  42.                             MASTER_TAG,
  43.                             "mb controller initialization fail, returns(0x%x).",
  44.                             (uint32_t)err);
  45.  
  46.  
  47.     // Set UART pin numbers
  48.     err = uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
  49.                               CONFIG_MB_UART_RTS, UART_PIN_NO_CHANGE);
  50.  
  51.  
  52.  
  53.     err = mbc_master_setup((void*)comm_info);
  54.     ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
  55.                             MASTER_TAG,
  56.                             "mb controller setup fail, returns(0x%x).",
  57.                             (uint32_t)err);
  58.  
  59.     err = mbc_master_set_descriptor(&device_parameters[0], num_device_parameters);
  60.     printf("num_device_parameters = %d\n", num_device_parameters);
  61.     ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
  62.                                 MASTER_TAG,
  63.                                 "mb controller set descriptor fail, returns(0x%x).",
  64.                                 (uint32_t)err);
  65.     debug_print("Modbus master stack initialized...");
  66.  
  67.     err = mbc_master_start();
  68.     ESP_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
  69.                             MASTER_TAG,
  70.                             "mb controller start fail, returns(0x%x).",
  71.                             (uint32_t)err);
  72.     vTaskDelay(5);
  73.     return err;
  74. }
  75. }

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.

ESP_alisitsyn
Posts: 211
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: Modbus RTU + TCP Master

Postby ESP_alisitsyn » Tue Jan 25, 2022 4:26 am

Hello @officializm,

Unfortunately, the current implementation of the ESP_Modbus library does not allow this. It is not possible to install several instances of master or several instances of a slave at the same time because they share the same FSM - final state machine (one master and one slave is allowed at the same time). One simple way is to install master RTU and get data over RTU from slave then destroy and reinstall master in TCP mode. This is not a good and reliable approach. Otherwise, you need a custom implementation of it.

officializm
Posts: 2
Joined: Fri Jan 21, 2022 6:25 pm

Re: Modbus RTU + TCP Master

Postby officializm » Thu Jan 27, 2022 6:23 am

Thank you for the information.

SIlvesterrr
Posts: 4
Joined: Sun Jan 23, 2022 4:01 pm

Re: Modbus RTU + TCP Master

Postby SIlvesterrr » Mon Sep 26, 2022 8:20 am

Did this changed? Does version 4.4 support several instances of mb?
I need to have simultaneous modbus tcp slave and modbus rtu slave is that possible?

ESP_alisitsyn
Posts: 211
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: Modbus RTU + TCP Master

Postby ESP_alisitsyn » Wed Nov 30, 2022 8:03 am

This is not possible yet.

Mykyta_Mar
Posts: 4
Joined: Fri Dec 08, 2023 9:28 am

Re: Modbus RTU + TCP Master

Postby Mykyta_Mar » Wed Feb 28, 2024 8:34 am

Hi, will it be fixed, so 2 Master can run at the same time or the continuous initializing and deinitializing of a master runs stable ?
If yes - could someone tell wenn it will approximately be made ?

Who is online

Users browsing this forum: No registered users and 136 guests