cancel
Showing results for 
Search instead for 
Did you mean: 

How HAL_UART_Receive_IT() works?

Maunik Patel
Associate II
Posted on June 11, 2018 at 15:02

Hello,

I would like to know exactly how HAL_UART_Receive_IT() API works ...?I am using STM32L432KB controller with Quectel's BG96 module to send AT commands and receive its variable length responses.

A demo code of my work is given below.

uint8_t ATE[] = 'ATE0\r';

char buf[100], ch;

int i=0;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

    if(huart->Instance == USART1){

    buf[i] = ch;               // filling 'buf', index by index, by 'ch', which has been received from main()

    i++;

    HAL_UART_Receive_IT(&huart1, (uint8_t*)&ch, 1);

  }

}

int main(){

   while(1){

      i=0;

      HAL_UART_Transmit(&huart1,(uint8_t *)ATE, sizeof(ATE),1000);   // turn Echo OFF for AT commands

      // HAL_UART_Receive(&huart1, (uint8_t*)&buf, sizeof(buf), 1000);   // if using this API, I can read the response in 'buf', but it takes 1 sec to receive about10 bytes

                                                                                                                 // @115200 buad.

                                                                                                                 // except this demo, I can't judge how much bytes I am about to receive

      HAL_UART_Receive_IT(&huart1, (uint8_t*)&ch,  1);                        // receiving one byte and storing it in 'ch'

      while(!strstr(buf, 'OK')){                                                                     // waiting to get full response of AT command, so that it can be used for ERROR handling

   }

}

While following this approach to develop my application I am getting many unpredictable results.

Can you help me to figure out what is wrong in this approach and how can I receive any variable length string in minimal time ?

Thank You,

Maunik Patel
1 REPLY 1
Posted on June 11, 2018 at 15:18

strstr() expects a NUL terminated string to work with, stuffing data in the buffer doesn't explicitly achieve that, nor does the code seem to wait for any sort of completion. HAL_UART_Receive_IT() does not block, so you'd need to way of signalling from the callback that there was either new data available or some complete data.

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