cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive UART fixed length datas in sequence

paker earth
Associate II
Posted on January 24, 2017 at 15:27

Hi all

I'd like to use UART to receive soe fixed length datas, ex.'1234567890'.

uint8_t buff[10]; 

hal_uart_receive_it(&huart, buff, 10); or 

hal_uart_receive_dma(&huart, buff, 10);

How should I do and then make sure the buff contain '1234567890' every time, not '5678901234' or other situation.

Thank you in advance. 

3 REPLIES 3
Khouloud GARSI
Lead II
Posted on January 24, 2017 at 16:23

Hi

paker.earth

,

I advice you to have a look at the 'UART' examples provided by ST.

Depending on your STM32 device, download the convenient

http://www.st.com/content/st_com/en/extended-query.html?querycriteria=productId=SC2004$$associatedTo=SC1169

package.

The 'UART' examples could be found under:

STM32Cube_FW_F...\Projects\STM32F....\Examples\UART

May this help you

Khouloud.

Posted on January 24, 2017 at 16:32

You'd need some flexibility in your code to be able to resynchronize to the data stream if you lost bytes or came in mid stream. ie realize you have 4 bytes of your packet in the first receive, and you need only get 6 more to complete it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 24, 2017 at 16:32

The device that you are using is asynchronous by design, there is no data framing at the byte level.

Your options are to put framing bytes (STX/ETX) around your data and use those to delineate your data or put an idle period between your data packets. Arrange for the idle time to be longer than one packet, then use DMA with interrupts along with a timer interrupt. If the packet is too short, the timer will fire in the gap between packets, picking off a short packet. If you get a reasonable amount of data, the DMA transfer will give you an interrupt and you can kill the timer.

If you have an unstated requirement that you can't change the data stream by adding framing or idle periods, you're basically screwed. You will have to look at the data, figure out what is missing and use short DMA requests to try and get back into sync, but you have lost data. That's probably not okay.

A