MsgQueue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /********************************************************************************
  2. * File Name: MsgQueue.c
  3. * Function Describe: 共享消息数据缓冲区的消息队列
  4. * Explain:
  5. 此消息队列是一个环形数据结构,可以接受消息入队列,并提取消息出队列,队列最多可容纳MSG_QUEUE_NUM_MAX个消息
  6. 每个消息队列使用一个环形结构的消息数据缓冲区,该消息队列内的所有消息将共用此数据缓冲区
  7. 适用于串口接收的数据处理,尤其适用于与模块的串口接收数据处理。
  8. 用法:
  9. 先定义消息队列SUT_MSG_QUEUE实例,并定义存放消息数据缓冲区实例
  10. 然后用MsgQueueInit初始化消息队列
  11. 用MsgQueuePost将消息入队列,入队列时将拷贝消息的数据内容到SUT_MSG_QUEUE实例的数据缓冲区中。
  12. 用MsgQueueAccept将从队列中取消息,取消息时将从数据缓冲区中拷贝数据到指定的其他非环形缓冲区,以便处理消息
  13. * Writer: ShiLiangWen
  14. * Date: 2015-7-2
  15. *******************************************************************************/
  16. #define THIS_FILE_ID 5
  17. /********************************************************************************/
  18. #include "includes.h"
  19. /********************************************************************************************
  20. *
  21. *********************************************************************************************/
  22. void MsgQueueInit(SUT_MSG_QUEUE *pMsgQueue,char *pDataBuffer,unsigned short DataBufferLenMax)
  23. {
  24. int i;
  25. pMsgQueue->MsgIn=0;
  26. pMsgQueue->MsgNum=0;
  27. pMsgQueue->MsgOut=0;
  28. for(i=0;i<MSG_QUEUE_NUM_MAX;i++){
  29. pMsgQueue->MsgQueue[i].DataStartIndex=0;
  30. pMsgQueue->MsgQueue[i].MsgLen=0;
  31. }
  32. //
  33. pMsgQueue->DataBuffer=pDataBuffer;
  34. for(i=0;i<DataBufferLenMax;i++)pMsgQueue->DataBuffer[i]=0;
  35. pMsgQueue->DataBufferLenMax=DataBufferLenMax;
  36. pMsgQueue->DataBufferIn=0;
  37. pMsgQueue->DataBufferOut=0;
  38. pMsgQueue->DataBufferLen=0;
  39. }
  40. /********************************************************************************************
  41. *
  42. *********************************************************************************************/
  43. void MsgQueuePost(SUT_MSG_QUEUE *pMsgQueue,char *pData,unsigned short DataLen)
  44. {
  45. unsigned short i;
  46. //队列满,不允许再入队列
  47. if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
  48. //SlwTrace("MsgQueue full!\r\n");
  49. return;
  50. }
  51. //数据缓冲区不够,不允许入队列
  52. if((DataLen+pMsgQueue->DataBufferLen)>pMsgQueue->DataBufferLenMax){
  53. //SlwTrace("DataBuffer full!\r\n");
  54. return;
  55. }
  56. RunMake(THIS_FILE_ID);
  57. //消息入队列
  58. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn;
  59. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
  60. RunMake(THIS_FILE_ID);
  61. //消息数据入缓冲区
  62. for(i=0;i<DataLen;i++){
  63. pMsgQueue->DataBuffer[pMsgQueue->DataBufferIn]=pData[i];
  64. pMsgQueue->DataBufferIn++;
  65. if(pMsgQueue->DataBufferIn>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferIn=0;
  66. pMsgQueue->DataBufferLen++;
  67. }
  68. RunMake(THIS_FILE_ID);
  69. //消息个数增加
  70. pMsgQueue->MsgIn++;
  71. if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
  72. pMsgQueue->MsgNum++;
  73. }
  74. /********************************************************************************************
  75. *
  76. *********************************************************************************************/
  77. void MsgQueuePostLoopBuf(SUT_MSG_QUEUE *pMsgQueue,char *pLoopBuf,unsigned short LoopDataBufLen,unsigned short LoopDataStart,unsigned short DataLen)
  78. {
  79. unsigned short i;
  80. unsigned short LoopDataIndex;
  81. //队列满,不允许再入队列
  82. if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
  83. SlwTrace(DEBUG,"MsgQueue full!\r\n");
  84. return;
  85. }
  86. //数据缓冲区不够,不允许入队列
  87. if((DataLen+pMsgQueue->DataBufferLen)>=pMsgQueue->DataBufferLenMax){
  88. SlwTrace(DEBUG,"MsgQueue DataBuffer full!\r\n");
  89. return;
  90. }
  91. RunMake(THIS_FILE_ID);
  92. //消息入队列
  93. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn;
  94. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
  95. RunMake(THIS_FILE_ID);
  96. //消息数据入缓冲区
  97. LoopDataIndex=LoopDataStart;
  98. for(i=0;i<DataLen;i++){
  99. pMsgQueue->DataBuffer[pMsgQueue->DataBufferIn]=pLoopBuf[LoopDataIndex];
  100. if(++LoopDataIndex>=LoopDataBufLen)LoopDataIndex=0;
  101. pMsgQueue->DataBufferIn++;
  102. if(pMsgQueue->DataBufferIn>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferIn=0;
  103. pMsgQueue->DataBufferLen++;
  104. }
  105. RunMake(THIS_FILE_ID);
  106. //消息个数增加
  107. pMsgQueue->MsgIn++;
  108. if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
  109. pMsgQueue->MsgNum++;
  110. RunMake(THIS_FILE_ID);
  111. }
  112. /********************************************************************************************
  113. 提取pMsgQueue消息,并放入pBuf中,给定pBuf空间长度为BufLen
  114. 返回r:
  115. r=0 --无消息
  116. r>0 --有消息,并已拷贝消息数据到pData,r为消息数据长度
  117. r<0 --有消息,但接受数据缓冲区长度不够,r为长度的负数
  118. *********************************************************************************************/
  119. int MsgQueueAccept(SUT_MSG_QUEUE *pMsgQueue,char *pBuf,unsigned short BufLen)
  120. {
  121. int len;
  122. unsigned short i,j;
  123. char data;
  124. SUT_MESSAGE msg;
  125. tsk_lock ();
  126. BufLen-=1;//流出一个字节保存/0
  127. RunMake(THIS_FILE_ID);
  128. if(pMsgQueue->MsgNum==0){
  129. tsk_unlock ();
  130. return 0;
  131. }
  132. msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen;
  133. msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex;
  134. j=0;
  135. for(i=0;i<msg.MsgLen;i++){
  136. if(0==pMsgQueue->DataBufferLen)break;
  137. data=pMsgQueue->DataBuffer[pMsgQueue->DataBufferOut];
  138. pMsgQueue->DataBufferOut++;
  139. if(pMsgQueue->DataBufferOut>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferOut=0;
  140. pMsgQueue->DataBufferLen--;
  141. if(j<BufLen)pBuf[j++]=data;
  142. }
  143. pMsgQueue->MsgOut++;
  144. if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0;
  145. pMsgQueue->MsgNum--;
  146. pBuf[j]=0;
  147. RunMake(THIS_FILE_ID);
  148. if(i!=msg.MsgLen){
  149. SlwTrace(DEBUG,"MsgQueueAccept Err=-2\r\n");
  150. len=-1*msg.MsgLen;
  151. tsk_unlock ();
  152. return len;//Databuffer len error!
  153. }
  154. if(msg.MsgLen>BufLen){
  155. SlwTrace(DEBUG,"MsgQueueAccept Err=-1\r\n");
  156. len=-1*msg.MsgLen;
  157. tsk_unlock ();
  158. return len;
  159. }else{
  160. tsk_unlock ();
  161. return msg.MsgLen;
  162. }
  163. }
  164. /********************************************************************************************
  165. *
  166. *********************************************************************************************/