缓存数据:环形队列,不啰嗦
#define SENSOR_485_DATA_BUFFER_MAX_LENGTH 60
typedef struct {
uint16_t front;
uint16_t rear;
uint8_t* buffer;
uint32_t maxSize;
}Buffer_t;
static Buffer_t sensor485Buffer;
uint8_t sensor485DataBuffer[SENSOR_485_DATA_BUFFER_MAX_LENGTH];//缓存数组
void uart3WriteByte(uint8_t data)
{
Buffer_Puts(&sensor485Buffer,&data,1);//入队
}
bool Buffer_Puts(Buffer_t* buffer, uint8_t* data, uint16_t length)
{
if (buffer->maxSize - Buffer_Size(buffer) <= length)//队满
return false;
for (uint16_t i = 0; i<length; ++i)//队列未满
{
buffer->rear = (buffer->rear + 1) % buffer->maxSize;
buffer->buffer[buffer->rear] = data[i];//进队
}
return true;
}
|