Search code examples
ccharvoid-pointersfreertos

How to print char buffer on FreeRtos returned by function xQueueReceive


I am using Free RTOS for a project.

I have a higher priority task appending char buffers to a Free RTOS queue and a smaller priority task popping the char buffers from the queue and printing them.

QueueHandle_t xQueue1 = xQueueCreate(10,50);

//higher priority task
for(;;){
   char buffer[50];
   sprintf(buffer, "%s, %d\n", t1[i].name, tman_tick);
                
   if( xQueue1 != 0 ){
      if( xQueueSend( xQueue1,( void * ) &buffer,( TickType_t ) 0 ) != pdPASS ){
      /* Failed to post the message, even after 10 ticks. */
      }
   }
}

//lower priority task
for(;;){
     void * xStructure;
     char *ptemp;
     if( xQueueReceive( xQueue1,& (xStructure),( TickType_t ) 10 ) == pdPASS ){
      /* xStructure now contains a copy of xMessage. */
         printf("ENTERS HERE\n");
         ptemp = (char *)xStructure;
         printf("HERE TOO\n");
         printf("%s\n", ptemp);    
     }       
}

I think everything is working except the last print. It print "ENTERS HERE" and "HERE TOO", but after the conversion to a char array, it can not print the ptemp and I don't know why. Am I doing the conversion in a wrong way?


Solution

  • From the FreeRTOS documentation:

    pvBuffer A pointer to the memory into which the received data will be copied.
    

    You have to provide a pointer to allocated memory. As your queue items are 50 bytes long you need to pass a pointer to a chunk of memory at least 50 bytes long.

    char xStructure[50];
    if (xQueueReceive( xQueue1, xStructure, (TickType_t)10) == pdPASS ){
    }