add the command 0x11 - get slave info

This commit is contained in:
Alex Lisitsyn
2024-12-06 19:25:22 +08:00
parent 5a36ece1a0
commit db57bd6251
12 changed files with 185 additions and 28 deletions
@@ -305,6 +305,26 @@ static void master_operation_func(void *arg)
ESP_LOGI(TAG, "Start modbus test...");
// Command - 17 (0x11) Report Slave ID (Serial Line only)
// The command contains vendor specific data and should be interpreted accordingly.
// This version of command handler needs to define the maximum number
// of registers that can be returned from concrete slave (buffer size).
// The returned slave info data will be stored into the `info_buf`.
// Request fields: slave_addr - the UID of slave, reg_start - not used,
// reg_size = max size of buffer (registers).
mb_param_request_t req = {.slave_addr = 1, .command = 0x11,
.reg_start = 0, .reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1)};
uint8_t info_buf[CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE] = {0};
// This is the way to retrieve slave ID information from slave (vendor specific command = 0x11).
err = mbc_master_send_request(&req, &info_buf[0]);
if (err != ESP_OK) {
ESP_LOGE("SLAVE_INFO", "Read slave info fail.");
} else {
ESP_LOG_BUFFER_HEX_LEVEL("SLAVE_INFO", (void*)info_buf, sizeof(info_buf), ESP_LOG_WARN);
}
for(uint16_t retry = 0; retry <= MASTER_MAX_RETRY && (!alarm_state); retry++) {
// Read all found characteristics from slave(s)
for (uint16_t cid = 0; (err != ESP_ERR_NOT_FOUND) && cid < MASTER_MAX_CIDS; cid++) {
+13
View File
@@ -126,6 +126,10 @@ static void setup_reg_data(void)
input_reg_params.input_data7 = 4.78;
}
// This is the way to expose the funcion to set slave ID .
// The related function is vendor specific and stack by default hides this functionality from user.
extern int eMBSetSlaveID(uint8_t slave_id, bool is_running, uint8_t const *pdata, uint16_t data_len);
// An example application of Modbus slave. It is based on freemodbus stack.
// See deviceparams.h file for more information about assigned Modbus parameters.
// These parameters can be accessed from main application and also can be changed
@@ -226,6 +230,15 @@ void app_main(void)
ESP_LOGI(TAG, "Modbus slave stack initialized.");
ESP_LOGI(TAG, "Start modbus test...");
uint8_t ext_data[] = {0x11, 0x22, 0x33, 0x44, 0x55};
// This is the way to set Slave ID fields to retrieve it by master using report slave ID command.
int err = eMBSetSlaveID(comm_info.slave_addr, true, (uint8_t *)&ext_data, sizeof(ext_data));
if (!err) {
ESP_LOG_BUFFER_HEX_LEVEL("SET_SLAVE_ID", (void*)ext_data, sizeof(ext_data), ESP_LOG_WARN);
} else {
ESP_LOGE("SET_SLAVE_ID", "Set slave ID fail, err=%d.", err);
}
// The cycle below will be terminated when parameter holdingRegParams.dataChan0
// incremented each access cycle reaches the CHAN_DATA_MAX_VAL value.
for(;holding_reg_params.holding_data0 < MB_CHAN_DATA_MAX_VAL;) {