Skip to main content
TH.16.336
Associate III
February 5, 2020
Question

How to find number of chars/bytes to read over UART channel?

  • February 5, 2020
  • 2 replies
  • 2840 views

Hello all,

I am using NUCLEO-F446RE board.

I am looking for some way to find how many bytes of data is received over UART which we want to read using HAL_UART_Read API.

Normally, we will know what is the incoming data. So, depending on that we Read those many bytes of data.

But in a special scenario, I don't how many bytes of data is coming. Once data is received I need to validate it. For this, I need a way to find how many bytes are coming over UART?

Please help me out.

This topic has been closed for replies.

2 replies

waclawek.jan
Super User
February 5, 2020

Receive/check bytes one by one.

JW

Ozone
Principal
February 5, 2020

Exactly.

> Normally, we will know what is the incoming data.

No, you don't.

Characters get lost or get corrupted, you can even get false receptions.

You only know how much characters you expect.

> But in a special scenario, I don't how many bytes of data is coming. Once data is received I need to validate it.

Use a protocol with designated start or end character, or better both.

Synchronize your reception on those characters.

Always assume that start/end character can get lost or corrupted as well.

Validate once you successfully received a package.

KnarfB
Super User
February 5, 2020

> But in a special scenario, I don't how many bytes of data is coming.

There is a UART idle time interrupt feature. The interrupt will trigger after the serial line was idle for a certain time. This interrupt is not covered by HAL UART API, but there is a nice article about it here: https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx