cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Uart_Receive is Receiving only one byte, even the size is >1.

Mr.L0seR
Associate II

Hello,

Am using STM32F407 (Discovery board). This is connected with a PC which is sending 100 bytes of data and realized that only 1 byte is received and remaining 99 bytes are lost. Below is the code excerpt running on STM32F407

  ret = HAL_UART_Receive(&huart6, &ins_data[i],10,10);

         if(ret == HAL_OK)

         {

            rx_bytes++;

            i++;

         }

I do not want to use DMA but if data is available consume the data.

In the above code we are transmitting 100 bytes from PC.But at the end it is receiving only one byte,even the size is >1, and that one is only first byte of the transmitted bytes always.

We checked the ORE bit in CR1 register it is changed from 0 to 1, So at the receiving side after receiving of first byte other bytes are ready to receive,And every time after one received byte it enters into HAL_TIMEOUT error.

we are transmitting the data at high frequency so we do not want to loss the data.

Is there any other posibilities to receive the data according to our requirement without using Urat_DMA.

5 REPLIES 5
MLens27
Associate II

It could be taht yoru timeout value is too low, what does the hal function return?

If the function return HAL_TIMEOUT then u need to increase the timeout value.

IT returns HAL_TIMEOUT when size is >1 and it returns HAL_OK if size is 1. but in both cases it is receiving only one byte.

we are processing the data at high speed so we have to use the low timeouts. Is there any other solution except increasing the Timeout value.

i would suggest to increase it and test if that solves the problem. The timeout is only the maximum amount of time that the recieve may use.

It could be another problem if there is stil a timeout after increasing the timeout value to lets say 1000 ms.

I increased the timeout value but it is showing the same issue.only one byte is received. What am I missing.

Bob S
Principal

Is that REALLY the exact code you are running?

Your HAL_UART_Receive() call is asking to receive 10 bytes into an array indexed by "i", then you increment "i" by one and I presume try again. So, at best, this would receive 10 bytes and store them start at ins_data[i]. Then try to receive 10 more bytes, storing them starting at ins_data[i+1], thus overwriting 9 of the previous bytes.

So I will presume that is a typo and you really are trying to receive 1 byte at a time, with a timeout of 10ms. What is sending the data to you? Does it pause between sending each byte? Or does it send then with no gaps between bytes? Have you verified this with a scope? At 115200 baud, 1 stop bit, 100 bytes should take 8.7ms.

What is your CPU clock frequency? What is your baud rate? What else does your code do before it goes back to call HAL_UART_Receive() again after receiving one byte?