fix the custom commands support

This commit is contained in:
aleks authored and Alex Lisitsyn committed 2025-06-09 08:02:24 +01:00
1 parent 38205ca78f
commit 37c750ec35
6 files changed
+68 -26

No files matched your search

+13 -18
View File
@@ -8,7 +8,8 @@
#include "esp_modbus_master.h" // for public interface defines
// Helper macro to set custom command
#define SET_CMD(mode, rd_cmd, wr_cmd) ((mode == MB_PARAM_WRITE) ? wr_cmd : (mode == MB_PARAM_READ) ? rd_cmd : 0)
#define GET_CMD(mode, access, rd_cmd, wr_cmd) (((mode == MB_PARAM_WRITE) && (access & PAR_PERMS_WRITE)) ? wr_cmd : \
((mode == MB_PARAM_READ) && (access & PAR_PERMS_READ)) ? rd_cmd : 0)
static const char TAG[] __attribute__((unused)) = "MB_CONTROLLER_MASTER";
@@ -737,6 +738,8 @@ esp_err_t mbc_master_set_param_data(void* dest, void* src, mb_descr_type_t param
// Helper function to get configured Modbus command for each type of Modbus register area.
// Supports custom command options using the PAR_PERMS_CUST_CMD permission.
// The MB_PARAM_CUSTOM register type mimics the custom commands specificly handled with
// custom command handlers which have to be defined in command handling table.
uint8_t mbc_master_get_command(const mb_parameter_descriptor_t *pdescr, mb_param_mode_t mode)
{
MB_RETURN_ON_FALSE((pdescr), 0, TAG, "incorrect data pointer.");
@@ -744,31 +747,23 @@ uint8_t mbc_master_get_command(const mb_parameter_descriptor_t *pdescr, mb_param
switch(pdescr->mb_param_type)
{
case MB_PARAM_HOLDING:
if (pdescr->access & PAR_PERMS_CUST_CMD) {
command = SET_CMD(mode, (uint8_t)pdescr->param_opts.cust_cmd_read, (uint8_t)pdescr->param_opts.cust_cmd_write);
} else {
command = SET_CMD(mode, MB_FUNC_READ_HOLDING_REGISTER, MB_FUNC_WRITE_MULTIPLE_REGISTERS);
}
command = GET_CMD(mode, pdescr->access, MB_FUNC_READ_HOLDING_REGISTER, MB_FUNC_WRITE_MULTIPLE_REGISTERS);
break;
case MB_PARAM_INPUT:
if (pdescr->access & PAR_PERMS_CUST_CMD) {
command = SET_CMD(mode, (uint8_t)pdescr->param_opts.cust_cmd_read, 0);
} else {
command = SET_CMD(mode, MB_FUNC_READ_INPUT_REGISTER, 0);
}
command = GET_CMD(mode, pdescr->access, MB_FUNC_READ_INPUT_REGISTER, 0);
break;
case MB_PARAM_COIL:
if (pdescr->access & PAR_PERMS_CUST_CMD) {
command = SET_CMD(mode, (uint8_t)pdescr->param_opts.cust_cmd_read, (uint8_t)pdescr->param_opts.cust_cmd_write);
} else {
command = SET_CMD(mode, MB_FUNC_READ_COILS, MB_FUNC_WRITE_MULTIPLE_COILS);
}
command = GET_CMD(mode, pdescr->access, MB_FUNC_READ_COILS, MB_FUNC_WRITE_MULTIPLE_COILS);
break;
case MB_PARAM_DISCRETE:
command = GET_CMD(mode, pdescr->access, MB_FUNC_READ_DISCRETE_INPUTS, 0);
break;
case MB_PARAM_CUSTOM:
if (pdescr->access & PAR_PERMS_CUST_CMD) {
command = SET_CMD(mode, (uint8_t)pdescr->param_opts.cust_cmd_read, 0);
// Use custom command in the request for read or write
command = GET_CMD(mode, pdescr->access, (uint8_t)pdescr->param_opts.cust_cmd_read, (uint8_t)pdescr->param_opts.cust_cmd_write);
} else {
command = SET_CMD(mode, MB_FUNC_READ_DISCRETE_INPUTS, 0);
command = 0;
}
break;
default:
@@ -150,11 +150,12 @@ typedef enum
* @brief Type of Modbus parameter
*/
typedef enum {
MB_PARAM_HOLDING = 0x00, /*!< Modbus Holding register. */
MB_PARAM_INPUT, /*!< Modbus Input register. */
MB_PARAM_COIL, /*!< Modbus Coils. */
MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
MB_PARAM_HOLDING = 0x00, /*!< Modbus Holding register. */
MB_PARAM_INPUT, /*!< Modbus Input register. */
MB_PARAM_COIL, /*!< Modbus Coils. */
MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
MB_PARAM_COUNT,
MB_PARAM_CUSTOM, /*!< Modbus custom commands (is not counted in area descriptors). */
MB_PARAM_UNKNOWN = 0xFF
} mb_param_type_t;
@@ -71,7 +71,7 @@ typedef union {
struct {
int cust_cmd_read; /*!< Parameter custom read request command */
int cust_cmd_write; /*!< Parameter custom write request command */
int cust_cmd_not_used; /*!< Not used option for custom request */
int cust_cmd_other; /*!< Optional field for custom request */
};
} mb_parameter_opt_t;
@@ -179,7 +179,7 @@ typedef struct {
*/
typedef struct {
uint8_t slave_addr; /*!< Modbus slave address */
uint8_t command; /*!< Modbus command to send */
uint8_t command; /*!< Modbus command to send */
uint16_t reg_start; /*!< Modbus start register */
uint16_t reg_size; /*!< Modbus number of registers */
} mb_param_request_t;
@@ -482,6 +482,20 @@ mb_err_enum_t mbc_reg_coils_master_cb(mb_base_t *inst, uint8_t *reg_buffer, uint
*/
esp_err_t mbc_master_set_param_data(void* dest, void* src, mb_descr_type_t param_type, size_t param_size);
/**
* @brief The helper function to get supported modbus function code (command) according to parameter type
*
* @param[in] pdescr the pointer to the characteristic descriptor in data dictionary
* @param[in] mode access mode for characteristic
*
* @return
* - modbus function code, if the command is correctly configured
* - 0 - invalid argument or command not found
*/
uint8_t mbc_master_get_command(const mb_parameter_descriptor_t *pdescr, mb_param_mode_t mode);
#ifdef __cplusplus
}
#endif