5858#define EACH_ITEM (array , length ) \
5959(typeof(*(array)) *pitem = (array); (pitem < &((array)[length])); pitem++)
6060
61+ #define MB_CUST_DATA_LEN 100 // The length of custom command buffer
62+
6163static const char * TAG = "MASTER_TEST" ;
6264
6365// Enumeration of modbus device addresses accessed by master device
@@ -217,6 +219,8 @@ const mb_parameter_descriptor_t device_parameters[] = {
217219// Calculate number of parameters in the table
218220const uint16_t num_device_parameters = (sizeof (device_parameters )/sizeof (device_parameters [0 ]));
219221
222+ static char my_custom_data [MB_CUST_DATA_LEN ] = {0 }; // the custom data buffer
223+
220224static void * master_handle = NULL ;
221225
222226// The function to get pointer to parameter storage (instance) according to parameter description table
@@ -301,6 +305,19 @@ static void *master_get_param_data(const mb_parameter_descriptor_t *param_descri
301305} \
302306))
303307
308+ mb_exception_t my_custom_handler (void * , uint8_t * frame_ptr , uint16_t * plen )
309+ {
310+ MB_RETURN_ON_FALSE ((frame_ptr && plen && * plen && * plen < (MB_CUST_DATA_LEN - 1 )), MB_EX_CRITICAL , TAG ,
311+ "incorrect custom frame buffer" );
312+ ESP_LOGW (TAG , "Custom handler, Frame ptr: %p, len: %u" , frame_ptr , * plen );
313+ // This error handler will be executed to handle the request for the registered custom command
314+ // Refer the handler functions in `esp-modbus/modbus/mb_objects/functions/mbfuncinput_master.c` for more information.
315+ // Parameters: pframe: is pointer to incoming frame buffer, plen: is pointer to length including the function code
316+ strncpy ((char * )& my_custom_data [0 ], (char * )& frame_ptr [1 ], MB_CUST_DATA_LEN );
317+ ESP_LOG_BUFFER_HEXDUMP ("CUSTOM_DATA" , & my_custom_data [0 ], (* plen - 1 ), ESP_LOG_WARN );
318+ return MB_EX_NONE ;
319+ }
320+
304321// User operation function to read slave values and check alarm
305322static void master_operation_func (void * arg )
306323{
@@ -309,6 +326,20 @@ static void master_operation_func(void *arg)
309326 const mb_parameter_descriptor_t * param_descriptor = NULL ;
310327
311328 ESP_LOGI (TAG , "Start modbus test..." );
329+
330+ char * pcustom_string = "Master" ;
331+ mb_param_request_t req = {
332+ .slave_addr = MB_DEVICE_ADDR1 , // the slave UID to send the request
333+ .command = 0x41 , // the custom function code,
334+ .reg_start = 0 , // unused,
335+ .reg_size = (strlen (pcustom_string ) >> 1 ) // length of the data to send (registers)
336+ };
337+
338+ // Send the request with custom command (vendor speciic)
339+ err = mbc_master_send_request (master_handle , & req , pcustom_string );
340+ if (err != ESP_OK ) {
341+ ESP_LOGE ("CUSTOM_DATA" , "Send custom request fail." );
342+ }
312343
313344#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
314345 // Command - 17 (0x11) Report Slave ID
@@ -318,11 +349,11 @@ static void master_operation_func(void *arg)
318349 // The returned slave info data will be stored into the `info_buf`.
319350 // Request fields: slave_addr - the UID of slave, reg_start - not used,
320351 // reg_size = max size of buffer (registers).
321- mb_param_request_t req = {
322- .slave_addr = MB_DEVICE_ADDR1 , // slave UID to retrieve ID
323- .command = 0x11 , // the <Report Slave ID> command,
324- .reg_start = 0 , // must be zero,
325- .reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1 ) // the expected length of buffer in registers
352+ req = {
353+ .slave_addr = MB_DEVICE_ADDR1 , // slave UID to retrieve ID
354+ .command = 0x11 , // the <Report Slave ID> command,
355+ .reg_start = 0 , // must be zero,
356+ .reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1 ) // the expected length of buffer in registers
326357 };
327358
328359 uint8_t info_buf [CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE ] = {0 }; // The buffer to save slave ID
@@ -492,6 +523,18 @@ static esp_err_t master_init(void)
492523 MB_RETURN_ON_FALSE ((err == ESP_OK ), ESP_ERR_INVALID_STATE , TAG ,
493524 "mb controller initialization fail, returns(0x%x)." , (int )err );
494525
526+ uint8_t override_command = 0x41 ;
527+ err = mbc_master_set_handler (master_handle , override_command , NULL );
528+ MB_RETURN_ON_FALSE ((err == ESP_OK || err == ESP_ERR_INVALID_STATE ), ESP_ERR_INVALID_STATE , TAG ,
529+ "could not override handler, returned (0x%x)." , (int )err );
530+ err = mbc_master_set_handler (master_handle , override_command , my_custom_handler );
531+ MB_RETURN_ON_FALSE ((err == ESP_OK ), ESP_ERR_INVALID_STATE , TAG ,
532+ "could not override handler, returned (0x%x)." , (int )err );
533+ mb_fn_handler_fp phandler = NULL ;
534+ err = mbc_master_get_handler (master_handle , override_command , & phandler );
535+ MB_RETURN_ON_FALSE ((err == ESP_OK && phandler == my_custom_handler ), ESP_ERR_INVALID_STATE , TAG ,
536+ "could not get handler for command %d, returned (0x%x)." , (int )override_command , (int )err );
537+
495538 // Set UART pin numbers
496539 err = uart_set_pin (MB_PORT_NUM , CONFIG_MB_UART_TXD , CONFIG_MB_UART_RXD ,
497540 CONFIG_MB_UART_RTS , UART_PIN_NO_CHANGE );
0 commit comments