串口接收中断处理(0x0D 0x0A作为结尾)
串口接收没有采用环形缓冲区,使用0x0D 0x0A作为结尾(即回车换行符)。
uint8_t rxBuffer[128];
串口接收到的数据保存在rxBuffer数组中,
uint16_t count=0;
count表示串口接收数据的数量
bool bFlagOneFrame = false;
bFlagOneFrame一帧数据是否结束的标志
说明:在C99中直接使用bool定义,需要包含如下标准头文件
#include <stdbool.h>
说明:在C99中直接使用uint8_t等整形数据类型关键字定义,需要包含如下标准头文件
#include <stdint.h>
测试结果
具体代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "cJSON.h"
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "cmd_queue.h"
char *strJSON;
cJSON *objJson;
float f1;
char *json_string = "{\"SV\":60,\"KP\":5.0,\"KI\":100,\"KD\":300}";
uint8_t rxBuffer[128];
uint16_t count=0;
bool bFlagOneFrame = false;
uint8_t ucTemp;
void USART1_IRQHandler(void)
{
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE))
{
__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
ucTemp = ( uint16_t)READ_REG(huart1.Instance->DR);
rxBuffer[count++] = ucTemp;
if(0x0A == ucTemp)
bFlagOneFrame = true;
}
}
void test_cjson(void);
void cJsonQueParse(char *str);
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
queue_reset();
while (1)
{
if(true == bFlagOneFrame)
{
cJsonQueParse((char *)rxBuffer);
count = 0;
bFlagOneFrame = false;
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void cJsonQueParse(char *str)
{
cJSON* cjson = cJSON_Parse((const char *)str);
if(cjson == NULL)
{
printf("cjson parse error...\r\n");
}
else
{
char *json_data = cJSON_PrintUnformatted(cjson);
printf("%s\n",json_data);
printf("/*********************以下就是提取的数据**********************/\n");
double fSV = cJSON_GetObjectItem(cjson,"SV")->valuedouble;
printf("SV: %.4f\n",fSV);
double fKP = cJSON_GetObjectItem(cjson,"KP")->valuedouble;
printf("KP: %.4f\n",fKP);
double fKI = cJSON_GetObjectItem(cjson,"KI")->valuedouble;
printf("KI: %.4f\n",fKI);
double fKD = cJSON_GetObjectItem(cjson,"KD")->valuedouble;
printf("KD: %.4f\n",fKD);
free(json_data);
cJSON_Delete(cjson);
}
}
void test_cjson(void)
{
double grade[4]= {66.51,118.52,61.53,128.54};
int time[4]= {123,456,789,150};
cJSON *measurements = cJSON_CreateObject();
cJSON_AddStringToObject(measurements,"AAA","26.55666");
cJSON_AddNumberToObject(measurements,"BBB",13);
cJSON_AddNumberToObject(measurements,"CCC",13.55666);
cJSON_AddFalseToObject(measurements,"gender");
cJSON_AddStringToObject(measurements,"status","0x8000");
cJSON *item1 = cJSON_CreateObject();
cJSON_AddStringToObject(item1,"item1_string","China");
cJSON_AddNumberToObject(item1,"item1_num",2021);
cJSON_AddItemToObject(measurements,"item1",item1);
objJson = cJSON_GetObjectItem(measurements, "AAA");
f1 = strtod(objJson->valuestring, NULL);
char *json_data = cJSON_PrintUnformatted(measurements);
printf("%s\n",json_data);
free(json_data);
cJSON_Delete(measurements);
}
int fputc(int ch,FILE *f)
{
uint8_t temp[1]= {ch};
HAL_UART_Transmit(&huart1,temp,1,2);
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
|