MsgQueue.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. unsigned char msgBuffer[MSG_DATA_BUF_LEN];
  20. SUT_MSG_QUEUE msgQue;
  21. void MsgQueueSet(void)
  22. {
  23. MsgQueueInit((void *)msgBuffer,MSG_DATA_BUF_LEN);
  24. }
  25. /********************************************************************************************
  26. *
  27. *********************************************************************************************/
  28. void MsgQueueInit(char *pDataBuffer,unsigned short DataBufferLenMax)
  29. {
  30. int i;
  31. SUT_MSG_QUEUE *pMsgQueue = &msgQue;
  32. pMsgQueue->MsgIn=0;
  33. pMsgQueue->MsgNum=0;
  34. pMsgQueue->MsgOut=0;
  35. for(i=0;i<MSG_QUEUE_NUM_MAX;i++){
  36. pMsgQueue->MsgQueue[i].DataStartIndex=0;
  37. pMsgQueue->MsgQueue[i].MsgLen=0;
  38. }
  39. //
  40. pMsgQueue->DataBuffer=pDataBuffer;
  41. for(i=0;i<DataBufferLenMax;i++)pMsgQueue->DataBuffer[i]=0;
  42. pMsgQueue->DataBufferLenMax=DataBufferLenMax;
  43. pMsgQueue->DataBufferIn=0;
  44. pMsgQueue->DataBufferOut=0;
  45. pMsgQueue->DataBufferLen=0;
  46. }
  47. /********************************************************************************************
  48. *
  49. *********************************************************************************************/
  50. int MsgQueuePost(char *pData,unsigned short DataLen)
  51. {
  52. SUT_MSG_QUEUE *pMsgQueue = &msgQue;
  53. unsigned short i;
  54. //队列满,不允许再入队列
  55. //SlwTrace(INF, "in MSQ\r\n");
  56. if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
  57. //SlwTrace(INF, "MsgQueue full!\r\n");
  58. return -1;
  59. }
  60. //数据缓冲区不够,不允许入队列
  61. if((DataLen+pMsgQueue->DataBufferLen)>pMsgQueue->DataBufferLenMax){
  62. //SlwTrace(INF,"DataBuffer full!\r\n");
  63. return -2;
  64. }
  65. //消息入队列
  66. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn;
  67. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
  68. //消息数据入缓冲区
  69. for(i=0;i<DataLen;i++){
  70. pMsgQueue->DataBuffer[pMsgQueue->DataBufferIn]=pData[i];
  71. pMsgQueue->DataBufferIn++;
  72. if(pMsgQueue->DataBufferIn>=MSG_DATA_BUF_LEN)pMsgQueue->DataBufferIn=0;
  73. pMsgQueue->DataBufferLen++;
  74. }
  75. //消息个数增加
  76. pMsgQueue->MsgIn++;
  77. if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
  78. pMsgQueue->MsgNum++;
  79. return 0;
  80. }
  81. /********************************************************************************************
  82. 提取pMsgQueue消息,并放入pBuf中,给定pBuf空间长度为BufLen
  83. 返回r:
  84. r=0 --无消息
  85. r>0 --有消息,并已拷贝消息数据到pData,r为消息数据长度
  86. r<0 --有消息,但接受数据缓冲区长度不够
  87. *********************************************************************************************/
  88. int MsgQueueAccept(char *pBuf,unsigned short BufLen)
  89. {
  90. SUT_MSG_QUEUE *pMsgQueue = &msgQue;
  91. unsigned short i,j;
  92. char data;
  93. SUT_MESSAGE msg;
  94. if(pMsgQueue->MsgNum==0)return 0;
  95. //SlwTrace(INF, "out MSQ\r\n");
  96. msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen;
  97. msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex;
  98. j=0;
  99. for(i=0;i<msg.MsgLen;i++){
  100. if(0==pMsgQueue->DataBufferLen)break;
  101. data=pMsgQueue->DataBuffer[pMsgQueue->DataBufferOut];
  102. pMsgQueue->DataBufferOut++;
  103. if(pMsgQueue->DataBufferOut>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferOut=0;
  104. pMsgQueue->DataBufferLen--;
  105. if(j<BufLen)pBuf[j++]=data;
  106. }
  107. pMsgQueue->MsgOut++;
  108. if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0;
  109. pMsgQueue->MsgNum--;
  110. if(i!=msg.MsgLen){
  111. return -2;//Databuffer len error!
  112. }
  113. if(msg.MsgLen>BufLen){
  114. return -1;
  115. }else{
  116. return msg.MsgLen;
  117. }
  118. }
  119. /********************************************************************************************
  120. *
  121. *********************************************************************************************/