Modbus TCP/IP, Slave Adress, IP Adress

mkremer
Posts: 9
Joined: Sun Feb 27, 2022 6:36 pm

Modbus TCP/IP, Slave Adress, IP Adress

Postby mkremer » Wed Jan 04, 2023 9:03 am

Hello,

can someone tell me how I can assign an individual IP address and slave ID to a Modbus slave?

For me, the address is always assigned automatically. In addition, I can use the master to address the slave with any desired slave ID, no matter which one I enter there.

The description for the master states that I can do this with

char* slave_ip_address_table[MB_DEVICE_COUNT] = {
"192.168.1.21", // Address corresponds to MB_DEVICE_ADDR1 and set to predefined value by user
"192.168.1.22", // Address corresponds to MB_DEVICE_ADDR2 of slave device in the Modbus data dictionary
NULL // Marker of end of list
};

But where do I enter that?

As master I use "Simply Modbus TCP"

--------
In "esp_modbus_common.h"

I tried it with

// TCP/UDP communication structure
structure {
mb_mode_type_tip_mode; /*!< Modbus communication mode */
uint16_t ip_port; /*!< Modbus port */
mb_tcp_addr_type_t ip_addr_type; /*!< Modbus address type */
void* ip_addr; /*!< Modbus address table for connection */
void* ip_netif_ptr; /*!< Modbus network interface */
};
} mb_communication_info_t;


But when I write in the app_main()

mb_communication_info_t comm_info = { 0 };
comm_info.ip_addr = "192.168.1.21";

nothing changes. The IP is assigned automatically again and the slave ID still doesn't matter.

Greeting
Michael


PS: everything works fine with Modbus RTU.

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

Re: Modbus TCP/IP, Slave Adress, IP Adress

Postby ESP_alisitsyn » Mon Jan 16, 2023 9:30 am

Hello,

The Modbus uses lwip socket transport and netif interface. So, the netif interface should be configured accordingly prior start of Modbus stack.
The mb_communication_info_t::ip_address is used to bind to this address and useless in this example. NULL - means bind to local address, not NULL - bind to specific address.

If you need to define the static IP address for your slave you can use the code below as an example:

Code: Select all

// Main function of Modbus example
void app_main(void)
{
    ESP_ERROR_CHECK(init_services());

    // Set UART log level
    esp_log_level_set(TAG, ESP_LOG_INFO);

    mb_communication_info_t comm_info = { 0 };
    esp_netif_t *s_example_sta_netif = (void*)get_example_netif();

    ESP_ERROR_CHECK(esp_netif_dhcpc_stop(s_example_sta_netif)); // Disable dhcp to be able to set static IP
    
    // Your local network configuration is defined as below (just an example)
    char* ip= "192.168.88.242"; 			// <<<<<<<<<<<< The static address of the Modbus slave
    char* gateway = "192.168.88.1"; 		// Gateway for your network 
    char* netmask = "255.255.255.0"; 		// Network mask 
    esp_netif_ip_info_t info_t;
    memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
    info_t.ip.addr = esp_ip4addr_aton((const char *)ip);
    info_t.gw.addr = esp_ip4addr_aton((const char *)gateway);
    info_t.netmask.addr = esp_ip4addr_aton((const char *)netmask);
    esp_netif_set_ip_info(s_example_sta_netif, &info_t); 	// Assign static IP to the netif
    
    // ..... the rest of the modbus example code is here   
    
    ;
Similar configuration can be done for the Ehernet interface. Refer to https://docs.espressif.com/projects/esp ... r-s-manual for more information. Let me know if you have more questions.

Brichart
Posts: 2
Joined: Thu Sep 02, 2021 5:41 am

Re: Modbus TCP/IP, Slave Adress, IP Adress

Postby Brichart » Fri Jan 05, 2024 7:13 am

Darn, mine still won't work and ChatGPT is no help (no surprise). Can I post what I have?

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 53 guests