Skip to main content
lmeli
Associate II
March 6, 2019
Question

Communication UART

  • March 6, 2019
  • 9 replies
  • 1791 views

Good morning,

HAL_UART_Transmit(&huart2,myRxData,11,10);

HAL_UART_Receive(&huart2,myRxData,11,100);

My question is: only to make a test of sending and receiving data is enough to use the previous functions and make the connection of the pins tx with rx and rx with tx using the configuration of usart2 for the electronic card stm32f411 or should I do something more? Thank you.

This topic has been closed for replies.

9 replies

Vangelis Fortounas
Associate II
March 6, 2019

hello!

You can't use these functions to make a loop-back test.

these are blocking functions that means HAL_UART_Transmit will not return until transmit all 11 bytes or timeout. So HAL_UART_Receive will called after transmit finishes.

Try the IT versions of these functions (HAL_UART_Transmit_IT with appropriate callbacks and different buffers to have both RX and TX simultaneously.

lmeli
lmeliAuthor
Associate II
March 7, 2019

Thank you

S.Ma
Principal
March 6, 2019

Reception can occur anytime, so use at least Receive using Interrupt (IT)

For transmit, you can use polling.

You'll have to learn how to react when a byte has been received, fill a buffer, decide when the message is ready for processing, and set a flag for the main loop to react.

lmeli
lmeliAuthor
Associate II
March 7, 2019

Thank you

lmeli
lmeliAuthor
Associate II
March 8, 2019

I agree I am working with interruptions as you suggested to me using the code:

Transmitter code.

uint8_t Rx_data[]={1,2,3,4};

 while (1)

 {

HAL_UART_Transmit_IT(&huart2,(uint8_t *)Rx_data,4);

HAL_Delay(200);

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

#####################################

Receiver code

#####################################

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

//uint8_t Rx_data[10];

uint8_t Rx_data[4];

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Receive_IT(&huart2,Rx_data,4);

}

/* USER CODE END 0 */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 /* USER CODE BEGIN 2 */

HAL_UART_Receive_IT(&huart2,Rx_data,4);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);

HAL_Delay(500);

 }0690X0000087eCcQAI.png0690X0000087eCXQAY.png0690X0000087eCSQAY.png0690X0000087eCNQAY.png0690X0000087eCIQAY.png

Then I use two different computers to load each part of the code, the transmission and reception and first execute the reception so that it is ready to receive and then the transmission code, however in the reception debbug I observe absolutely nothing.

When I run and test the transmitter code, I test it in this way.

When executing and testing the reception code, I test it in this way

Thank you

S.Ma
Principal
March 8, 2019

Callback is done within the interrupt! That is where you get an incoming byte and you ve got to do something quick about it. Activate the IT from the main loop, add a 100ms delay, then transmit... For test

Nesrine.JLASSI
Visitor II
March 22, 2019

Hi @lmeli​ 

You can't use these functions to do the loop-back test.

You can Try the IT versions of this function HAL_UART_Transmit_IT with appropriate reminders and different buffers to have RX and TX simultaneously.

Best regards.

Nesrine