2025-03-04 14:39:51 +08:00
|
|
|
|
#include "modbus_params.h" // for modbus parameters structures
|
|
|
|
|
|
#include "mbcontroller.h"
|
2025-03-04 15:21:41 +08:00
|
|
|
|
#include "string.h"
|
|
|
|
|
|
#include "esp_log.h"
|
2025-03-04 14:39:51 +08:00
|
|
|
|
|
2025-03-04 15:21:41 +08:00
|
|
|
|
#define MB_PORT_NUM (UART_NUM_1) // Number of UART port used for Modbus connection
|
|
|
|
|
|
#define MB_DEV_SPEED (9600) // The communication speed of the UART
|
2025-03-04 14:39:51 +08:00
|
|
|
|
|
2025-03-04 15:21:41 +08:00
|
|
|
|
// Note: Some pins on target chip cannot be assigned for UART communication.
|
|
|
|
|
|
// See UART documentation for selected board and target to configure pins using Kconfig.
|
2025-03-04 14:39:51 +08:00
|
|
|
|
|
2025-03-04 15:21:41 +08:00
|
|
|
|
// The number of parameters that intended to be used in the particular control process
|
|
|
|
|
|
#define MASTER_MAX_CIDS num_device_parameters
|
|
|
|
|
|
|
|
|
|
|
|
// Number of reading of parameters from slave
|
2025-03-11 15:15:37 +08:00
|
|
|
|
#define MASTER_MAX_RETRY 3
|
2025-03-04 15:21:41 +08:00
|
|
|
|
|
|
|
|
|
|
// Timeout to update cid over Modbus
|
|
|
|
|
|
#define UPDATE_CIDS_TIMEOUT_MS (500)
|
|
|
|
|
|
#define UPDATE_CIDS_TIMEOUT_TICS (UPDATE_CIDS_TIMEOUT_MS / portTICK_PERIOD_MS)
|
|
|
|
|
|
|
|
|
|
|
|
// Timeout between polls
|
|
|
|
|
|
#define POLL_TIMEOUT_MS (1)
|
|
|
|
|
|
#define POLL_TIMEOUT_TICS (POLL_TIMEOUT_MS / portTICK_PERIOD_MS)
|
|
|
|
|
|
|
|
|
|
|
|
// The macro to get offset for parameter in the appropriate structure
|
|
|
|
|
|
#define HOLD_OFFSET(field) ((uint16_t)(offsetof(holding_reg_params_t, field) + 1))
|
|
|
|
|
|
#define INPUT_OFFSET(field) ((uint16_t)(offsetof(input_reg_params_t, field) + 1))
|
|
|
|
|
|
#define COIL_OFFSET(field) ((uint16_t)(offsetof(coil_reg_params_t, field) + 1))
|
|
|
|
|
|
// Discrete offset macro
|
|
|
|
|
|
#define DISCR_OFFSET(field) ((uint16_t)(offsetof(discrete_reg_params_t, field) + 1))
|
|
|
|
|
|
|
|
|
|
|
|
#define STR(fieldname) ((const char*)( fieldname ))
|
|
|
|
|
|
// Options can be used as bit masks or parameter limits
|
|
|
|
|
|
#define OPTS(min_val, max_val, step_val) { .opt1 = min_val, .opt2 = max_val, .opt3 = step_val }
|
|
|
|
|
|
|
|
|
|
|
|
/*<2A><><EFBFBD><EFBFBD>ͨѶ<CDA8>ӿڶ<D3BF><DAB6><EFBFBD>*/
|
|
|
|
|
|
#define CONFIG_MB_UART_TXD 0
|
|
|
|
|
|
#define CONFIG_MB_UART_RXD 1
|
|
|
|
|
|
|
|
|
|
|
|
//#define MB_PORT_NUM UART_NUM_1
|
|
|
|
|
|
//#define MB_DEV_SPEED 9600
|
|
|
|
|
|
|
|
|
|
|
|
// Enumeration of modbus device addresses accessed by master device
|
|
|
|
|
|
enum {
|
|
|
|
|
|
MB_DEVICE_ADDR1 = 1 // Only one slave device used for the test (add other slave addresses here)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Enumeration of all supported CIDs for device (used in parameter definition table)
|
|
|
|
|
|
enum {
|
|
|
|
|
|
CID_INP_DATA_0 = 0,
|
|
|
|
|
|
CID_HOLD_DATA_0,
|
|
|
|
|
|
CID_INP_DATA_1,
|
|
|
|
|
|
CID_HOLD_DATA_1,
|
|
|
|
|
|
CID_INP_DATA_2,
|
|
|
|
|
|
CID_HOLD_DATA_2,
|
|
|
|
|
|
CID_HOLD_TEST_REG,
|
|
|
|
|
|
CID_RELAY_P1,
|
|
|
|
|
|
CID_RELAY_P2,
|
|
|
|
|
|
CID_DISCR_P1,
|
|
|
|
|
|
CID_COUNT
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-03-11 18:55:34 +08:00
|
|
|
|
#define USE_MODBUS_OFFICIAL 0
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-14 15:58:53 +08:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-03-04 15:21:41 +08:00
|
|
|
|
/*Modbus<75><73><EFBFBD>߳<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
2025-03-11 15:15:37 +08:00
|
|
|
|
esp_err_t modbus_master_init(void);
|
|
|
|
|
|
|
|
|
|
|
|
void master_operation_func(void *arg);
|
|
|
|
|
|
|
2025-03-14 15:58:53 +08:00
|
|
|
|
static void* master_get_param_data(const mb_parameter_descriptor_t* param_descriptor);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|