Modbus调试失败,开始使用UART进行调试

This commit is contained in:
jarvis 2025-03-11 15:15:37 +08:00
parent 500841b6d2
commit 67b3708af1
5 changed files with 125 additions and 11 deletions

View File

@ -12,7 +12,7 @@
"idf.openOcdConfigs": [
"board/esp32s3-builtin.cfg"
],
"idf.portWin": "COM6",
"idf.portWin": "COM5",
"idf.flashType": "UART",
"files.associations": {
"ledc.h": "c",

View File

@ -66,3 +66,109 @@ esp_err_t modbus_master_init(void)
void master_operation_func(void *arg)
{
esp_err_t err = ESP_OK;
float value = 0;
bool alarm_state = false;
const mb_parameter_descriptor_t* param_descriptor = NULL;
ESP_LOGI(TAG, "Start modbus test...");
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++)
{
// Get data from parameters description table
// and use this information to fill the characteristics description table
// and having all required fields in just one table
err = mbc_master_get_cid_info(cid, &param_descriptor);
if ((err != ESP_ERR_NOT_FOUND) && (param_descriptor != NULL))
{
void* temp_data_ptr = master_get_param_data(param_descriptor);
assert(temp_data_ptr);
uint8_t type = 0;
err = mbc_master_get_parameter(cid, (char*)param_descriptor->param_key,
(uint8_t*)temp_data_ptr, &type);
if (err == ESP_OK)
{
if ((param_descriptor->mb_param_type == MB_PARAM_HOLDING) || (param_descriptor->mb_param_type == MB_PARAM_INPUT))
{
value = *(float*)temp_data_ptr;
ESP_LOGI(TAG, "Characteristic #%u %s (%s) value = %f (0x%" PRIx32 ") read successful.",
param_descriptor->cid,
param_descriptor->param_key,
param_descriptor->param_units,
value,
*(uint32_t*)temp_data_ptr);
if (((value > param_descriptor->param_opts.max) || (value < param_descriptor->param_opts.min)))
{
alarm_state = true;
break;
}
}else{
uint8_t state = *(uint8_t*)temp_data_ptr;
const char* rw_str = (state & param_descriptor->param_opts.opt1) ? "ON" : "OFF";
if ((state & param_descriptor->param_opts.opt2) == param_descriptor->param_opts.opt2)
{
ESP_LOGI(TAG, "Characteristic #%u %s (%s) value = %s (0x%" PRIx8 ") read successful.",
param_descriptor->cid,
param_descriptor->param_key,
param_descriptor->param_units,
(const char*)rw_str,
*(uint8_t*)temp_data_ptr);
} else {
ESP_LOGE(TAG, "Characteristic #%u %s (%s) value = %s (0x%" PRIx8 "), unexpected value.",
param_descriptor->cid,
param_descriptor->param_key,
param_descriptor->param_units,
(const char*)rw_str,
*(uint8_t*)temp_data_ptr);
alarm_state = true;
break;
}
}
}
}
}
}
ESP_LOGI(TAG, "Destroy master...");
ESP_ERROR_CHECK(mbc_master_destroy());
}
static void* master_get_param_data(const mb_parameter_descriptor_t* param_descriptor)
{
assert(param_descriptor != NULL);
void* instance_ptr = NULL;
if (param_descriptor->param_offset != 0) {
switch(param_descriptor->mb_param_type)
{
case MB_PARAM_HOLDING:
instance_ptr = ((void*)&holding_reg_params + param_descriptor->param_offset - 1);
break;
case MB_PARAM_INPUT:
instance_ptr = ((void*)&input_reg_params + param_descriptor->param_offset - 1);
break;
case MB_PARAM_COIL:
instance_ptr = ((void*)&coil_reg_params + param_descriptor->param_offset - 1);
break;
case MB_PARAM_DISCRETE:
instance_ptr = ((void*)&discrete_reg_params + param_descriptor->param_offset - 1);
break;
default:
instance_ptr = NULL;
break;
}
} else {
ESP_LOGE(TAG, "Wrong parameter offset for CID #%u", (unsigned)param_descriptor->cid);
assert(instance_ptr != NULL);
}
return instance_ptr;
}

View File

@ -13,7 +13,7 @@
#define MASTER_MAX_CIDS num_device_parameters
// Number of reading of parameters from slave
#define MASTER_MAX_RETRY 30
#define MASTER_MAX_RETRY 3
// Timeout to update cid over Modbus
#define UPDATE_CIDS_TIMEOUT_MS (500)
@ -63,3 +63,7 @@ enum {
/*Modbus总线初始化函数*/
esp_err_t modbus_master_init(void);
void master_operation_func(void *arg);
static void* master_get_param_data(const mb_parameter_descriptor_t* param_descriptor);

View File

@ -35,14 +35,18 @@ void powerTask(void)
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN8, &Bat_Adc_Value));
/*根据采样值计算电池电压*/
Bat_Voltage_Value = 2 * (float)Bat_Adc_Value * 1.767 / 4095.0 + 2.3;
vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(5000));
/*配置电池信息页显示*/
lv_bar_set_value(ui_Batinfo_BarBAT,Bat_Adc_Value,LV_ANIM_ON);
lv_spinbox_set_value(ui_Batinfo_SpinboxBAT,Bat_Adc_Value);
lv_label_set_text_fmt(ui_Batinfo_valueVoltage,"%.2fV",Bat_Voltage_Value);
lv_label_set_text_fmt(ui_Batinfo_valueVolume,"%.0fmAh",Bat_Voltage_Value/4.2*2500);
if (lv_scr_act() == ui_Batinfo)
{
/*配置电池信息页显示*/
lv_bar_set_value(ui_Batinfo_BarBAT,Bat_Adc_Value,LV_ANIM_ON);
lv_spinbox_set_value(ui_Batinfo_SpinboxBAT,Bat_Adc_Value);
lv_label_set_text_fmt(ui_Batinfo_valueVoltage,"%.2fV",Bat_Voltage_Value);
lv_label_set_text_fmt(ui_Batinfo_valueVolume,"%.0fmAh",Bat_Voltage_Value/4.2*2500);
}
/*设置Home页的显示电量*/
lv_slider_set_value(ui_Home_SliderHeaderBat,Bat_Voltage_Value/4.2*100,LV_ANIM_ON);

View File

@ -20,7 +20,7 @@ void ReadSdCallFunc(lv_event_t * e)
void F1callbackFunc(lv_event_t * e)
{
// Your code here
master_operation_func(NULL);
}
void F2callbackFunc(lv_event_t * e)