Skip to main content
Dave Bohan
Associate III
May 26, 2020
Question

How to setup USART to read unknown string lengths.

  • May 26, 2020
  • 2 replies
  • 1437 views

I'm using a NUCLEO-L053R8 board. I have tried a variety of USART settings including interrupt and DMA, but cannot reliably read unknown string lengths (at 115200 baud).

I want to read either until a certain character is seen, or for a specific period of time.

However, by using the Arduino IDE and running the NUCLEO as an Arduino, I have no problem using the 'serial.read()' function to accomplish the task.

What kind of magic is under the hood in the Arduino code?

This topic has been closed for replies.

2 replies

TDK
May 26, 2020

You can use HAL_UART_Receive() in a manner similar to serial.read. Adjust timeout and size to your use case:

uint8_t c = 0;
if (HAL_UART_Receive(&huart, &c, 1, 1000) == HAL_OK) {
 // received character is in c
}

"If you feel a post has answered your question, please click ""Accept as Solution""."
Dave Bohan
Associate III
May 27, 2020

Thanks, but that method will loose characters at 115200 baud.

Pavel A.
Super User
May 27, 2020

> What kind of magic is under the hood in the Arduino code?

Looks like they use some smart heuristic to guess when to stop (timeout?) and their low level code is more efficient than the ST library.

-- pa

kurta999
Senior
May 27, 2020
Dave Bohan
Associate III
May 28, 2020

This is good information. Seems like my biggest issue was that the default clock frequency for an X-CUBE project on the NUCLEO-L053R8 board was 2MHz. Upping that to 30MHz solved the problem.