Example of using Queue to pass strings between tasks

sam@logic
Posts: 3
Joined: Thu Sep 27, 2018 6:01 am

Re: Example of using Queue to pass strings between tasks

Postby sam@logic » Mon Oct 15, 2018 6:37 am

kolban wrote:To create a queue, you would use xQueueCreate ... see http://www.freertos.org/a00116.html

The size parameter would be large enough to hold a pointer to a character string ... for example:

Code: Select all

sizeof(char *)
When you have a string to place on the queue:

Code: Select all

char *myData = "helloWorld";
you could then allocate enough storage for it and copy it in:

Code: Select all

char *myItem = malloc(strlen(myData)+1); 
strcpy(myItem, myData);
You could then push the item onto the queue using xQueueSendToBack() ... see http://www.freertos.org/xQueueSendToBack.html

for example,

Code: Select all

xQueueSendToBack(myQueue, &myItem, portMAX_DELAY);
When you wish to receive an item ...

Code: Select all

char *myReceivedItem;
xQueueReceive(myQueue, &myReceivedItem, portMAX_DELAY);
// Do something with received string and then delete it ...
free(myReceivedItem);
See: http://www.freertos.org/a00118.html
Hi,
I want to send and recieve multiple strings to/from queue.
but as I send strings in queue one by one in one task and receive in other task, on recieve task it is only receiving first string that is sent on queue. I am getting what is the problem. please help me out for this problem.
below is my code.

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "string.h"

#define BUF 1024
xQueueHandle xQueue;


void consumer_task(void *pvParameter)
{
    char* rxmesage;


    while(1){
    	if( xQueue != 0 ) {

			if( (xQueuePeek( xQueue, &( rxmesage ), ( portTickType ) 10 )) == pdTRUE)
			{
			   printf("value received on queue: %s \n",rxmesage);
			   vTaskDelay(1500/portTICK_PERIOD_MS); //wait for 500 ms
			}
    }
    }
}

void producer_task(void *pvParameter){

	char *mydata="scanData={\"priority\":\"Normal\",\"device\":\"Reader1\",\"event\":\"STRAY\",\"timeStamp\":\"10/8/2018 11:16:10 PM\",\"scanItems\":[{\"thing\":\"%d%d\",\"deviceName\":\"%s\",\"mfid\":\"%X%X\",\"aid\":\"%u\",\"rid\":\"%u\",\"rssi\":%s,\"txPower\":%d,\"battery\":%u,\"scanTime\":\"25/8/2018 11:16:10 PM\",\"scanCount\":1,\"additionalData\":{\"mov\":\"%u\",\"temp\":%d,\"humidity\":%u,\"pressure\":\"%u\",\"location\":null}}],\"message\":\"Test\"}";
    char  *repl_data="";
    int count=0;
    while(1){
		 asprintf(&repl_data,"%d %s",count,mydata);
		 printf("value sent on queue: %s \n",repl_data);
         xQueueSend(xQueue,(void *)&repl_data,(TickType_t )0); // add the counter value to the queue

         vTaskDelay(500/portTICK_PERIOD_MS); //wait for a second
         count++;

    }
}

void app_main()
{
	uint8_t* dataSerial = (uint8_t*) malloc(BUF);
	xQueue = xQueueCreate( 1, sizeof(dataSerial));
    if(xQueue != NULL){
        printf("Queue is created\n");
        vTaskDelay(1000/portTICK_PERIOD_MS); //wait for a second
        xTaskCreate(&producer_task,"producer_task",1024*4,NULL,5,NULL);
        printf("producer task  started\n");
        xTaskCreate(&consumer_task,"consumer_task",1024*4,NULL,5,NULL);
        printf("consumer task  started\n");
    }else{
        printf("Queue creation failed");
    }
}
Last edited by sam@logic on Thu Nov 01, 2018 5:47 am, edited 2 times in total.

burkulesomesh43
Posts: 67
Joined: Tue Aug 14, 2018 6:21 am

Re: Example of using Queue to pass strings between tasks

Postby burkulesomesh43 » Mon Oct 15, 2018 9:33 am

Hi,
Is the queue data can be stored only in runtime??
after reset of esp32, it losses data stored in queue??

ESP_Sprite
Posts: 1997
Joined: Thu Nov 26, 2015 4:08 am

Re: Example of using Queue to pass strings between tasks

Postby ESP_Sprite » Tue Oct 16, 2018 2:54 am

Yes, queues in the end are just memory structures like all other FreeRTOS structures, and they'll get cleared on reset. Use NVS or RTC memory if you want to store stuff over a reset.

burkulesomesh43
Posts: 67
Joined: Tue Aug 14, 2018 6:21 am

Re: Example of using Queue to pass strings between tasks

Postby burkulesomesh43 » Wed Oct 17, 2018 5:40 am

ESP_Sprite wrote:Yes, queues in the end are just memory structures like all other FreeRTOS structures, and they'll get cleared on reset. Use NVS or RTC memory if you want to store stuff over a reset.
Ok. thanks

burkulesomesh43
Posts: 67
Joined: Tue Aug 14, 2018 6:21 am

Re: Example of using Queue to pass strings between tasks

Postby burkulesomesh43 » Sun Oct 21, 2018 11:22 am

Hi,
I am sending data stored in variable using asprintf(), but the data recieved from queue is garbage data.
I cant find what is the problem.

Code: Select all

char *single_tag=NULL;
int ret=asprintf(&single_tag,"Some Data:%s",data);
if(ret==-1)
 {
   ESP_LOGI(tag, "asprintf:memory allocation failed");
 }

ESP_LOGI(TAG, "single_tag:%s", single_tag);

 if(xQueueSend( xQueue, ( void * )&single_tag, ( portTickType )0 )==errQUEUE_FULL)
  {
      ESP_LOGI("Queue","Queue is full..");
  }
  	 bzero(single_tag, strlen(single_tag));
	 free(single_tag);

chegewara
Posts: 529
Joined: Wed Jun 14, 2017 9:00 pm

Re: Example of using Queue to pass strings between tasks

Postby chegewara » Sun Oct 21, 2018 4:28 pm

You have to initialize single_tag with malloc or similar before you can use it.

burkulesomesh43
Posts: 67
Joined: Tue Aug 14, 2018 6:21 am

Re: Example of using Queue to pass strings between tasks

Postby burkulesomesh43 » Sun Oct 21, 2018 7:01 pm

chegewara wrote:You have to initialize single_tag with malloc or similar before you can use it.
bur asprintf() will automatically allocate the memory...

ESP_Sprite
Posts: 1997
Joined: Thu Nov 26, 2015 4:08 am

Re: Example of using Queue to pass strings between tasks

Postby ESP_Sprite » Tue Oct 23, 2018 2:32 am

You should send a pointer to your data to xQueueSend to queue the data. You are sending a pointer to a pointer to the data instead, queueing the pointer. Remove the & in the xQueueSend and you should be good.

burkulesomesh43
Posts: 67
Joined: Tue Aug 14, 2018 6:21 am

Re: Example of using Queue to pass strings between tasks

Postby burkulesomesh43 » Fri Nov 02, 2018 2:15 pm

Hi,
How to find number of items are in queue?? is there any api?

ESP_Sprite
Posts: 1997
Joined: Thu Nov 26, 2015 4:08 am

Re: Example of using Queue to pass strings between tasks

Postby ESP_Sprite » Wed Nov 07, 2018 1:08 am

burkulesomesh43 wrote:Hi,
How to find number of items are in queue?? is there any api?
https://www.freertos.org/a00018.html#uc ... gesWaiting

Who is online

Users browsing this forum: No registered users and 2 guests