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

 /*
* 串口的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. MySQL 索引优化原则

    一.索引优化原则 1.最左前缀匹配原则,联合索引,mysql会从做向右匹配直到遇到范围查询(>.<.between.like)就停止匹配,比如a = 1 and b = 2 and c & ...

  2. JS倒计时,自动提交表单!

    <form id="frm" action="http://www.baidu.com"> 考试还剩余<div id="time&q ...

  3. BMP文件结构(转)

    [转自网络] BMP文件存储结构的格式可以在Windows中的WINGDI.h文件中找到定义. BMP文件总体上由4部分组成,分别是位图文件头.位图信息头.调色板和图像数据,如表5-1所示. 表5-1 ...

  4. Python常见函数用法

    1. shape()函数 在numpy模块 输入参数:类似数组(比如列表,元组)等,或是数组 返回:一个整型数字的元组,元组中的每个元素表示相应的数组每一维的长度 # shape[0]返回对象的行数, ...

  5. 【jdbc】【c3p0】c3p0三种配置方式【整理】

    c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...

  6. Blending

    [Blending] Blending is used to make transparent objects. When graphics are rendered, after all shade ...

  7. solr配置相关:约束文件及引入ik分词器

    schema.xml: solr约束文件 Solr中会提前对文档中的字段进行定义,并且在schema.xml中对这些字段的属性进行约束,例如:字段数据类型.字段是否索引.是否存储.是否分词等等 < ...

  8. 读取properties文件并获取属性值

    1.Properties与ResourceBundle 两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单. 2.Propertie ...

  9. SpringBoot28 RabbitMQ知识点、Docker下载RabbitMQ、SpringBoot整合RabbtiMQ

    1 RabbitMQ知识点 1.1 整体架构图 消息生产者将消息投递到exchange中,exchange会以某种路由机制将生产者投递的消息路由到queue中,消息消费者再从queue中获取消息进行消 ...

  10. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...