cancel
Showing results for 
Search instead for 
Did you mean: 

How to setup USART to read unknown string lengths.

Dave Bohan
Associate II

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?

5 REPLIES 5
TDK
Guru

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".
kurta999
Senior

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

> 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

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.