xQueueReceive

arunbm123 wrote on Monday, March 18, 2019:

Hello
I just started with freertos …In the following code
I am reciving Que Items in a loop
I am getting Same Item in the Loop
Whats going wrong ?

while(i<10){
						i++;
						struct AMessage *pxMessage;
						if (xQueueReceive( xQueue1, &pxMessage, 0)){
							ESP_LOGI(TAG, "---minor---%d == slno=%d" , pxMessage->minor ,pxMessage->slno);
							asprintf(&post_data, "%s,%d", data ,pxMessage->minor	 );
							free(data);
							asprintf(&data, "%s", post_data	 );
							free(post_data);
							}
						}**

heinbali01 wrote on Monday, March 18, 2019:

When you post source code, please put it between 2 lines that contain 3 tildas only:

```
    /* Write you source code between tildas. */
```

Beside that: could you also show the code that fills the queue? How is the queue created? How is it filled?

arunbm123 wrote on Monday, March 18, 2019:

hello Hein
Noted…
Please Find Image… of code How I am sending Items to Queue

heinbali01 wrote on Monday, March 18, 2019:

Are you saying that you put in different items, but when you pop them from the queue, they are all the same?

arunbm123 wrote on Monday, March 18, 2019:

hello Hein,
Yes.
I am observing the Queue is getting empty by 10, since I am looping 10times.
I suspect something with pointer

heinbali01 wrote on Monday, March 18, 2019:

Can’t you show show the complete code? Especially how the queue is created?
The pointer &xMessage is the address of the data that must me copied into the queue space.
You could say that you are passing the contents of the struct.

When you want to pass big amounts of data to a queue, it can be more efficient to only pass references (pointers) to the queue. In that case, you would pas a pointer to a pointer to xQueueSendToFront()

arunbm123 wrote on Monday, March 18, 2019:

hello sir,
please find code for Queue Creation


	struct AMessage {
		uint16_t minor;
		uint16_t rssi;
		uint16_t slno;
	}xMessage;

	xQueue1 = xQueueCreate(1000, sizeof( struct AMessage *) );

heinbali01 wrote on Monday, March 18, 2019:

The trick with the tilda’s seems to work! You can also escape a piece of text by putting it between `back-quotes`.

There you are:

xQueue1 = xQueueCreate(1000, sizeof( struct AMessage *) );

you defined the element size as big as a pointer, in stead of the struct size:

xQueue1 = xQueueCreate(1000, sizeof( struct AMessage) );

So infact only 4 ( or 8 ) bytes were copied from struct to queue and vv.

arunbm123 wrote on Monday, March 18, 2019:

hi
I refered an example at http://www.openrtos.net/a00116.html.
One more thing

if I dont use loop… it works fine…
while in loop same items are recived…
‘```’
int i=0;
char *post_data;
char *data;
asprintf(&data, “%s”, “,”);

				while(i<10){
					i++;
					struct AMessage *pxMessage;
					if (xQueueReceive( xQueue1, &(pxMessage), 0)){
						ESP_LOGI(TAG, "---minor---%d == slno=%d" , pxMessage->minor ,pxMessage->slno);
						asprintf(&post_data, "%s,%d", data ,pxMessage->minor	 );
						free(data);
						asprintf(&data, "%s", post_data	 );
						free(post_data);
						}
					}
				if(data != NULL)
					free(data);
         '`````````````````````````````````````````````````'

heinbali01 wrote on Monday, March 18, 2019:

Ok, now you are passing a pointer to a pointer. Did you also change the code that sends the structure pointers to the queue? What does that look like now?

richarddamon wrote on Monday, March 18, 2019:

If you pass a pointer to a structure to xQueue send, then the queue makes a copy of the pointer, but NOT the stucture. That means the sender needs to use a different structure for each send, or it it changes the contnets of the structure, it will change what the task that gets that pointer sees in the structure. Basically, somewhere you need to store all the different values of the structures you want to send. If you send the structure, then the queue will copy ALL the data into the queue, and then copy all the data out of the queue, which is simpler programming, but a lot of work for the processor. If you send just a pointer to the structure, then you avoid all the data copying, but need to manage the buffers.