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

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

  1 /*
  2 * 串口的FIFO简单读取实现
  3 * 功能,实现串口的FIFO实现
  4 * 使用方法:
  5 * 更新时间:2017.9.26
  6 * 版本:v2.0.0
  7 * 编写:ZhangPeng
  8 */
  9 #include "stdio.h"
 10 #include <stdlib.h>
 11 #include "df_fifo.h"
 12 #include <memory.h>
 13 /*
 14  * 函数功能:Fifo出队操作
 15  * 入口参数
 16         参数1:缓冲区句柄
 17         参数2:要出队的指针地址
 18         参数3:出队的个数
 19  * 出口参数:是否出队成功
 20  * 注意事项:该函数只从头指针开始读取,length个字节,不会影响缓冲区数据
 21 */
 22 Df_StateTypeDef Df_FifoOutput(Df_FifoPointTypeDef kfifo, uint8_t * buf, uint8_t length)
 23 {
 24     Df_FifoStructTypeDef Pfifo;
 25     Pfifo = *kfifo;
 26     if (Pfifo.Count - length  < 0)//缓冲区没有足够的数据
 27     {
 28         return Df_StateOutOver;//读数据越界
 29     }
 30     while (length--)
 31     {
 32         *buf = Pfifo.buffer[Pfifo.read];
 33         buf++;
 34         Pfifo.read++;//读取指针自加
 35         if (Pfifo.read == Pfifo.length)
 36         {
 37             Pfifo.read = 0;
 38         }
 39     }
 40     return Df_StateOk;//数据读取成功
 41 }
 42 
 43 /*
 44  * 函数功能:Fifo入队操作
 45  * 入口参数
 46     参数1:缓冲区句柄
 47     参数2:要入队的指针地址
 48     参数3:入队的个数
 49  * 出口参数:是否入队成功
 50  * 注意事项:该函数在缓冲区满的时候会返回Over
 51 */
 52 Df_StateTypeDef Df_FifoInput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length)//
 53 {
 54     if (Pfifo->Count + length  > Pfifo->length)//写入的数据超过缓冲区
 55     {
 56         return Df_StateInOver;//写数据越界
 57     }
 58     while (length--)
 59     {
 60         Pfifo->buffer[Pfifo->write] = *buf;//赋值给缓冲区
 61         buf++;//缓冲区地址加一        
 62         Pfifo->Count++;
 63         Pfifo->write++;//
 64         if (Pfifo->write == Pfifo->length)
 65         {
 66             Pfifo->write = 0;
 67         }
 68     }
 69     return Df_StateOk;//数据读取成功
 70 }
 71 
 72 /*
 73  * 函数功能:缓冲区擦除操作
 74  * 入口参数
 75     参数1:缓冲区句柄
 76     参数2:擦除的数据长度
 77  * 出口参数:是否擦除
 78  * 注意事项:该函数会将缓冲区头开始的length个数据从缓冲区擦除
 79 */
 80 Df_StateTypeDef Df_FifoErase(Df_FifoPointTypeDef Pfifo, uint8_t length)//
 81 {
 82     if (Pfifo->Count - length > Pfifo->length)//写入的数据超过缓冲区
 83     {
 84         return Df_StateEaserOver;//写数据越界
 85     }
 86     while (length--)
 87     {     
 88         Pfifo->Count--;
 89         Pfifo->read++;//
 90         if (Pfifo->read == Pfifo->length)
 91         {
 92             Pfifo->read = 0;
 93         }
 94     }
 95     return Df_StateOk;//数据读取成功
 96 }
 97 
 98 /*
 99  * 函数功能:重置缓冲区
100  * 入口参数
101     参数1:缓冲区句柄
102     参数2:缓冲区数组首地址
103     参数3:缓冲区的大小
104  * 出口参数:是否重置成功
105  * 注意事项:length不能小于buf 的长度否则会导致内存泄漏
106 */
107 Df_StateTypeDef Df_FifoReset(Df_FifoPointTypeDef Pfifo,uint8_t * buf,uint16_t length)
108 {
109     printf("[%s]the buffer length is  %d
", __FUNCTION__, sizeof(buf));
110     Pfifo->buffer = buf;//缓冲区数组
111     Pfifo->length = length;//缓冲区大小
112     Pfifo->Count = 0;//数据为空
113     Pfifo->read = 0;//读指针为0
114     Pfifo->write = 0;//写指针为0
115     return Df_StateOk;
116 }
117 
118 /*
119  * 函数功能:Fifo出队操作
120  * 入口参数
121  参数1:缓冲区句柄
122  参数2:要出队的指针地址
123  参数3:出队的个数
124  * 出口参数:是否出队成功
125  * 注意事项:该函数只从头指针开始读取,length个字节,同时擦除该部分数据
126 */
127 Df_StateTypeDef Df_FifoOutEaser(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length)
128 {
129     if (Pfifo->Count - length < 0)//缓冲区没有足够的数据
130     {
131         return Df_StateOutOver;//读数据越界
132     }
133     while (length--)
134     {
135         *buf = Pfifo->buffer[Pfifo->read];
136         buf++;
137         Pfifo->Count--;
138         Pfifo->read++;//读取指针自加
139         if (Pfifo->read == Pfifo->length)
140         {
141             Pfifo->read = 0;
142         }
143     }
144     return Df_StateOk;//数据读取成功
145 }
146 #define  debug  1//是否打印缓冲区状态
147 /*测试*/
148 int main()
149 {
150     Df_FifoStructTypeDef Fifostructdefine;
151     uint8_t buffer[100];//定义缓冲区
152     memset(buffer, 0, 100 * sizeof(uint8_t));//初始化缓冲区
153     uint8_t data[100];//定义应用数组
154     uint8_t test[100];//
155     memset(test, 0, 100*sizeof(uint8_t));
156     for (int i = 0; i < 100; i++)
157         data[i] = i;
158     Df_FifoReset(&Fifostructdefine, buffer, 100);//初始化缓冲区结构
159 #if debug
160     printf("The buffer count is %d
", Fifostructdefine.Count);
161     printf("The buffer length is %d
", Fifostructdefine.length);
162     printf("The buffer Pread is %d
", Fifostructdefine.read);
163     printf("The buffer Pwrite is %d
", Fifostructdefine.write);
164 #endif // debug
165     printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>写入测试<<<<<<<<<<<<<<<<<<<<<<<
");
166     Df_FifoInput(&Fifostructdefine, data, 100);
167 #if debug
168     printf("The buffer count is %d
", Fifostructdefine.Count);
169     printf("The buffer length is %d
", Fifostructdefine.length);
170     printf("The buffer Pread is %d
", Fifostructdefine.read);
171     printf("The buffer Pwrite is %d
", Fifostructdefine.write);
172 #endif // debug
173     /*直接读取测试*/
174     printf(">>>>>>>>>>>>>>>>>>>>>>>>>直接读取测试<<<<<<<<<<<<<<<<<<<<<<<
");
175     Df_FifoOutput(&Fifostructdefine, &test, 50);
176     for (int i = 1;i <51;i++)
177     {
178         printf("test[%d] = %d	", i-1, test[i-1]);
179         if (i % 5 == 0)
180         {
181             printf("
");
182         }
183     }
184 #if debug
185     printf("The buffer count is %d
", Fifostructdefine.Count);
186     printf("The buffer length is %d
", Fifostructdefine.length);
187     printf("The buffer Pread is %d
", Fifostructdefine.read);
188     printf("The buffer Pwrite is %d
", Fifostructdefine.write);
189 #endif // debug
190     /*擦除测试*/
191     printf(">>>>>>>>>>>>>>>>>>>>>擦除测试<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
");
192     Df_FifoErase(&Fifostructdefine, 30);
193 #if debug
194     printf("The buffer count is %d
", Fifostructdefine.Count);
195     printf("The buffer length is %d
", Fifostructdefine.length);
196     printf("The buffer Pread is %d
", Fifostructdefine.read);
197     printf("The buffer Pwrite is %d
", Fifostructdefine.write);
198 #endif // debug
199     /*读取擦除测试*/
200     printf(">>>>>>>>>>>>>>>>>>>>>读取擦除测试<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
");
201     while (Df_FifoOutEaser(&Fifostructdefine,&data[0],1) == Df_StateOk)
202     {
203         printf("Df_FifoOutEaser data is  %d
", data[0]);
204     }
205 #if debug
206     printf("The buffer count is %d
", Fifostructdefine.Count);
207     printf("The buffer length is %d
", Fifostructdefine.length);
208     printf("The buffer Pread is %d
", Fifostructdefine.read);
209     printf("The buffer Pwrite is %d
", Fifostructdefine.write);
210 #endif // debug
211     system("pause");
212 }

.h文件

 1 #ifndef  _DF_FIFO_H
 2 #define  _DF_FIFO_H
 3 #define uint8_t unsigned char
 4 #define uint16_t unsigned int 
 5 /*该参数设置接受区大小*/
 6 
 7 typedef struct {
 8     
 9     int  read;//读指针
10     int  write;//写指针
11     int  Count;//缓冲区计数
12     int  length;//缓冲区大小
13     uint8_t  * buffer;// [RECERIVRSIZE];//接受缓冲区
14 }Df_FifoStructTypeDef, *Df_FifoPointTypeDef;
15 
16 
17 typedef enum Df_StateTypeDef
18 {
19     Df_StateOk,//成功
20     Df_StateErr,//失败
21     Df_StateTimeout,//超时
22     Df_StateOutOver,//读溢出
23     Df_StateInOver,//写溢出
24     Df_StateEaserOver//擦除溢出
25 }Df_StateTypeDef;
26 
27 
28 Df_StateTypeDef Df_FifoInput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length);
29 Df_StateTypeDef Df_FifoOutput(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint8_t length);
30 Df_StateTypeDef Df_FifoErase(Df_FifoPointTypeDef Pfifo, uint8_t length);
31 Df_StateTypeDef Df_FifoReset(Df_FifoPointTypeDef Pfifo, uint8_t * buf, uint16_t length);
32 
33 
34 #endif /*_DF_FIFO_H*/

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

原文地址:https://www.cnblogs.com/memorypro/p/7598193.html