Search code examples
cstm32uart

How to save only certain bytes I need instead of all in an array?


I am getting from a sensor via UART communication every second 10 bytes. But I don't need all bytes, actually only certain bytes to work with. Now what I do is to save all bytes into an array and create two new uint8_t's and assign them the byte from the buffer array I need.

Is there a way to only receive and save the bytes I need in the first place instead of all 10?

uint8_t buffer[10];

HAL_UART_Receive_DMA(&huart4, (uint8_t*)buffer, 10)

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart4)
{
  uint8_t value1 = buffer[4];
  uint8_t value2 = buffer[5];
  .
  .
  .
}

Solution

  • It is possible with just raw interrupt handling, without any DMA and (mostly) without fancy HAL functions.

    You'll have to write manual UART interrupt handler for RXNE flag, which is set when UART receives signle character. Read it from DR register and decide - save or discard it. Of cource it's up to you now, to count all received bytes and detect "end of message" condition.

    OR

    If your code have nothing to do during receiving this message, use

    HAL_UART_Receive

    To read message byte-by-byte