Example of using Queue to pass strings between tasks
Example of using Queue to pass strings between tasks
I am looking for an example where a char string is passed between tasks (producer/consumer).
Most of the examples I have seen are using numbers like int.
Thanks in advance
Most of the examples I have seen are using numbers like int.
Thanks in advance
-
- Posts: 9739
- Joined: Thu Nov 26, 2015 4:08 am
Re: Example of using Queue to pass strings between tasks
A queue actually is made for items with a defined length; if you throw in strings it'll waste a bunch of memory. Maybe you want to look at the ringbuffer implementation (in components/freertos/ringbuf.c and components/freertos/include/freertos/ringbuf.h) instead? Unfortunately, I also do not have an example for that (although, if I recall correctly, there should be an unit test for that thing somewhere.)
Re: Example of using Queue to pass strings between tasks
Thank you for the response. Will check ringbuffer. Saw the unit test and will check the code from there.ESP_Sprite wrote:A queue actually is made for items with a defined length; if you throw in strings it'll waste a bunch of memory. Maybe you want to look at the ringbuffer implementation (in components/freertos/ringbuf.c and components/freertos/include/freertos/ringbuf.h) instead? Unfortunately, I also do not have an example for that (although, if I recall correctly, there should be an unit test for that thing somewhere.)
I started off with Queue since I saw it being used for UART events sample. Is that also a ringbuffer behind the scene?
I am new to electronics, ESP32 & FreeRTOS, so sorry for the newbie questions
(Just 2 weeks with ESP32 & FreeRTOS)
-
- Posts: 9739
- Joined: Thu Nov 26, 2015 4:08 am
Re: Example of using Queue to pass strings between tasks
That is because events only have a limited, fixed size. I think the UART driver uses a ringbuffer as well for the incoming/outgoing data.
Re: Example of using Queue to pass strings between tasks
I have used queues to pass strings around in some of my projects. What I do is allocate storage for the string (so that it is not on stack) and then add a pointer to the string into the queue. The reader of the queue then sees a new entry which is a pointer to the string, works with the string and then deletes the allocated storage. The queue elements then become fixed size ... which is of course the size of a memory pointer. The un-written contract then becomes that the storage pointed to by the item on the queue has to be deleted/freed by the consumer.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: Example of using Queue to pass strings between tasks
Sounds good. Do you have any code samples for it I can check?kolban wrote:I have used queues to pass strings around in some of my projects. What I do is allocate storage for the string (so that it is not on stack) and then add a pointer to the string into the queue. The reader of the queue then sees a new entry which is a pointer to the string, works with the string and then deletes the allocated storage. The queue elements then become fixed size ... which is of course the size of a memory pointer. The un-written contract then becomes that the storage pointed to by the item on the queue has to be deleted/freed by the consumer.
Re: Example of using Queue to pass strings between tasks
So what I am trying to do is
TCP Socket server on a device (ESP32) emitting string data (not json now but maybe later) coming from different peripherals (3 for now).
Client is more like a wireless display in this scenario:
1. TCP Socket client on another ESP32 device
2. String of information comes through TCP socket
3. Add the string to queue
4. Get one string at a time from queue
5 . Process and display information on TFT
I found a nice sample here... but have not been able to make it work yet, so in the process of writing my own
https://github.com/finger563/esp32-wireless-display
TCP Socket server on a device (ESP32) emitting string data (not json now but maybe later) coming from different peripherals (3 for now).
Client is more like a wireless display in this scenario:
1. TCP Socket client on another ESP32 device
2. String of information comes through TCP socket
3. Add the string to queue
4. Get one string at a time from queue
5 . Process and display information on TFT
I found a nice sample here... but have not been able to make it work yet, so in the process of writing my own
https://github.com/finger563/esp32-wireless-display
Re: Example of using Queue to pass strings between tasks
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:
When you have a string to place on the queue:
you could then allocate enough storage for it and copy it in:
You could then push the item onto the queue using xQueueSendToBack() ... see http://www.freertos.org/xQueueSendToBack.html
for example,
When you wish to receive an item ...
See: http://www.freertos.org/a00118.html
The size parameter would be large enough to hold a pointer to a character string ... for example:
Code: Select all
sizeof(char *)
Code: Select all
char *myData = "helloWorld";
Code: Select all
char *myItem = malloc(strlen(myData)+1);
strcpy(myItem, myData);
for example,
Code: Select all
xQueueSendToBack(myQueue, &myItem, portMAX_DELAY);
Code: Select all
char *myReceivedItem;
xQueueReceive(myQueue, &myReceivedItem, portMAX_DELAY);
// Do something with received string and then delete it ...
free(myReceivedItem);
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: Example of using Queue to pass strings between tasks
Thank you so much Neil. Will try it out and come back
Re: Example of using Queue to pass strings between tasks
In the interest of people who might be searching for a solution for same kind of requirements...
I ended up using Ring Buffer and the sample over here really helped.
Documentation has really improved.
https://esp-idf.readthedocs.io/en/lates ... ffercreate
I ended up using Ring Buffer and the sample over here really helped.
Documentation has really improved.
https://esp-idf.readthedocs.io/en/lates ... ffercreate
Who is online
Users browsing this forum: No registered users and 13 guests