Skip to main content
BMo.1
Associate
September 20, 2021
Question

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

  • September 20, 2021
  • 24 replies
  • 3854 views

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.

This topic has been closed for replies.

24 replies

TDK
September 20, 2021

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""."
BMo.1
BMo.1Author
Associate
September 20, 2021

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.

ONadr.1
Senior III
September 21, 2021

So, try to blink LED when buffer[0] is equal 5. You probably dont receive what you expected.

waclawek.jan
Super User
September 20, 2021

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

BMo.1
BMo.1Author
Associate
September 20, 2021

stm32F103C8.

I tried with single digit numbers for payload length.

waclawek.jan
Super User
September 20, 2021

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

BMo.1
BMo.1Author
Associate
September 20, 2021

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

waclawek.jan
Super User
September 20, 2021

And if you use

int payload_length =buffer[0] - 1;

?

JW

BMo.1
BMo.1Author
Associate
September 20, 2021

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
September 21, 2021

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?

waclawek.jan
Super User
September 21, 2021

Or just drop Cube/HAL and write your own blocking UART receiver. It's literally 5 lines of code.

JW

BMo.1
BMo.1Author
Associate
September 21, 2021

So I tried to implement my own Receive function like this :

void Serial_Receive(uint8_t* buffer ,uint8_t size){
	int i=0 ;
	while(i < size ){
		while (!(USART1->SR & USART_SR_RXNE));
		buffer[i]=USART1->DR;
		i++;	
	}
}

When i call it with payload_length as a parameter it blocks exactly like the HAL fucntion

		Serial_Receive(buffer2,payload_length);

A call with a hard coded value proceeds smoothly and the program continues its execution.

Serial_Receive(buffer2,5);

The problem probably lies with payload_length variable and I don't see why honestly.

S.Ma
Principal
September 21, 2021

I would use DMA in polling modr. Use a cyclic mode over 16 bytes buffer, that is simpler.... and is non blocking...

BMo.1
BMo.1Author
Associate
September 21, 2021

Okay will try it.

I just want to understand what's the problem in this case tbh.

waclawek.jan
Super User
September 21, 2021

What exactly does the transmitter transmit? Is it

05 xx xx xx xx

or

05 xx xx xx xx xx

?

JW