cancel
Showing results for 
Search instead for 
Did you mean: 

Get single JSON message sent over Serial.

CLH
Associate II

Hi,

This may be an easy thing to answer, but I haven't had much experience.

I have a JSON message that I'm sending down to my F446RE board.

I am using the HAL_UART_RxCpltCallback function, this works great, however, for this to work I need to know the length of the message, but I have 10 different messages that can be sent which vary in length.

How can I handle this?

1 ACCEPTED SOLUTION

Accepted Solutions
Ozone
Lead II

Check for the termination character(s), usually '\n' or "\r\n".

Check for threads about GPS (NMEA183), which deal with a similar problem - messages of varying length.

View solution in original post

3 REPLIES 3
Ozone
Lead II

Check for the termination character(s), usually '\n' or "\r\n".

Check for threads about GPS (NMEA183), which deal with a similar problem - messages of varying length.

CLH
Associate II

Is it possible to get byte-by-byte interrupts without using DMA. My issue is that if I set the following: HAL_UART_Receive_IT(&huart3, buffer_rx, 1); Then once the buffer is full aka 1 byte then the interrupt is fired, however isn't fired again. The buffer_rx variable is 200 bytes wide, and I want to keep adding to the buffer until the termination is found.

I must be thinking about this wrong, can you assist further?

Much appreciated!

Variable length UART reception and DMA are mutually exclusive IMHO.

Even singel-byte DMA could cause problems, you would need interrupts to deal with reception errors.

I don't use Cube/HAL, by the way.

> The buffer_rx variable is 200 bytes wide, and I want to keep adding to the buffer until the termination is found.

The buffer needs to be sufficient for the longest message, plus some extra.

For robustness, always check for the index first. If you are out of range, you can drop the received characters anyway.

GPS has a start character as well, which makes re-synchronization after errors easier.

In absence of those, you would probably need to re-sync on the termination character.