forked from espressif/esp-modbus
independent instance command handling using lists, fixes as per review and test
This commit is contained in:
1 parent
ddb731578c
commit
20b3dbaa82
42 files changed
+845
-542
No files matched your search
@@ -20,7 +20,7 @@
|
||||
#define MASTER_MAX_CIDS num_device_parameters
|
||||
|
||||
// Number of reading of parameters from slave
|
||||
#define MASTER_MAX_RETRY 30
|
||||
#define MASTER_MAX_RETRY (10)
|
||||
|
||||
// Timeout to update cid over Modbus
|
||||
#define UPDATE_CIDS_TIMEOUT_MS (500)
|
||||
@@ -56,7 +56,7 @@
|
||||
#define OPTS(min_val, max_val, step_val) { .opt1 = min_val, .opt2 = max_val, .opt3 = step_val }
|
||||
|
||||
#define EACH_ITEM(array, length) \
|
||||
(typeof(*(array)) *pitem = (array); (pitem < &((array)[length])); pitem++)
|
||||
(typeof(*(array)) *pitem = (array); (pitem < &((array)[length])); pitem++)
|
||||
|
||||
#define MB_CUST_DATA_LEN 100 // The length of custom command buffer
|
||||
|
||||
@@ -305,19 +305,6 @@ static void *master_get_param_data(const mb_parameter_descriptor_t *param_descri
|
||||
} \
|
||||
))
|
||||
|
||||
mb_exception_t my_custom_handler(void *, uint8_t *frame_ptr, uint16_t *plen)
|
||||
{
|
||||
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen && *plen < (MB_CUST_DATA_LEN - 1)), MB_EX_CRITICAL, TAG,
|
||||
"incorrect custom frame buffer");
|
||||
ESP_LOGW(TAG, "Custom handler, Frame ptr: %p, len: %u", frame_ptr, *plen);
|
||||
// This error handler will be executed to handle the request for the registered custom command
|
||||
// Refer the handler functions in `esp-modbus/modbus/mb_objects/functions/mbfuncinput_master.c` for more information.
|
||||
// Parameters: pframe: is pointer to incoming frame buffer, plen: is pointer to length including the function code
|
||||
strncpy((char *)&my_custom_data[0], (char *)&frame_ptr[1], MB_CUST_DATA_LEN);
|
||||
ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", &my_custom_data[0], (*plen - 1), ESP_LOG_WARN);
|
||||
return MB_EX_NONE;
|
||||
}
|
||||
|
||||
// User operation function to read slave values and check alarm
|
||||
static void master_operation_func(void *arg)
|
||||
{
|
||||
@@ -336,12 +323,14 @@ static void master_operation_func(void *arg)
|
||||
};
|
||||
|
||||
// Send the request with custom command (vendor speciic)
|
||||
// This function supports sending of only even number of bytes
|
||||
// as instructed by req.reg_size (Modbus register = 2 bytes)
|
||||
err = mbc_master_send_request(master_handle, &req, pcustom_string);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE("CUSTOM_DATA", "Send custom request fail.");
|
||||
}
|
||||
|
||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
||||
#if CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
||||
// Command - 17 (0x11) Report Slave ID
|
||||
// The command contains vendor specific data and should be interpreted accordingly.
|
||||
// This version of command handler needs to define the maximum number
|
||||
@@ -349,12 +338,10 @@ static void master_operation_func(void *arg)
|
||||
// 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).
|
||||
req = {
|
||||
.slave_addr = MB_DEVICE_ADDR1, // slave UID to retrieve ID
|
||||
.command = 0x11, // the <Report Slave ID> command,
|
||||
.reg_start = 0, // must be zero,
|
||||
.reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1) // the expected length of buffer in registers
|
||||
};
|
||||
req.slave_addr = MB_DEVICE_ADDR1; // slave UID to retrieve ID
|
||||
req.command = 0x11; // the <Report Slave ID> command,
|
||||
req.reg_start = 0; // must be zero,
|
||||
req.reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1); // the expected length of buffer in registers
|
||||
|
||||
uint8_t info_buf[CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE] = {0}; // The buffer to save slave ID
|
||||
|
||||
@@ -498,6 +485,21 @@ static void master_operation_func(void *arg)
|
||||
ESP_ERROR_CHECK(mbc_master_delete(master_handle));
|
||||
}
|
||||
|
||||
// This is the custom function handler for the command.
|
||||
// The handler is executed from the context of modbus controller event task and should be as simple as possible.
|
||||
// Parameters: frame_ptr - the pointer to the incoming ADU frame from slave starting from function code,
|
||||
// plen - the pointer to length of the frame. After return from the handler the modbus object will
|
||||
// handle the end of transaction according to the exception returned.
|
||||
mb_exception_t my_custom_handler(void *inst, uint8_t *frame_ptr, uint16_t *plen)
|
||||
{
|
||||
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen && *plen < (MB_CUST_DATA_LEN - 1)), MB_EX_ILLEGAL_DATA_VALUE, TAG,
|
||||
"incorrect custom frame buffer");
|
||||
ESP_LOGD(TAG, "Custom handler, Frame ptr: %p, len: %u", frame_ptr, *plen);
|
||||
strncpy((char *)&my_custom_data[0], (char *)&frame_ptr[1], MB_CUST_DATA_LEN);
|
||||
ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", &my_custom_data[0], (*plen - 1), ESP_LOG_WARN);
|
||||
return MB_EX_NONE;
|
||||
}
|
||||
|
||||
// Modbus master initialization
|
||||
static esp_err_t master_init(void)
|
||||
{
|
||||
@@ -523,15 +525,16 @@ static esp_err_t master_init(void)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb controller initialization fail, returns(0x%x).", (int)err);
|
||||
|
||||
uint8_t override_command = 0x41;
|
||||
err = mbc_master_set_handler(master_handle, override_command, NULL);
|
||||
const uint8_t override_command = 0x41;
|
||||
// Delete the handler for specified command, if available
|
||||
err = mbc_delete_handler(master_handle, override_command);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_INVALID_STATE), ESP_ERR_INVALID_STATE, TAG,
|
||||
"could not override handler, returned (0x%x).", (int)err);
|
||||
err = mbc_master_set_handler(master_handle, override_command, my_custom_handler);
|
||||
err = mbc_set_handler(master_handle, override_command, my_custom_handler);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"could not override handler, returned (0x%x).", (int)err);
|
||||
mb_fn_handler_fp phandler = NULL;
|
||||
err = mbc_master_get_handler(master_handle, override_command, &phandler);
|
||||
err = mbc_get_handler(master_handle, override_command, &phandler);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK && phandler == my_custom_handler), ESP_ERR_INVALID_STATE, TAG,
|
||||
"could not get handler for command %d, returned (0x%x).", (int)override_command, (int)err);
|
||||
|
||||
|
||||
@@ -145,20 +145,22 @@ static void setup_reg_data(void)
|
||||
input_reg_params.input_data7 = 4.78;
|
||||
}
|
||||
|
||||
// This is a simple custom function handler for the command.
|
||||
// The handler is executed from the context of modbus controller event task and should be as simple as possible.
|
||||
// Parameters: frame_ptr - the pointer to the incoming ADU request frame from master starting from function code,
|
||||
// plen - the pointer to length of the frame. The handler body can override the buffer and return the length of data.
|
||||
// After return from the handler the modbus object will handle the end of transaction according to the exception returned,
|
||||
// then builds the response frame and send it back to the master. If the whole transaction time including the response
|
||||
// latency exceeds the configured slave response time set in the master configuration the master will ignore the transaction.
|
||||
mb_exception_t my_custom_fc_handler(void *pinst, uint8_t *frame_ptr, uint16_t *plen)
|
||||
{
|
||||
char *str_append = ":Slave";
|
||||
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen < (MB_CUST_DATA_MAX_LEN - strlen(str_append))), MB_EX_ILLEGAL_DATA_VALUE, TAG,
|
||||
"incorrect custom frame");
|
||||
//ESP_LOGW("CUSTOM_DATA", "Custom handler, frame ptr: %p, len: %u", frame_ptr, *plen);
|
||||
//ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", frame_ptr, *plen, ESP_LOG_WARN);
|
||||
// This command handler will be executed to check the request for the custom command
|
||||
// See the `esp-modbus/modbus/mb_objects/functions/functions/mbfuncinput.c` for more information
|
||||
// pframe is pointer to the buffer starting from function code, plen - is pointer to length of the data
|
||||
frame_ptr[*plen] = '\0';
|
||||
strcat((char *)&frame_ptr[1], str_append);
|
||||
*plen = (strlen(str_append) + *plen); // the length of (response + command)
|
||||
return MB_EX_NONE; // Set the exception code for slave appropriately
|
||||
return MB_EX_NONE; // Set the exception code for modbus object appropriately
|
||||
}
|
||||
|
||||
// An example application of Modbus slave. It is based on esp-modbus stack.
|
||||
@@ -190,15 +192,16 @@ void app_main(void)
|
||||
|
||||
ESP_ERROR_CHECK(mbc_slave_create_serial(&comm_config, &mbc_slave_handle)); // Initialization of Modbus controller
|
||||
|
||||
uint8_t custom_command = 0x41; // The custom command to be sent to slave
|
||||
esp_err_t err = mbc_slave_set_handler(mbc_slave_handle, custom_command, NULL);
|
||||
const uint8_t custom_command = 0x41; // The custom command to be sent to slave
|
||||
// Try to delete the handler for specified command.
|
||||
esp_err_t err = mbc_delete_handler(mbc_slave_handle, custom_command);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_INVALID_STATE), ;, TAG,
|
||||
"could not reset handler, returned (0x%x).", (int)err);
|
||||
err = mbc_slave_set_handler(mbc_slave_handle, custom_command, my_custom_fc_handler);
|
||||
"could not delete handler, returned (0x%x).", (int)err);
|
||||
err = mbc_set_handler(mbc_slave_handle, custom_command, my_custom_fc_handler);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ;, TAG,
|
||||
"could not set or override handler, returned (0x%x).", (int)err);
|
||||
mb_fn_handler_fp phandler = NULL;
|
||||
err = mbc_slave_get_handler(mbc_slave_handle, custom_command, &phandler);
|
||||
err = mbc_get_handler(mbc_slave_handle, custom_command, &phandler);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK && phandler == my_custom_fc_handler), ;, TAG,
|
||||
"could not get handler for command %d, returned (0x%x).", (int)custom_command, (int)err);
|
||||
|
||||
|
||||
Reference in new issue
Block a user