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

 /*
* 串口的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. C++Primer笔记-----day04

    1.函数指针.函数指针指向某种特定类型,函数的类型由它的返回类型和形参类型决定,与函数名无关.比如:bool lengthCompare(const string &,const string ...

  2. AngularJS1.6版本中ui-router路由中/#!/的解决方法 - zhuan

    本地编译出的文件可以正常运行,但是服务器编译后到了测试那里路由上就莫名的出现了/#!/,这导致了很多问题. 后来查了下是服务器编译器把AngularJS升级到了1.6版本,而我本地的依旧是1.5. 但 ...

  3. C# unsafe模式内存操作深入探索

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...

  4. .net Core 2.0使用NLog

    最近研究了一下NLog的使用方式,简单的入了一下门. 实现的功能,对于不同的日志,进行不同的记录,分别有系统运行日志,和个人在程序中写的异常日志.发布之后放在了IIS上.进行查看日志的信息 参考了两篇 ...

  5. PHP提取字符串中的手机号正则表达式怎么写

    0. 简介 PHP通过正则表达式提取字符串中的手机号并判断运营商,简单快速方便,能提取多个手机号. 1. 代码 <?php header("content-type:text/plai ...

  6. java核心知识点 --- 线程池ThreadPool

    线程池是多线程学习中需要重点掌握的. 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统交互.在这种情形下,使用线程池可以很好的提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考 ...

  7. Apache Hive (一)Hive初识

    转自:https://www.cnblogs.com/qingyunzong/p/8707885.html Hive 简介 什么是Hive 1.Hive 由 Facebook 实现并开源 2.是基于 ...

  8. zabbix自定义key监控redis

    一.启动redis-server cd /data/redis redis-server redis.conf (根据自己的环境启动redis) 测试脚本(写入1000个数据): seq |while ...

  9. SqlServer——索引

    索引是根据表中一列或若干列按照一定顺序建立的列值与记录行之间的对应关系表.在数据库系统中建立索引主要有以下作用: l快速存取数据: l保证数据记录的唯一性: l实现表与表之间的参照完整性: l在使用O ...

  10. [Plan]计划

    1. scala 2. kafka 1. lua 2. openResty 1. 日志收集 - python 2. 代码生成 3. 权限系统