cancel
Showing results for 
Search instead for 
Did you mean: 

How to recieve a UART message using interrupt and HAL without knowing the length of the message?

OBorr.1
Associate II

Hello community!

I'm staging in a company and they let my program a conversor on my own, de issue is when I started with the UART communication. I need to analyse a message recieved by the user, so I need to be able to analyse any kind of message recieved without knowing the length and using HAL functions. I also need to do it without blocking the program, so it has to be done with interrupts.

I have tried with HAL_UART_Receive_IT and combining HAL_UART_Receive with HAL_UART_IRQHandler but I got the same results.

If there is any source would be a big help.

Anticipated Thanks!

(Sorry if my english is bad, i'm from a foreign country, if any aclaration is needed i'll be here)

1 ACCEPTED SOLUTION

Accepted Solutions

Use the IT version, select one byte, STM32 typically interrupts for each anyway. In the callback start another IT request.

You'll need to determine the natural breaks in the data, either by packet boundaries or user stops typing or hits ENTER/RETURN

Some parts support an IDLE interrupt.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

9 REPLIES 9

Use the IT version, select one byte, STM32 typically interrupts for each anyway. In the callback start another IT request.

You'll need to determine the natural breaks in the data, either by packet boundaries or user stops typing or hits ENTER/RETURN

Some parts support an IDLE interrupt.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

You can use HAL_UARTEx_ReceiveToIdle_IT/_DMA to receive characters until the line goes idle or until X characters are received.

If you feel a post has answered your question, please click "Accept as Solution".

Or just simply drop Cube/HAL, handling UART in interrupt is a rudimentary skill.

JW

Thanks for your answer but, would that work?, i mean, if the user send a short message, the program would not process it. Would It?

source?

ONadr.1
Senior III

And what about use a circular buffer. Feed it from serial (HAL plus IRQ feedback, where is the received byte add into buffer). And you can in every moment detect, how many bytes is in it and get what you need.

Amel NASRI
ST Employee

Hi @OBorr.1​ ,

In his article STM32 UART DMA RX/TX, @Tilen MAJERLE​ deals with the topic "Receiving data with UART and DMA when application does not know in advance size of bytes to be received".

I think that this will help you to understand more about UART.

-Amel

PS: Once your question is answered, please click on "Select as Best" for the comment containing the answer to your initial question.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Pavel A.
Evangelist III

This becomes a frequent user request.

Perhaps ST can consider adding a simple "continuous receiver" API, something like this:

HAL_UART_Receive_Continous_IT( huart, void (*rx_callback(uint32_t chr)))

Where rx_callback is user provided function that gets the received bytes. Variant: it can be called also on errors and various events.

/* Circular DMA of course is more efficient, but much harder to set up */

Guenael Cadier
ST Employee

Hello @OBorr.1​ 

As mentioned by @TDK​ , UART HAL API now provides services that would allow "continuous" reception or reception of unknown length.

For example, a use case could be based on HAL_UARTEx_ReceiveToIdle_DMA() API with DMA configured in Circular mode, that allows to have continuous reception and user callback executed as soon as either buffers are filled or Idle event occurs (pause in the transmission, i.e. enough time elapses after last received char) to retrieve data.

This callback will be executed when any of following events occurs :

   - HT (Half Transfer) : Half of Rx buffer is filled)

   - TC (Transfer Complete) : Rx buffer is full.

     (In case of Circular DMA, reception could go on, and next reception data will be stored in index 0 of reception buffer by DMA).

   - Idle Event on Rx line : Triggered when RX line has been in idle state (normally high state) for 1 frame time, after last received byte.

Usually, FW package contains a UART example highlighting such use case (example name UART_ReceptionToIdle_CircularDMA), with an example of callback implementation for retrieving all received data.

Please have a look to this example to confirm it could address your needs.

Regards