FreeRTOS message queue transfer structure pointer and structure similarities and differences

Similarities and differences between message queue delivery structure pointer and structure

  1. Use queues to pass complex data types

    When the receiver receives a message sent from multiple sources to the queue, it needs to determine the source of the data. The method of use: the queue is used to transfer the structure, and the structure contains the data value and data source information .

    typedef struct
    {
          
          
        int  iValue;  //数据值
        int iMeaning; //数据来源信息
    }xData;
    
  2. Use the queue to transfer data pointers (faster processing speed)

    1. The ownership of the memory space pointed to by the pointer must be clear

      The contents of shared memory can only be accessed by the sending task before its pointer is sent to the queue;

      After the shared memory pointer is read from the queue, its content is only allowed to be accessed by the receiving task

    2. The memory space pointed to by the pointer must be valid

      The memory space pointed to by the pointer is dynamically allocated, and only one task should release its memory. When this memory space is released, no other tasks should access this space again.

//传递结构体的指针
/* 创建10个存储指针变量的消息队列,由于CM3/CM4内核是32位机,一个指针变量占用4个字节 */
xQueue2 = xQueueCreate(10, sizeof(struct Msg *));    //单元大小为一个指针变量的大小

//发送消息,实现结构体指针的传递 
MSG_T   *ptMsg;        //MSG_T为结构体声明
ptMsg = &g_tMsg;   /* 初始化结构体指针 */
// 初始化数组 
    ptMsg->ucMessageID = 0;
    ptMsg->ulData[0] = 0;
    ptMsg->usData[0] = 0;
//使用消息队列实现指针变量的传递 
 if(xQueueSend(xQueue2,                  /* 消息队列句柄 */
               (void *) &ptMsg,           // 发送结构体指针变量ptMsg的地址  “&”  取结构体指针的地址,传递指针
               (TickType_t)10) != pdPASS )
     
//接收消息,接收结构体的指针
MSG_T *ptMsg; //定义一个结构体指针
xResult = xQueueReceive(xQueue2,            /* 消息队列句柄 */
                       (void *)&ptMsg,      // 这里获取的是结构体的地址,类似于 char *a="stm";char *b;b=a 指针赋值,a和b指向同一个地址
                       (TickType_t)xMaxBlockTime);/* 设置阻塞时间 */
if(xResult == pdPASS)
            {
    
    
                /* 成功接收,并通过串口将数据打印出来 */
                printf("接收到消息队列数据ptMsg->ucMessageID = %d\r\n",ptMsg->ucMessageID);
                printf("接收到消息队列数据ptMsg->ulData[0] = %d\r\n", ptMsg->ulData[0]);
                printf("接收到消息队列数据ptMsg->usData[0] = %d\r\n", ptMsg->usData[0]);
            }     
//传递结构体本身
//创建一个消息队列
xQueue2 = xQueueCreate(10, sizeof(struct Msg));  //单员大小为结构体的大小  

//发送消息,实现结构体的传递 
MSG_T   ptMsg;       //MSG_T为结构体声明
//初始化数组
    ptMsg.ucMessageID = 0;
    ptMsg.ulData[0] = 0;
    ptMsg.usData[0] = 0;
 //使用消息队列实现指针变量的传递
 if(xQueueSend(xQueue2,                  /* 消息队列句柄 */
               (void *) &ptMsg,           // 发送结构体ptMsg的值,将值拷贝至队列中
               (TickType_t)10) != pdPASS )
     
//接收消息,接收结构体的值
MSG_T ptMsg; //定义一个结构体指针
xResult = xQueueReceive(xQueue2,            
                       (void *)&ptMsg,      // 这里获取的是结构体的值
                       (TickType_t)xMaxBlockTime);/* 设置阻塞时间 */

if(xResult == pdPASS)
            {
    
    
                /* 成功接收,并通过串口将数据打印出来 */
                printf("接收到消息队列数据ptMsg.ucMessageID = %d\r\n",ptMsg.ucMessageID);
                printf("接收到消息队列数据ptMsg.ulData[0] = %d\r\n", ptMsg.ulData[0]);
                printf("接收到消息队列数据ptMsg.usData[0] = %d\r\n", ptMsg.usData[0]);
            }     

The similarities and differences between the structure pointer and the structure itself

  1. When creating a message queue, the unit size declaration is different

  2. Variable initialization is different. One is to define a pointer, and the assigned structure entity is global , and the other is to define a structure

  3. Data output is different. The former uses the pointer member operator "->", and the latter uses the structure member operator

  4. When sending a message and requesting a message, the format of the two is the same, but the meaning is different. The former passes the address of the structure pointer, and the latter passes the value of the structure.

Reference link: https://www.cnblogs.com/yangguang-it/p/7204541.html
FreeRTOS detailed explanation, please click here

Guess you like

Origin blog.csdn.net/weixin_44333597/article/details/107523343