cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART Transmit/Receive dont work when not using a fixed size as parameter?

BMo.1
Associate II

in my program is receive first data that tells me what's the size of the payload i am getting next through UART ,When i use that variable as parameter in the UART functions it doesnt work ,however when i hard code a value say HAL_Uart_Receive(&huart1,buffer,5,...) it does indeed work.

Here is my code

#define Max_Buffer_Size		16384UL
uint8_t buffer[Max_Buffer_Size];
 
HAL_UART_Receive(&huart1,buffer,1,HAL_MAX_DELAY);
int payload_length =buffer[0];
HAL_UART_Receive(&huart1,buffer,payload_length,HAL_MAX_DELAY);

as i said if replace line 6 with this is it works :

HAL_UART_Receive(&huart1,buffer,5,HAL_MAX_DELAY);

Thank you for your help.

24 REPLIES 24
TDK
Guru

This:

int payload_length = 5;
HAL_UART_Receive(&huart1,buffer,payload_length,HAL_MAX_DELAY);

is equivalent to this:

HAL_UART_Receive(&huart1,buffer,5,HAL_MAX_DELAY);

Must be some other explanation. Perhaps buffer[0] is not 5.

If you feel a post has answered your question, please click "Accept as Solution".

Which STM32?

What exactly means is "it doesn't work"? What is value of "payload length" when it "does not work"?

HAL_UART_Receive is open-source, you can debug it as any other code.

JW

stm32F103C8.

I tried with single digit numbers for payload length.

BMo.1
Associate II

The same code swapping the first snippet with the second literally makes it work.(Both use Buffer[0] )

I don't have a logical analyzer with me so i can't really debug this rn.(I am debugging with LEDs ).

Thanks for the help.

What exactly means is "it doesn't work"? How do you know "it doesn't work"?

As I've said, Cube is open source. You can debug it whatever tools you are using, including LEDs to trace execution.

JW

I've been testing with a LED and the program doesn't get past the 6th Line,it blocks there.

And if you use

int payload_length =buffer[0] - 1;

?

JW

BMo.1
Associate II

buffer[0] is perfectly fine and holds the value i get through UART.

The code that follows works perfectly fine and does what is expected of it.

		HAL_UART_Receive(&huart1,buffer,1,HAL_MAX_DELAY);
		uint8_t payload_length =buffer[0];
                HAL_UART_Transmit(&huart1,&buffer[0],payload_length,HAL_MAX_DELAY);
 

S.Ma
Principal

HAL is ill equiped for variable packet length, unless using a dma cyclic'ed buffer checked at regular time interval. You need also a sync method to detect head and tail of your data packets.... what is both mcus are not reset at the same time?