Chapter 2. API Reference
(continued from previous page)
};
ESP_ERROR_CHECK(mbc_master_setup((void*)&comm_info));
Note: Refer to esp_netif component for more information about network interface initialization.
The slave IP addresses in the table can be assigned automatically using mDNS service as described in the example.
Refer to protocols/modbus/tcp/mb_tcp_master for more information.
Note: RS485 communication requires call to UART specific APIs to setup communication mode and pins. Refer
to Running UART Communication section of UART documentation.
Master Communication The starting of the Modbus controller is the final step in enabling communication. This
is performed using function below:
mbc_master_start()
esp_err_t err = mbc_master_start();
if (err != ESP_OK) {
ESP_LOGE(TAG, "mb controller start fail, err=%x.", err);
}
The list of functions below are used by the Modbus master stack from a user’s application:
mbc_master_send_request(): This function executes a blocking Modbus request. The master sends a data
request (as defined in parameter request structure mb_param_request_t) and then blocks until a response from
corresponding slave and returns the status of command execution. This function provides a standard way for read/write
access to Modbus devices in the network.
mbc_master_get_cid_info(): The function gets information about each characteristic supported in the data
dictionary and returns the characteristic’s description in the form of the mb_parameter_descriptor_t
structure. Each characteristic is accessed using its CID.
mbc_master_get_parameter(): The function reads the data of a characteristic defined in the parameters of
a Modbus slave device. The additional data for request is taken from parameter description table.
Example:
const mb_parameter_descriptor_t* param_descriptor = NULL;
uint8_t temp_data[4] = {0}; // temporary buffer to hold maximum CID size
uint8_t type = 0;
....
// Get the information for characteristic cid from data dictionary
esp_err_t err = mbc_master_get_cid_info(cid, ¶m_descriptor);
if ((err != ESP_ERR_NOT_FOUND) && (param_descriptor != NULL)) {
err = mbc_master_get_parameter(param_descriptor->cid, (char*)param_descriptor->
,→param_key, (uint8_t*)temp_data, &type);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%08x) read successful.
,→",
param_descriptor->cid,
(char*)param_descriptor->param_key,
(char*)param_descriptor->param_units,
*(uint32_t*)temp_data);
} else {
ESP_LOGE(TAG, "Characteristic #%d (%s) read fail, err = 0x%x (%s).",
(continues on next page)
Espressif Systems 627
Submit Document Feedback
Release v4.4