How can I manage communication between tasks using queues in FreeRTOS?

How can I manage communication between tasks using queues in FreeRTOS? My queue data seems to be getting lost, and I'm encountering the error: "Queue send failed". Here's my queue creation and send code:
xQueue = xQueueCreate(10, sizeof(int));
int value = 42;
xQueueSend(xQueue, &value, portMAX_DELAY);
xQueue = xQueueCreate(10, sizeof(int));
int value = 42;
xQueueSend(xQueue, &value, portMAX_DELAY);
@Middleware & OS @Helper
Solution:
@ZacckOsiemo It's been resolved and my code works better now after adding the function that confirms if the queue was actually created, regarding d syntax error seems I didn't add a ; were it was required, this little error caused me almost a day of headache, thanks for the help guys, @everyone This is my code now 👇...
Jump to solution
10 Replies
ZacckOsiemo
ZacckOsiemo•7mo ago
Are you checking that the queue is created successfully, Is there enough space on the heap? Are you calling xQueueSend in an ISR?
Boss lady
Boss lady•7mo ago
@ZacckOsiemo Am checking if it's created successfully, cause I keep getting the error queue send failed
ZacckOsiemo
ZacckOsiemo•7mo ago
start by checking that its actually created, then ensure you are calling xQueueSend in the correct context
youcef_ali
youcef_ali•7mo ago
First check that your queue has been created correctly, and before sending check that you have anough/available space in your queue
Boss lady
Boss lady•7mo ago
I tried adding a function to check if the queue is successfully created but now having a syntax error This my code
// Include necessary FreeRTOS header files
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Declare the queue handle
QueueHandle_t xQueue;

// Task1 function
void Task1(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int value = 42;
while (1) {
// Send value to the queue
if (xQueueSend(xQueue, &value, portMAX_DELAY) != pdPASS) {
// Queue send failed
printf("Queue send failed!\n");
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

// Task2 function
void Task2(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int receivedValue;
while (1) {
// Receive value from the queue
if (xQueueReceive(xQueue, &receivedValue, portMAX_DELAY) == pdPASS) {
// Successfully received value from the queue
printf("Received value: %d\n", receivedValue);
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000))
}
}

int main() {
// Create the queue
xQueue = xQueueCreate(10, sizeof(int));

// Check if queue is created
if (xQueue == NULL) {
// Queue creation failed
printf("Queue creation failed!\n");
return 1;
}

// Create Task1
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);

// Create Task2
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL)

// Start scheduler
vTaskStartScheduler();

while(1); // Should never reach here
return 0;
}
// Include necessary FreeRTOS header files
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Declare the queue handle
QueueHandle_t xQueue;

// Task1 function
void Task1(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int value = 42;
while (1) {
// Send value to the queue
if (xQueueSend(xQueue, &value, portMAX_DELAY) != pdPASS) {
// Queue send failed
printf("Queue send failed!\n");
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

// Task2 function
void Task2(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int receivedValue;
while (1) {
// Receive value from the queue
if (xQueueReceive(xQueue, &receivedValue, portMAX_DELAY) == pdPASS) {
// Successfully received value from the queue
printf("Received value: %d\n", receivedValue);
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000))
}
}

int main() {
// Create the queue
xQueue = xQueueCreate(10, sizeof(int));

// Check if queue is created
if (xQueue == NULL) {
// Queue creation failed
printf("Queue creation failed!\n");
return 1;
}

// Create Task1
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);

// Create Task2
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL)

// Start scheduler
vTaskStartScheduler();

while(1); // Should never reach here
return 0;
}
ZacckOsiemo
ZacckOsiemo•7mo ago
whats the error?
Boss lady
Boss lady•7mo ago
Syntax error
ZacckOsiemo
ZacckOsiemo•7mo ago
yeah what syntax error
Solution
Boss lady
Boss lady•7mo ago
@ZacckOsiemo It's been resolved and my code works better now after adding the function that confirms if the queue was actually created, regarding d syntax error seems I didn't add a ; were it was required, this little error caused me almost a day of headache, thanks for the help guys, @everyone This is my code now 👇
Boss lady
Boss lady•7mo ago
// Include necessary FreeRTOS header files
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Declare the queue handle
QueueHandle_t xQueue;

// Task1 function
void Task1(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int value = 42;
while (1) {
// Send value to the queue
if (xQueueSend(xQueue, &value, portMAX_DELAY) != pdPASS) {
// Queue send failed
printf("Queue send failed!\n");
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

// Task2 function
void Task2(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int receivedValue;
while (1) {
// Receive value from the queue
if (xQueueReceive(xQueue, &receivedValue, portMAX_DELAY) == pdPASS) {
// Successfully received value from the queue
printf("Received value: %d\n", receivedValue);
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000)); // Fixed: Added missing semicolon
}
}

int main() {
// Create the queue
xQueue = xQueueCreate(10, sizeof(int));

// Check if queue is created
if (xQueue == NULL) {
// Queue creation failed
printf("Queue creation failed!\n");
return 1;
}

// Create Task1
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);

// Create Task2
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL); // Fixed: Added missing semicolon

// Start scheduler
vTaskStartScheduler();

while(1); // Should never reach here
return 0;
}
// Include necessary FreeRTOS header files
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Declare the queue handle
QueueHandle_t xQueue;

// Task1 function
void Task1(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int value = 42;
while (1) {
// Send value to the queue
if (xQueueSend(xQueue, &value, portMAX_DELAY) != pdPASS) {
// Queue send failed
printf("Queue send failed!\n");
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

// Task2 function
void Task2(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int receivedValue;
while (1) {
// Receive value from the queue
if (xQueueReceive(xQueue, &receivedValue, portMAX_DELAY) == pdPASS) {
// Successfully received value from the queue
printf("Received value: %d\n", receivedValue);
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000)); // Fixed: Added missing semicolon
}
}

int main() {
// Create the queue
xQueue = xQueueCreate(10, sizeof(int));

// Check if queue is created
if (xQueue == NULL) {
// Queue creation failed
printf("Queue creation failed!\n");
return 1;
}

// Create Task1
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);

// Create Task2
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL); // Fixed: Added missing semicolon

// Start scheduler
vTaskStartScheduler();

while(1); // Should never reach here
return 0;
}
Want results from more Discord servers?
Add your server