cancel
Showing results for 
Search instead for 
Did you mean: 

Receiving more than one byte for HAL_UART_Receive

PatTheTrickster
Associate III

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];
}

}

 

2 REPLIES 2
Issamos
Lead II

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

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 Up vote any posts that you find helpful, it shows what's working..