Files
Frame-rate-optimization/App/app_get_json.c
2026-04-09 10:14:20 +08:00

124 lines
4.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "app_get_json.h"
#include "cJSON.h"
#include "bsp_uart.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h> // 必须包含用于atoi函数
#include "app_scan.h"
uint8_t uart_rx_buffer[1024];
cJSON *cjson = NULL;
int Max_resis;
int Min_resis;
int Div_resis;
// 辅助函数解析JSON数组到uint8_t数组
// json_arr: cJSON数组对象, arr: 目标数组, max_len: 数组最大长度
void parse_json_array(cJSON *json_item, uint8_t *arr, int max_len) {
// 1. 初始化数组为0避免脏数据
memset(arr, 0, max_len * sizeof(uint8_t));
// 2. 检查JSON项是否有效是字符串类型
if (json_item == NULL || !cJSON_IsString(json_item) || json_item->valuestring == NULL) {
return; // 不是有效字符串,直接返回
}
// 3. 复制字符串到临时缓冲区避免修改原JSON数据
char temp_buf[64] = {0}; // 足够容纳12个数字+逗号的长度
strncpy(temp_buf, json_item->valuestring, sizeof(temp_buf) - 1);
// 4. 按逗号分割字符串,解析为数字数组
char *token = strtok(temp_buf, ",");
int idx = 0;
while (token != NULL && idx < max_len) {
// 把分割后的字符串转为数字,存入数组
arr[idx++] = (uint8_t)atoi(token);
token = strtok(NULL, ",");
}
}
int get_Json_data(json_data_struct * sys){
int result = -1; // 1 表示成功,其他值表示错误
uint16_t uart_get_num = usart_rx_get_rx_data_count(USART_3_TR);
// 只有当串口接收到数据时才进行解析
if(uart_get_num > 0 && uart_get_num < sizeof(uart_rx_buffer)){ // 增加缓冲区溢出保护
// 读取串口接收的数据到缓冲区
uint16_t get_size = usart_rx_recv(USART_3_TR, uart_rx_buffer, uart_get_num);
// 确保数据读取成功
if(get_size != uart_get_num){
memset(uart_rx_buffer, 0, sizeof(uart_rx_buffer)); // 清空缓冲区
return -2; // 读取数据失败错误码
}
// 先释放之前的cJSON内存避免内存泄漏
if(cjson != NULL ){
cJSON_Delete(cjson);
cjson = NULL; // 释放后置空,避免野指针
}
// 解析JSON字符串将串口缓冲区数据转为cJSON对象
cjson = cJSON_Parse((const char*)uart_rx_buffer);
memset(uart_rx_buffer, 0, sizeof(uart_rx_buffer));//解析完成,清空
if (cjson != NULL) {
// 2. 依次获取JSON中的三个字段并进行有效性检查
cJSON *Max_resis_item = cJSON_GetObjectItem(cjson, "Max_resis");
cJSON *Min_resis_item = cJSON_GetObjectItem(cjson, "Min_resis");
cJSON *Div_resis_item = cJSON_GetObjectItem(cjson, "Div_resis");
cJSON *x_rank_arr = cJSON_GetObjectItem(cjson, "x_rank");
cJSON *y_rank_arr = cJSON_GetObjectItem(cjson, "y_rank");
if(x_rank_arr != NULL && y_rank_arr != NULL)
{
uint8_t x_rank[12] = {0};
parse_json_array(x_rank_arr, x_rank, 12);
// 3. 直接解析y_rank JSON数组
uint8_t y_rank[12] = {0};
parse_json_array(y_rank_arr, y_rank, 12);
// 4. 调用保存函数
save_config_ranks(x_rank, y_rank);
}
// 检查所有需要的字段是否存在且为字符串类型你的JSON中值是字符串格式
if(Max_resis_item != NULL && cJSON_IsString(Max_resis_item) &&
Min_resis_item != NULL && cJSON_IsString(Min_resis_item) &&
Div_resis_item != NULL && cJSON_IsString(Div_resis_item))
{
// 3. 将字符串类型的值转换为int并存储到结构体中
Max_resis = atoi(Max_resis_item->valuestring);
Min_resis = atoi(Min_resis_item->valuestring);
Div_resis = atoi(Div_resis_item->valuestring);
sys->max_trigger_res_value = Max_resis;
sys->min_trigger_res_value = Min_resis;
sys->div_trigger_res = Div_resis;
result = 1; // 解析成功返回1
}
else{
result = -3; // 字段缺失或类型错误
}
// 4. 清理cJSON内存解析完成后必须释放避免内存泄漏
cJSON_Delete(cjson);
cjson = NULL; // 置空,防止重复释放
}
else{
result = -4; // JSON格式错误解析失败
}
// 清空串口接收缓冲区,准备下一次接收
}
else{
result = -5; // 串口无数据或数据超出缓冲区大小
}
return result;
}