123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /********************************************************************************
- * File Name: MsgQueue.c
- * Function Describe: 共享消息数据缓冲区的消息队列
- * Explain:
- 此消息队列是一个环形数据结构,可以接受消息入队列,并提取消息出队列,队列最多可容纳MSG_QUEUE_NUM_MAX个消息
- 每个消息队列使用一个环形结构的消息数据缓冲区,该消息队列内的所有消息将共用此数据缓冲区
- 适用于串口接收的数据处理,尤其适用于与模块的串口接收数据处理。
- 用法:
- 先定义消息队列SUT_MSG_QUEUE实例,并定义存放消息数据缓冲区实例
- 然后用MsgQueueInit初始化消息队列
- 用MsgQueuePost将消息入队列,入队列时将拷贝消息的数据内容到SUT_MSG_QUEUE实例的数据缓冲区中。
- 用MsgQueueAccept将从队列中取消息,取消息时将从数据缓冲区中拷贝数据到指定的其他非环形缓冲区,以便处理消息
- * Writer: ShiLiangWen
- * Date: 2015-7-2
- *******************************************************************************/
- #define THIS_FILE_ID 5
- /********************************************************************************/
- #include "includes.h"
- unsigned char msgBuffer[MSG_DATA_BUF_LEN];
- SUT_MSG_QUEUE msgQue;
- void MsgQueueSet(void)
- {
- MsgQueueInit((void *)msgBuffer,MSG_DATA_BUF_LEN);
- }
- /********************************************************************************************
- *
- *********************************************************************************************/
- void MsgQueueInit(char *pDataBuffer,unsigned short DataBufferLenMax)
- {
- int i;
- SUT_MSG_QUEUE *pMsgQueue = &msgQue;
- pMsgQueue->MsgIn=0;
- pMsgQueue->MsgNum=0;
- pMsgQueue->MsgOut=0;
- for(i=0;i<MSG_QUEUE_NUM_MAX;i++){
- pMsgQueue->MsgQueue[i].DataStartIndex=0;
- pMsgQueue->MsgQueue[i].MsgLen=0;
- }
- //
- pMsgQueue->DataBuffer=pDataBuffer;
- for(i=0;i<DataBufferLenMax;i++)pMsgQueue->DataBuffer[i]=0;
- pMsgQueue->DataBufferLenMax=DataBufferLenMax;
- pMsgQueue->DataBufferIn=0;
- pMsgQueue->DataBufferOut=0;
- pMsgQueue->DataBufferLen=0;
- }
- /********************************************************************************************
- *
- *********************************************************************************************/
- int MsgQueuePost(char *pData,unsigned short DataLen)
- {
- SUT_MSG_QUEUE *pMsgQueue = &msgQue;
- unsigned short i;
- //队列满,不允许再入队列
- //SlwTrace(INF, "in MSQ\r\n");
- if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
- //SlwTrace(INF, "MsgQueue full!\r\n");
- return -1;
- }
- //数据缓冲区不够,不允许入队列
- if((DataLen+pMsgQueue->DataBufferLen)>pMsgQueue->DataBufferLenMax){
- //SlwTrace(INF,"DataBuffer full!\r\n");
- return -2;
- }
- //消息入队列
- pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn;
- pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
- //消息数据入缓冲区
- for(i=0;i<DataLen;i++){
- pMsgQueue->DataBuffer[pMsgQueue->DataBufferIn]=pData[i];
- pMsgQueue->DataBufferIn++;
- if(pMsgQueue->DataBufferIn>=MSG_DATA_BUF_LEN)pMsgQueue->DataBufferIn=0;
- pMsgQueue->DataBufferLen++;
- }
- //消息个数增加
- pMsgQueue->MsgIn++;
- if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
- pMsgQueue->MsgNum++;
- return 0;
- }
- /********************************************************************************************
- 提取pMsgQueue消息,并放入pBuf中,给定pBuf空间长度为BufLen
- 返回r:
- r=0 --无消息
- r>0 --有消息,并已拷贝消息数据到pData,r为消息数据长度
- r<0 --有消息,但接受数据缓冲区长度不够
- *********************************************************************************************/
- int MsgQueueAccept(char *pBuf,unsigned short BufLen)
- {
- SUT_MSG_QUEUE *pMsgQueue = &msgQue;
- unsigned short i,j;
- char data;
- SUT_MESSAGE msg;
-
- if(pMsgQueue->MsgNum==0)return 0;
- //SlwTrace(INF, "out MSQ\r\n");
- msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen;
- msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex;
- j=0;
- for(i=0;i<msg.MsgLen;i++){
- if(0==pMsgQueue->DataBufferLen)break;
- data=pMsgQueue->DataBuffer[pMsgQueue->DataBufferOut];
- pMsgQueue->DataBufferOut++;
- if(pMsgQueue->DataBufferOut>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferOut=0;
- pMsgQueue->DataBufferLen--;
- if(j<BufLen)pBuf[j++]=data;
- }
- pMsgQueue->MsgOut++;
- if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0;
- pMsgQueue->MsgNum--;
-
- if(i!=msg.MsgLen){
- return -2;//Databuffer len error!
- }
-
- if(msg.MsgLen>BufLen){
- return -1;
- }else{
- return msg.MsgLen;
- }
- }
- /********************************************************************************************
- *
- *********************************************************************************************/
|