Skip to main content
NHing.1
Associate III
January 30, 2021
Question

How to clear UART's RX_buffer in stm32f030cct6

  • January 30, 2021
  • 3 replies
  • 18066 views

Hello Forum,

I am using stm32f030cct6 and Quectel L86 GPS module for my application.

I am doing coding on STMCubeIDE, using HAL libraries.

The GPS module is in continuously receiving data mode.

I am receiving that data on uart in a buffer.

Max size of buffer i kept is 1500, which is sufficient.

My main question is that while receiving the data i need to clear the previously stored data from buffer.

For a while it receives variation in data but after some time it keep same data till reset of MCU.

I want to clear the previous data as it receives fresh data each second.

Can anyone tell me how to do that?

This is the data that received on UART

$PMTK011,MTKGPS*08
$PMTK010,001*2E
$PMTK011,MTKGPS*08
$PMTK010,002*2D
$GPRMC,064512.498,V,,,,,0.00,0.00,270121,,,N*4B
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
$GPGGA,064512.498,,,,,0,0,,,M,,M,,*49
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79
$GLGSV,1,1,00*65
$GPGLL,,,,,064512.498,V,N*7B
$GPTXT,01,01,02,ANTSTATUS=OPEN*2B
 
$GPRMC,064513.498,V,,,,,0.00,0.00,270121,,,N*4A
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
$GPGGA,064513.498,,,,,0,0,,,M,,M,,*48
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79
$GLGSV,1,1,00*65
$GPGLL,,,,,064513.498,V,N*7A
$GPTXT,01,01,02,ANTSTATUS=OPEN*2B

@Guenael Cadier

This topic has been closed for replies.

3 replies

KnarfB
Super User
January 30, 2021

Sounds like an issue with your code (not shown). Use memset to clear the buffer before the next message comes in?

Receive with HAL_UARTEx_ReceiveToIdle_IT(&huart2, &buffer, sizeof(buffer) ); and in the callback, you get the number of bytes read:

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	// process message of size Size in buffer
	HAL_UARTEx_ReceiveToIdle_IT(&huart2, &buffer, sizeof(buffer) );
}

NHing.1
NHing.1Author
Associate III
January 30, 2021

Hello @KnarfB​,

Thanks for the reply.

I am using

HAL_UART_Receive(&GPSUART,(uint8_t *)ch,MAX,500); /* Receiving data from GPS on UART_1*/
str_len = strlen(ch);
HAL_UART_Transmit(&TerminalUART,(uint8_t *)ch,str_len,500); /*Transmitting data to Terminal UART_2*/

 this in while loop

when i use memset after transmit loop get stops

Piranha
Principal III
January 30, 2021

An array of some data is not a zero terminated string. You cannot use strlen() or other string manipulation functions on it.

KnarfB
Super User
January 30, 2021

Put memset immediately before HAL_UART_Receive.

Note that HAL_UART_Receive returns after either MAX bytes were read or the timeout (500ms) elapsed.

NHing.1
NHing.1Author
Associate III
February 4, 2021

Hello @KnarfB​ , @Piranha​ 

I use memset before receiving data

memset(ch,0,MAX);
 
HAL_UART_Receive(&GPSUART,(uint8_t *)ch,MAX,1000); /* Receiving data from GPS on UART_1*/
str_len = strlen(ch);
HAL_UART_Transmit(&TerminalUART,(uint8_t *)ch,str_len,1000);/*Transmitting data to Terminal UART_2*/

and i got this output

$PMTK011,MTKGPS*08
$PMTK010,001*2E
$PMTK011,MTKGPS*08
$PMTK010,002*2D
$GPRMC,063153.974,V,,,,,0.00,0.00,040221,,,N*40
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
$GPGGA,063153.974,,,,,0,0,,,M,,M,,*40
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79
$GLGSV,1,1,00*65
$GPGLL,,,,,063153.974,V,N*72
$GPTXT,01,01,02,ANTSTATUS=OPEN*2B
 
 
330
 
$GPRMC,063154.974,V,,,,,0.00,0.00,040221,,,N*47
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
$GPGGA,063154.974,,,,,0,0,,,M,,M,,*47
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79
$GLGSV,1,1,00*65
$GPGLL,,,,,063154.974,V,N*75
$GPTXT,01,01,02,ANTSTATUS=OPEN*2B
 
 
256
 
$
 
1

 and HAL_UART_Receive returns after timeout

Alexander Stankov
Associate
October 28, 2021

https://www.youtube.com/watch?v=EsZLgqhqfO0&t=455s - watch right after the 13-minute mark. Maybe this can give you an idea. There is a (Rx/Tx)HalfCpltCallback or something in the HAL_Drivers. And UART can also use DMA.