Chapter 2. API Reference
Table 9: Table 3 Modbus register area descriptor
Field Description
start_offsetZero based register relative offset for defined register area. Example: register address = 40002 ( 4x
register area - Function 3 - holding register ), start_offset = 2
type Type of the Modbus register area. Refer to mb_param_type_t for more information.
addressA pointer to the memory area which is used to store the register data for this area descriptor.
size The size of the memory area in bytes which is used to store register data.
mbc_slave_set_descriptor()
The function initializes Modbus communication descriptors for each type of Modbus register area (Holding
Registers, Input Registers, Coils (single bit output), Discrete Inputs). Once areas are initialized and the
mbc_slave_start() API is called the Modbus stack can access the data in user data structures by request
from master.
#define MB_REG_INPUT_START_AREA0 (0)
#define MB_REG_HOLDING_START_AREA0 (0)
#define MB_REG_HOLD_CNT (100)
#define MB_REG_INPUT_CNT (100)
mb_register_area_descriptor_t reg_area; // Modbus register area descriptor␣
,→structure
unit16_t holding_reg_area[MB_REG_HOLD_CNT] = {0}; // storage area for holding␣
,→registers
unit16_t input_reg_area[MB_REG_INPUT_CNT] = {0}; // storage area for input␣
,→registers
reg_area.type = MB_PARAM_HOLDING; // Set type of␣
,→register area
reg_area.start_offset = MB_REG_HOLDING_START_AREA0; // Offset of␣
,→register area in Modbus protocol
reg_area.address = (void*)&holding_reg_area[0]; // Set pointer to␣
,→storage instance
reg_area.size = sizeof(holding_reg_area) << 1; // Set the size of␣
,→register storage area in bytes
ESP_ERROR_CHECK(mbc_slave_set_descriptor(reg_area));
reg_area.type = MB_PARAM_INPUT;
reg_area.start_offset = MB_REG_INPUT_START_AREA0;
reg_area.address = (void*)&input_reg_area[0];
reg_area.size = sizeof(input_reg_area) << 1;
ESP_ERROR_CHECK(mbc_slave_set_descriptor(reg_area));
At least one area descriptor per each Modbus register type must be set in order to provide register access to its area.
If the master tries to access an undefined area, the stack will generate a Modbus exception.
Direct access to register area from user application must be protected by critical section:
portENTER_CRITICAL(¶m_lock);
holding_reg_area[2] += 10;
portEXIT_CRITICAL(¶m_lock);
Slave Communication Options The function initializes the Modbus controller interface and its active context
(tasks, RTOS objects and other resources).
mbc_slave_setup()
The function is used to setup communication parameters of the Modbus stack.
Example initialization of Modbus TCP communication:
Espressif Systems 629
Submit Document Feedback
Release v4.4