I am using a STM32L Discovery board (monted device STM32L476xx), for a transmit and receive UART operation using HAL libraries. I am using a FTDI chip for serial to usb conversion as I am unable to use the Virtual Com port of the STM.

I have got the HAL_UART_Transmit function working but can not get the similar receive function to work.

My UART handle Definitions void MX_USART2_UART_Init(void) {

    huart2.Instance = USART2;
    huart2.Init.BaudRate = 9600;
    huart2.Init.WordLength = UART_WORDLENGTH_8B;   // changed from 8B
    huart2.Init.StopBits = UART_STOPBITS_1;
    huart2.Init.Parity = UART_PARITY_NONE;
    huart2.Init.Mode = UART_MODE_TX_RX;
    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart2.Init.OverSampling = UART_OVERSAMPLING_16;
    huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart2) != HAL_OK)
        _Error_Handler(__FILE__, __LINE__);


I used the following for Transmit function which prints a string.

char bufftr[] = "check\n";
len = strlen(bufftr);
HAL_UART_Transmit(&huart2, (uint8_t*)bufftr, len, 100);

But I can't get a echo working where I transmit what I receive

char test[100] ;
while (1) {

        HAL_UART_Receive(&huart2, (uint8_t *)test, 4, HAL_MAX_DELAY);

        len = sizeof(test);
        HAL_UART_Transmit(&huart2, (uint8_t *)test, len, 100);  


This outputs seemingly random characters of the correct length. Please let me know how can I can I get this to work and also how to get UART transmission through the virtual COM port

  • 1
    \$\begingroup\$ What does size() do? \$\endgroup\$
    – Jeroen3
    Commented Jan 3, 2020 at 10:51
  • \$\begingroup\$ You are not checking the return value of the receive function, likely it is timing out and you are getting a garbage buffer of previous stack contents. Most people ultimately end up writing their own UART routines (even if they use the HAL's configuration ones) as the semantics of those provided by the HAL are an awkward match to many real needs. \$\endgroup\$ Commented Jan 3, 2020 at 16:00

1 Answer 1


Make Sur your GPIO Rx pin in correctly configured as Rx USART2. I suppose, FTDI cable is corretly connected between RX TX STM32 pins and your computer. -For testing your RX comm let use empty While(1) loop. -Enable UART2 interrupts on CUBEMx. -Set a breakpoint in the UART2 generated ISR. When you enter any caracter on the computer terminal, your embedded soft should stop on break point. This is a way a use to check Rx connection.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.