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

You can echo back the bytes you receive.

What do you use on PC side to transmit? Is that tool capable of transmitting binary data? Aren't you trying to transmit the first byte as ASCII character '5', i.e. 0x35?

JW

S.Ma
Principal

Looks like there are missing practical application examples from Cube library...

Problems arise only when I use the variable as parameter to the fucnction.

With the code below ,when i transmit 1,it echoes back 1 .

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

However with this one below i get back a bunch of gibberish "�?`ʼnF.T`�ÉS('#(à´Qšò“„$i"¨`bØ€".

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

I am transmitting data as ASCII through a software called Termite .

An ASCII 1 is binary 0x31 i.e. 49. And and ASCII 5 is binary 0x35.

If you want to transmit "5Hello", then your receiver should subtract 0x30 from the first character:

uint8_t payload_length = buffer[0] - 0x30;

JW

BMo.1
Associate II

All I had to do was convert the payload to an uin8t_t and that made it work

uint8_t payload_length =buffer[0]-'0';

Thanks everyone for the help