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

 /*
* 串口的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. Linux常用命令----基本文件系统常用命令

    1.查看当前工作目录---pwd sunny@sunny-ThinkPad-T450:~$ pwd /home/sunny sunny@sunny-ThinkPad-T450:~$ cd Worksp ...

  2. 分别用js和css实现瀑布流

    下午查找了瀑布流的相关原理,找了一些css3实现的还有js实现的,最后总结了一些比较简单的,易懂的整理起来 1.css3实现 只要运用到    column-count分列 column-width固 ...

  3. java基础之抽象类和接口的区别

    抽象类和接口的区别 A:成员区别 抽象类: 成员变量:可以是变量,也可以是常量 构造方法:有 成员方法:可以是抽象方法,也可以是非抽象方法 接口: 成员变量:只能是静态常量(不写修饰符,默认是 sta ...

  4. len=in.read(b,0,len)和len=in.read(b)的区别

    byte[] byte = new byte[1024]; int len =0 ; while((len=in.read(b))!=-1){ out.write(b,0,len); } read函数 ...

  5. Python_10-异常处理

    目录: 1.1 python标准异常1.2 简介1.3 try语句     1.3.1 使用try和except语句来捕获异常     1.3.2 该种异常处理语法的规则     1.3.3 使用tr ...

  6. unity自带寻路Navmesh入门教程

    http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/ http://liweizhaolili.blog.163 ...

  7. 开发微信小程序入门教程,含破解工具

    2016年09月21日晚 微信发不了微信“小程序”的内测版,一时间整个互联网都炸了锅.个大新闻.论坛都在讨论这个事情. 作为互联网的一猿,我们怎能不紧跟时代的脚步.于是第二天上午也对微信发布的“小程序 ...

  8. pcl文档库

    http://docs.pointclouds.org/trunk/structpcl_1_1_polygon_mesh.html

  9. zigbee初探

    什么是zigbee? 1.它是一种通信方式,一种通信协议: 2.其作用就是构建一个类似无线局域网的东西:如果这个局域网用于传感器的数据收集.监控,那么这个网络就叫做无线传感器网络. 应用领域:家居.工 ...

  10. SpringMVC 2.5.6 +Hibernate 3.2.0

    spring MVC配置详解 现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时 ...