Skip to main content
PatTheTrickster
Associate II
September 14, 2023
Question

Receiving more than one byte for HAL_UART_Receive

  • September 14, 2023
  • 2 replies
  • 2310 views

I'm a bit confused on how to receive data with multiple bytes and store it to some buffer on the stm32 microcontroller?

Currently I have a python script that will send the data over uart serial "serial.Serial.write(b'4215'))"

Then I have a UART interrupt in my main loop that will capture only the first number but I have not been able to see the 215 when in debugging mode.

Just going to write the relevant code below since everything seems to be working right it's just I can't get more than one byte.

Any feedback is much appreciated! Let me know if I need to provide more information!

 

uint8_t Rx_buffer[100];
uint8_t msg_r_data[7];
uint8_t rx_indx = 0;
uint8_t msg_r_buffer[100];

....

while (1)
{
HAL_UART_Receive_IT(&hlpuart1, Rx_buffer, 4);
...
...

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart -> Instance == LPUART1) {
 msg_r_data[rx_indx++] = Rx_buffer[0];
}

}

 

This topic has been closed for replies.

2 replies

Issamos
Super User
September 14, 2023

Hello @PatTheTrickster .

To make your request clearer, I suggest you to add your MCU reference and your full code if it's possible.

Best regards.

II

Tesla DeLorean
Guru
September 14, 2023

You don't start the process in a loop, you initiate it, and then restart in the callback

uint8_t Rx_buffer[1]; // You can do more than 1, but you could be left hanging, and need to process all in the callback
volatile uint8_t msg_r_data[7];
volatile uint8_t rx_indx = 0;

....

HAL_UART_Receive_IT(&hlpuart1, Rx_buffer, 1); // Initiate

while (1)
{
...
...

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart -> Instance == LPUART1) {
 msg_r_data[rx_indx++] = Rx_buffer[0]; // watch the index stays in scope
 HAL_UART_Receive_IT(&hlpuart1, Rx_buffer, 1); // Arm again
}

}

 

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