// 文件名称: Queue.c
// 功能描述: 循环队列存储结构,用于保存串口发来的信息
// 维护记录: 2007-8-10 v1.0
//======================================================
#include "error.h"
#include "string.h"
#define MAXQSIZE 7
static char base[MAXQSIZE][100];//队列存储数组,可存储的最大字符串为100个字符
static int front; //队头指针
static int rear; //队尾指针
//========================================================================
// 语法格式: int Queue_Enter(char *str)
// 实现功能: 字符串入队操作
// 参数: str: 要入队的字符串
// 返回值: 正常入队返回OK, 失败返回ERROR
//========================================================================
int Queue_Enter(char *str)
{
if( (rear + 1) % MAXQSIZE == front )
return ERROR; //队列满,则返回错误
strcpy(base[rear],str);
rear = (rear + 1) % MAXQSIZE;
return OK;
}
//========================================================================
// 语法格式: int Queue_Delete(char *str)
// 实现功能: 字符串出队操作
// 参数: str: 保存出队的字符串
// 返回值: 正常出队返回OK, 失败返回ERROR
//========================================================================
int Queue_Delete(char *str)
{
if( front == rear )
return ERROR; //队列空,则退出
strcpy(str,base[front]);
front =(front + 1) % MAXQSIZE;
return OK;
}
//========================================================================
// 语法格式: int Queue_Clear(void)
// 实现功能: 清空队列中存在的所有元素
// 参数: 无
// 返回值: 返回OK,
//========================================================================
int Queue_Clear(void)
{
front = 0;
rear = 0;
return OK;
}
串口接收中断里面 调用 Queue_Enter
I_Buf[i ++ ] = ch;
if(ch == '\n')
{
Queue_Enter(I_Buf);// 当接收到一条完整的消息时,入队
i = 0;
}
在使用串口数据的时候调用Queue_Delete
Queue_Solid=Queue_Delete(Buf);
http://wenku.baidu.com/view/805615e2524de518964b7d89.html