直接上代码
#pragma once
#ifndef QLIST_H_
#define QLIST_H_
void commInit();
void commDataIn(int * pData, int len);
int commGetDataLen();
int commGetData(int *pRevcBuff, int len);
#endif
#include "stdafx.h"
#include "Qlist.h"
/***************************************************************
* COMM_BUFF_MAX 设置缓存区大小
* void commDataIn(int * pData, int len); 存入数据(存入指针数组,长度)
* int commGetDataLen(); 获取当前环形缓存区长度
* int commGetData(int *pRevcBuff, int len); 获取数据(获取后返回的指针数组,要获取长度). 返回值:成功:返回长度; 失败:-1 (超过缓存内的数据)
*
***************************************************************/
#define COMM_BUFF_MAX 128
int comm_bufferRecv[COMM_BUFF_MAX];
int comm_bufferNextWritePos=0;
int comm_bufferNextReadPos=0;
int comm_bufferDataCount=0;
//传入数据
void commDataIn(int * pData, int len)
{
for (int i = 0; i < len; i++)
{
if (comm_bufferDataCount < COMM_BUFF_MAX)
{
comm_bufferRecv[comm_bufferNextWritePos++] = pData[i];
comm_bufferDataCount++;
if (comm_bufferNextWritePos >= COMM_BUFF_MAX)
{
comm_bufferNextWritePos = 0;
}
}
}
}
int commGetDataLen()
{
return comm_bufferDataCount;
}
int commGetData(int *pRevcBuff, int len)
{
int i;
if (len <= comm_bufferDataCount)
{
for (i = 0; i<len; i++)
{
pRevcBuff[i] = comm_bufferRecv[comm_bufferNextReadPos++];
comm_bufferDataCount--;
if (comm_bufferNextReadPos >= COMM_BUFF_MAX)
{
comm_bufferNextReadPos = 0;
}
}
return len;
}
else
{
return -1; //超出长度
}
}
commDataIn(buf, n); //存入数据
int len = commGetData(buf, n); //取出数据