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

 /*
* 串口的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*/

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

串口实现FIFO接受数据(V2)的更多相关文章

  1. 串口实现FIFO接受数据

    基本原理:静态队列 /* * 串口的FIFO简单读取实现 * 功能,实现串口的FIFO实现 * 使用方法: * 版本:v1.0.0 * */ #include "sys.h" #i ...

  2. 串口通信:接受数据(仿真task写法)

    1.功能描述 设计一个串口数据接收模块.能够以设定的波特率(与发射端口速率匹配)接收数据,并输出保存到一个寄存器中. 2.过程描述 ①边沿检测器,识别出起始位时让接收使能端有效.这里需要排除边沿脉冲的 ...

  3. RS232串口用事件接受数据(一问一答)

    private void button1_Click(object sender, EventArgs e) { serialPort1.Open(); serialPort1.DataReceive ...

  4. C#上位机制作之串口接受数据(利用接受事件)

    前面设计好了界面,现在就开始写代码了,首先定义一个串口对象.. SerialPort serialport = new SerialPort();//定义串口对象 添加串口扫描函数,扫描出来所有可用串 ...

  5. linux串口驱动分析——发送数据

    一.应用程序中write函数到底层驱动历程 和前文提到的一样,首先先注册串口,使用uart_register_driver函数,依次分别为tty_register_driver,cdev_init函数 ...

  6. dsp28377控制DM9000收发数据——第三版程序,通过外部引脚触发来实现中断接受数据,优化掉帧现象

    //-------------------------------------------------------------------------------------------- - //D ...

  7. STM32串口接收不定长数据原理与源程序(转)

    今天说一下STM32单片机的接收不定长度字节数据的方法.由于STM32单片机带IDLE中断,所以利用这个中断,可以接收不定长字节的数据,由于STM32属于ARM单片机,所以这篇文章的方法也适合其他的A ...

  8. STM32F407的串口采用DMA收发数据

    源:STM32F407的串口采用DMA收发数据

  9. 纠错:基于FPGA串口发送彩色图片数据至VGA显示

    今天这篇文章是要修改之前的一个错误,前面我写过一篇基于FPGA的串口发送图片数据至VGA显示的文章,最后是显示成功了,但是显示的效果图,看起来确实灰度图,当时我默认我使用的MATLAB代码将图片数据转 ...

随机推荐

  1. 如何去掉Myeclipse对JS等文件的验证

    或 MyEclipse->validation->Excluded Resource下找到不需要验证的文件或者文件夹 或 右键点击该项目-->MyEclipse-->Exclu ...

  2. Python基础补充(二) 多核CPU上python多线程并行的一个假象【转】

    在python上开启多个线程,由于GIL的存在,每个单独线程都会在竞争到GIL后才运行,这样就干预OS内部的进程(线程)调度,结果在多核CPU上: python的多线程实际是串行执行的,并不会同一时间 ...

  3. python 获取当前运行的类名函数名

    import inspect def get_current_function_name(): return inspect.stack()[1][3] class MyClass: def func ...

  4. iOS布局之Auto Layout

    学习资源: <iOS6核心编程>自动布局部分 <iOS6范例经典>自动布局部分 Tutorial: iOS 6 Auto Layout versus Springs and S ...

  5. Renderer.materials 和sharedMaterials一些用法上的区别

    Not allowed to access Renderer.materials on prefab object. Use Renderer.sharedMaterials insteadUnity ...

  6. [SoapUI] DataSource, DataSourceLoop, DataSink

    Script assertion in login:

  7. [SoapUI] SOAP UI-Groovy Useful Commands

    Hi All, I have posted the SOAPUI and Groovy useful commands that may help you in your testing. Below ...

  8. LWIP协议栈1

    STM32F4自带的MAC,而没有PHY纯模拟电路部分,没有把PHY做进STM32F4是因为会对芯片的功耗有影响,同时芯片的体积会增大等原因. MAC与PHY的通信接口是MII以及RMII方式. MD ...

  9. mybatis常用方法总结

    mybatis的强大特性之一就是动态SQL.我们在写复杂查询的时候,会发现复杂查询包括了各种各样的判断,我们很难一鼓作气的写出完美的查询.动态SQL语句可以帮助我们拼接不同的SQL语句,而已让我们的代 ...

  10. HUST软工1506班第2周作业成绩公布

    说明 本次公布的成绩对应的作业为: 第2周个人作业:WordCount编码和测试 如果同学对作业成绩存在异议,在成绩公布的72小时内(截止日期4月26日0点)可以进行申诉,方式如下: 毕博平台的第二周 ...