cancel
Showing results for 
Search instead for 
Did you mean: 

same STM32 UART1_TX is connected to UART2_RX. But UART2_RX only receive 1st element

hoangminh1998
Associate
If you have time, please help me to find the solution to enhance the code below. In same STM32F103C8T6, UART1_TX is connected to UART2_RX which is received data from TX.
 
My expected result is: RxData = {2, 1, 3, 0, 0}
My Actual result: RxData = {2, 0, 0, 0, 0}
The code:
#include "main.h"
#include <stdio.h>
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
...
uint8_t TxData[] = {2, 1, 3};
uint8_t RxData[5] ;
/*#################### usart1Tx = PA9 connect usart2RX = PA3 ####################*/
int main(void)
{
 ...
  while (1)
  {
   
HAL_UART_Transmit(&huart1, TxData, sizeof(TxData), HAL_MAX_DELAY);
      HAL_UART_Receive(&huart2, RxData, sizeof(RxData), HAL_MAX_DELAY);
  }
}


1 REPLY 1
gbm
Lead III

The code above sends three bytes. The first byte is received by UART but not read from it, so the other bytes are lost. Then you call Receive, which reads the only byte received. So, everything works as expected. Read the description of non-blocking  functions, like HAL_UART_Receive_IT(). Call it first, then call Transmit - the bytes sent will be received in the background. Or better - don't use HAL for UART - it doesn't fit any real world scenario. 😉