Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Problem whith xQueueSend and array

Posted by Dennis Ignazio Virgo on October 26, 2008
Hello,

I'm a newbie in FreeRTOS and C. While trying out I had some trouble in understanding why source code A does work, but B and C do not work properly.

/*** simplified functions ***/
void vStringOutput (portCHAR *pcSomeString);

void vFunctionWithQueueReceive(void)
{
portCHAR *pcReceivedString;
xQueueHandle xQueue;

xQueue = xQueueCreate((20, sizeof(portCHAR *));

if(xQueue != 0)
{
if(xQueueReceive(xQueue, &pcReceivedString, 10))
{
xStringOutput(pcReceivedString);
}
}
}


A: This works perfectly
void vFunctionWithQueueSend(void)
{
...
portChar *pcAString = "abcdefg";
...
xQueueSend(xQueue, &pcAString1, 10);
...
}

B: This puts out wierd characters
void vFunctionWithQueueSend(void)
{
...
portChar cAnArray[] = "abcdefg";
...
xQueueSend(xQueue, &cAnArray, 10);
...
}

C: This puts out wierd characters
void vFunctionWithQueueSend(void)
{
...
xQueueSend(xQueue, (void *)"abcdefg", 10);
...
}

How can I manage to put a char array in a Queue?

Thank you in advance.

Dennis

RE: Problem whith xQueueSend and array

Posted by Richard on October 26, 2008
> A: This works perfectly
> void vFunctionWithQueueSend(void)
> {
> ...
> portChar *pcAString = "abcdefg";
> ...
> xQueueSend(xQueue, &pcAString1, 10);
> ...
> }

pcAString is a pointer to a const string, which is probably stored in Flash. The pointer contains the address of data that is in flash and is always there.

> B: This puts out wierd characters
> void vFunctionWithQueueSend(void)
> {
> ...
> portChar cAnArray[] = "abcdefg";
> ...
> xQueueSend(xQueue, &cAnArray, 10);
> ...
> }


You have declared an array as a local (stack) variable within vFunctionWithQueueSend(). Once vFunctionWithQueueSend() exits the variable will no longer exist and its value in RAM will probably be overwritten during the next function call.

>
> C: This puts out wierd characters
> void vFunctionWithQueueSend(void)
> {
> ...
> xQueueSend(xQueue, (void *)"abcdefg", 10);
> ...
> }

Here you are posting a character pointer (char *), but your queue receive function is expecting a pointer to a character pointer (char **).

These statements are just general C pointers, the same would be true if you were using FreeRTOS.org or not.

Regards.

RE: Problem whith xQueueSend and array

Posted by Dennis Ignazio Virgo on October 26, 2008
Thank you very much! I thought that my problem has to do with FreeRTOS, but now I understand that it is an C issue. I have to read more about pointer... :)

With best Regards
Dennis

RE: Problem whith xQueueSend and array

Posted by Dennis Ignazio Virgo on October 26, 2008
After thinking to understand the problem and after many trials I'm not able to pass the array in the queue like intendet in B.
If possible i want to avoid global arrays, so I have to use a local one.
I tryed to allocate dynamic memory for a pointer and copied each array element to the allocated memory, but it didn't work.
Without using a queue I would try to call the function tranferring a pointer and work with a reference or maybe I would try a function which returns a pointer.
Since I have to use xQueueSend() and I read that the items are passed by copy, I have no ideas how to continue.

Are there any hints?

What possibilities do I have to send the array from example B threadsave to the Queue?

Regards

Dennis


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists