#ifndef USER_COMFIFO_H
#define USER_COMFIFO_H

#include "stm32f4xx_hal.h"
#define RINGBUFF_LEN 512
typedef struct
{
	uint16_t Head;
  uint16_t Tail;
  uint16_t Lenght;
  uint8_t Ring_Buff[RINGBUFF_LEN];
}RingBuff_t;
RingBuff_t ringBuff;//创建一个ringBuff的缓冲区
void RingBuff_Init(void);
uint8_t  Write_RingBuff(uint8_t data);
uint8_t Read_RingBuff(uint8_t *rData);
#endif
#include"ComFIFO.h"
/**
* @brief RingBuff_Init
* @param void
* @return void
* @author 杰杰
* @date  2018
* @version v1.0
* @note  初始化环形缓冲区
*/
void RingBuff_Init(void)
{
  //初始化相关信息
  ringBuff.Head = 0;
  ringBuff.Tail = 0;
  ringBuff.Lenght = 0;
}
/**
* @brief Write_RingBuff
* @param u8 data
* @return FLASE:环形缓冲区已满,写入失败;TRUE:写入成功
* @author 杰杰
* @date  2018
* @version v1.0
* @note  往环形缓冲区写入u8类型的数据
*/
uint8_t Write_RingBuff(uint8_t data)
{
  if(ringBuff.Lenght >= RINGBUFF_LEN) //判断缓冲区是否已满
  {
   return 0;
  }
  ringBuff.Ring_Buff[ringBuff.Tail]=data;
//  ringBuff.Tail++;
  ringBuff.Tail = (ringBuff.Tail+1)%RINGBUFF_LEN;//防止越界非法访问
  ringBuff.Lenght++;
  return 1;
}
/**
* @brief Read_RingBuff
* @param u8 *rData,用于保存读取的数据
* @return FLASE:环形缓冲区没有数据,读取失败;TRUE:读取成功
* @author 杰杰
* @date  2018
* @version v1.0
* @note  从环形缓冲区读取一个u8类型的数据
*/
uint8_t Read_RingBuff(uint8_t *rData)
{
  if(ringBuff.Lenght == 0)//判断非空
  {
    return 0;
  }
  *rData = ringBuff.Ring_Buff[ringBuff.Head];//先进先出FIFO,从缓冲区头出
//  ringBuff.Head++;
  ringBuff.Head = (ringBuff.Head+1)%RINGBUFF_LEN;//防止越界非法访问
  ringBuff.Lenght--;
  return 1;
}

 

2、调用方法

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance==USART3)
	{

		Write_RingBuff(RxBuffer[0]);
		//USARTx_ReceiveFIFO_PutChar(&Com1FIFO,RxBuffer[0]);
		HAL_UART_Receive_IT(&huart3, (uint8_t *)RxBuffer, 1);
	}

}

 

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  //USARTx_ReceiveFIFO_Init(&Com1FIFO);
  RingBuff_Init();
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */
  HAL_UART_Receive_IT(&huart3, (uint8_t *)RxBuffer, 1);
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
   {
	  uint8_t data[1024];
	  uint16_t dataLength=0;
	  uint16_t read=Read_RingBuff(&data[dataLength]);
	  if(read>0)dataLength++;
	  while(read>0)
	  {

		  read=Read_RingBuff(&data[dataLength]);
		  if(read>0)dataLength++;
	  }
	  HAL_UART_Receive_IT(&huart3, (uint8_t *)RxBuffer, 1);
	  if(dataLength>0)
	  {
		  HAL_UART_Transmit(&huart3, (uint8_t *)data, dataLength,0xFFFF);
	  }
   }
}

  

参考:https://blog.csdn.net/jiejiemcu/article/details/80563422