cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive() on STM32F407 is receiving only one byte a time irrespective of amount of data being transmitted on transmitter side.

DevBee
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

      while(1)

      {

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

         if(ret != HAL_OK)

         {

            if(ret == HAL_TIMEOUT)

               break;

            Error_Handler();

         }

         rx_bytes++;

         i++;

      }

I do not want to use DMA but if data is available consume the data. No blocking, no waiting. I run the task at higher rate so as not to lose any byte in transmitted data from PC.

In above code say the transmitter has send 100 bytes, the value of i should get incremented by 100 bytes (for that matter the FIFO has been enabled. If at all FIFO length is an issue, at least till the size of FIFO it should not be an issue...). If the reason is that FIFO is not used unless we use DMA, I should at least get intermediate bytes but am getting the very first byte transmitted by PC everytime. Kindly guide...

1 REPLY 1
TDK
Guru

> ret = HAL_UART_Receive(&huart6, &ins_data[i], 1,0);

This will only ever return 1 value.

>      if(ret == HAL_TIMEOUT)

>               break;

This is going to exit your while loop if no characters are available. If you want to wait for more characters, don't exit the loop on timeout. Considering handling the HAL_OK and HAL_TIMEOUT cases and going to Error_Handler on other codes.

break does not exit if statements, which was maybe your intention here.

ret = HAL_UART_Receive(&huart6, &ins_data[i], 1,0);
if (ret == HAL_OK) {
    rx_bytes++;
    i++;
} else if (ret != HAL_TIMEOUT) {
    Error_Handler();
{

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