FreeRTOS on esp32, sending structure via queue

Hello, I am trying to send structure via queue in FreeRTOS. This is my code:

#include "queue.h"

typedef int taskProfiler;

typedef enum
{
  eSender1 = 0,
  eSender2,
}DataSource_t;

typedef struct
{
  uint8_t ucValue;
  DataSource_t eDataSource;
}Data_t;

static Data_t xStructToSend[2] = 
{
  (100, eSender1),
  (50, eSender2)
};

QueueHandle_t xQueue;

taskProfiler senderTaskProfiler = 0;
taskProfiler receiverTaskProfiler = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(5000);
  Serial.println(xStructToSend[1].ucValue);
  
  xQueue = xQueueCreate(3, sizeof(Data_t));

  xTaskCreate(vSenderTask, "Sender Task 1", 8192, &(xStructToSend[0]), 2, NULL);
  xTaskCreate(vSenderTask, "Sender Task 2", 8192, &(xStructToSend[1]), 2, NULL);

  xTaskCreate(vReceiverTask, "Receiver Task", 8192, NULL, 2, NULL);
}

void vSenderTask(void *pvParameters)
{
  BaseType_t xStatus;
  const TickType_t xTicksToWait = pdMS_TO_TICKS(100);
  Data_t xReceivedStructure;
  memcpy(&xReceivedStructure, &pvParameters, sizeof(pvParameters));
  while(1)
  {
    xStatus = xQueueSend(xQueue, pvParameters, xTicksToWait);

    if(xStatus != pdPASS)
    {
      Serial.println("Could not send to the Queue");
    }
    Serial.println(xReceivedStructure.ucValue);
  }
}

void vReceiverTask(void *pvParameters)
{
  Data_t xReceivedStructure;
  BaseType_t xStatus;
  while(1)
  {
    xStatus = xQueueReceive(xQueue, &xReceivedStructure, 0);

    if(xStatus == pdPASS)
    {
      if(xReceivedStructure.eDataSource == eSender1)
      {
        Serial.print("This is from Sender1: ");
        Serial.println(xReceivedStructure.ucValue);
      }
      else
      {
        Serial.print("This is from Sender2: ");
        Serial.println(xReceivedStructure.ucValue);
      }
    }
    else
    {
      Serial.println("Could not receive data from the queue");
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

In line "Serial.println(xStructToSend[1].ucValue);" (in void setup()) I get value 0 and I should get 50. The same issue is in line "Serial.println(xReceivedStructure.ucValue);" ( in void vSenderTask(void *pvParameters)). In receiver task i get only values 152 and 160. That looks like I have some error in inicialization structure values. How can I deal with this problem? Thanks in advance for your help.
terminal

Hello

Your array is not initialized correctly, change parenthesis to brackets:

static Data_t xStructToSend[2] = 
{
  { 100, eSender1 },
  { 50, eSender2 }
};
1 Like

That solved my problem. Thank you very much!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.