串口实现FIFO接受数据(V2)

在上一次的基础上添加了不同需求缓冲区大小可变的更改。

 /*
* 串口的FIFO简单读取实现
* 功能,实现串口的FIFO实现
* 使用方法:
* 更新时间:2017.9.26
* 版本:v2.0.0
* 编写:ZhangPeng
*/
#include "stdio.h"
#include <stdlib.h>
#include "df_fifo.h"
#include <memory.h>
/*
* 函数功能:Fifo出队操作
* 入口参数
参数1:缓冲区句柄
参数2:要出队的指针地址
参数3:出队的个数
* 出口参数:是否出队成功
* 注意事项:该函数只从头指针开始读取,length个字节,不会影响缓冲区数据
*/
Df_StateTypeDef Df_FifoOutput(Df_FifoPointTypeDef kfifo, uint8_t * buf, uint8_t length)
{
Df_FifoStructTypeDef Pfifo;
Pfifo = *kfifo;
if (Pfifo.Count - length < )//缓冲区没有足够的数据
{
return Df_StateOutOver;//读数据越界
}
while (length--)
{
*buf = Pfifo.buffer[Pfifo.read];
buf++;
Pfifo.read++;//读取指针自加
if (Pfifo.read == Pfifo.length)
{
Pfifo.read = ;
}
}
return Df_StateOk;//数据读取成功
} /*
* 函数功能:Fifo入队操作
* 入口参数
参数1:缓冲区句柄
参数2:要入队的指针地址
参数3:入队的个数
* 出口参数:是否入队成功
* 注意事项:该函数在缓冲区满的时候会返回Over
*/
Df_StateTypeDef Df_FifoInput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length)//
{
if (Pfifo->Count + length > Pfifo->length)//写入的数据超过缓冲区
{
return Df_StateInOver;//写数据越界
}
while (length--)
{
Pfifo->buffer[Pfifo->write] = *buf;//赋值给缓冲区
buf++;//缓冲区地址加一
Pfifo->Count++;
Pfifo->write++;//
if (Pfifo->write == Pfifo->length)
{
Pfifo->write = ;
}
}
return Df_StateOk;//数据读取成功
} /*
* 函数功能:缓冲区擦除操作
* 入口参数
参数1:缓冲区句柄
参数2:擦除的数据长度
* 出口参数:是否擦除
* 注意事项:该函数会将缓冲区头开始的length个数据从缓冲区擦除
*/
Df_StateTypeDef Df_FifoErase(Df_FifoPointTypeDef Pfifo, uint8_t length)//
{
if (Pfifo->Count - length > Pfifo->length)//写入的数据超过缓冲区
{
return Df_StateEaserOver;//写数据越界
}
while (length--)
{
Pfifo->Count--;
Pfifo->read++;//
if (Pfifo->read == Pfifo->length)
{
Pfifo->read = ;
}
}
return Df_StateOk;//数据读取成功
} /*
* 函数功能:重置缓冲区
* 入口参数
参数1:缓冲区句柄
参数2:缓冲区数组首地址
参数3:缓冲区的大小
* 出口参数:是否重置成功
* 注意事项:length不能小于buf 的长度否则会导致内存泄漏
*/
Df_StateTypeDef Df_FifoReset(Df_FifoPointTypeDef Pfifo,uint8_t * buf,uint16_t length)
{
printf("[%s]the buffer length is %d\n", __FUNCTION__, sizeof(buf));
Pfifo->buffer = buf;//缓冲区数组
Pfifo->length = length;//缓冲区大小
Pfifo->Count = ;//数据为空
Pfifo->read = ;//读指针为0
Pfifo->write = ;//写指针为0
return Df_StateOk;
} /*
* 函数功能:Fifo出队操作
* 入口参数
参数1:缓冲区句柄
参数2:要出队的指针地址
参数3:出队的个数
* 出口参数:是否出队成功
* 注意事项:该函数只从头指针开始读取,length个字节,同时擦除该部分数据
*/
Df_StateTypeDef Df_FifoOutEaser(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length)
{
if (Pfifo->Count - length < )//缓冲区没有足够的数据
{
return Df_StateOutOver;//读数据越界
}
while (length--)
{
*buf = Pfifo->buffer[Pfifo->read];
buf++;
Pfifo->Count--;
Pfifo->read++;//读取指针自加
if (Pfifo->read == Pfifo->length)
{
Pfifo->read = ;
}
}
return Df_StateOk;//数据读取成功
}
#define debug 1//是否打印缓冲区状态
/*测试*/
int main()
{
Df_FifoStructTypeDef Fifostructdefine;
uint8_t buffer[];//定义缓冲区
memset(buffer, , * sizeof(uint8_t));//初始化缓冲区
uint8_t data[];//定义应用数组
uint8_t test[];//
memset(test, , *sizeof(uint8_t));
for (int i = ; i < ; i++)
data[i] = i;
Df_FifoReset(&Fifostructdefine, buffer, );//初始化缓冲区结构
#if debug
printf("The buffer count is %d\n", Fifostructdefine.Count);
printf("The buffer length is %d\n", Fifostructdefine.length);
printf("The buffer Pread is %d\n", Fifostructdefine.read);
printf("The buffer Pwrite is %d\n", Fifostructdefine.write);
#endif // debug
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>写入测试<<<<<<<<<<<<<<<<<<<<<<<\n");
Df_FifoInput(&Fifostructdefine, data, );
#if debug
printf("The buffer count is %d\n", Fifostructdefine.Count);
printf("The buffer length is %d\n", Fifostructdefine.length);
printf("The buffer Pread is %d\n", Fifostructdefine.read);
printf("The buffer Pwrite is %d\n", Fifostructdefine.write);
#endif // debug
/*直接读取测试*/
printf(">>>>>>>>>>>>>>>>>>>>>>>>>直接读取测试<<<<<<<<<<<<<<<<<<<<<<<\n");
Df_FifoOutput(&Fifostructdefine, &test, );
for (int i = ;i <;i++)
{
printf("test[%d] = %d\t", i-, test[i-]);
if (i % == )
{
printf("\n");
}
}
#if debug
printf("The buffer count is %d\n", Fifostructdefine.Count);
printf("The buffer length is %d\n", Fifostructdefine.length);
printf("The buffer Pread is %d\n", Fifostructdefine.read);
printf("The buffer Pwrite is %d\n", Fifostructdefine.write);
#endif // debug
/*擦除测试*/
printf(">>>>>>>>>>>>>>>>>>>>>擦除测试<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
Df_FifoErase(&Fifostructdefine, );
#if debug
printf("The buffer count is %d\n", Fifostructdefine.Count);
printf("The buffer length is %d\n", Fifostructdefine.length);
printf("The buffer Pread is %d\n", Fifostructdefine.read);
printf("The buffer Pwrite is %d\n", Fifostructdefine.write);
#endif // debug
/*读取擦除测试*/
printf(">>>>>>>>>>>>>>>>>>>>>读取擦除测试<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
while (Df_FifoOutEaser(&Fifostructdefine,&data[],) == Df_StateOk)
{
printf("Df_FifoOutEaser data is %d\n", data[]);
}
#if debug
printf("The buffer count is %d\n", Fifostructdefine.Count);
printf("The buffer length is %d\n", Fifostructdefine.length);
printf("The buffer Pread is %d\n", Fifostructdefine.read);
printf("The buffer Pwrite is %d\n", Fifostructdefine.write);
#endif // debug
system("pause");
}

.h文件

 #ifndef  _DF_FIFO_H
#define _DF_FIFO_H
#define uint8_t unsigned char
#define uint16_t unsigned int
/*该参数设置接受区大小*/ typedef struct { int read;//读指针
int write;//写指针
int Count;//缓冲区计数
int length;//缓冲区大小
uint8_t * buffer;// [RECERIVRSIZE];//接受缓冲区
}Df_FifoStructTypeDef, *Df_FifoPointTypeDef; typedef enum Df_StateTypeDef
{
Df_StateOk,//成功
Df_StateErr,//失败
Df_StateTimeout,//超时
Df_StateOutOver,//读溢出
Df_StateInOver,//写溢出
Df_StateEaserOver//擦除溢出
}Df_StateTypeDef; Df_StateTypeDef Df_FifoInput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length);
Df_StateTypeDef Df_FifoOutput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length);
Df_StateTypeDef Df_FifoErase(Df_FifoPointTypeDef Pfifo, uint8_t length);
Df_StateTypeDef Df_FifoReset(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint16_t length); #endif /*_DF_FIFO_H*/

该版本同样的使用静态队列实现,添加了擦除输出函数,只输出不删除函数,和删除函数,静态队列长度根据不同的应用可以自定义

上一篇:Delphi 实现获取其他程序的子窗体


下一篇:Java技术列表