Skip to main content
JPanc.1
Associate III
May 5, 2019
Question

UART Recive for unknown length

  • May 5, 2019
  • 4 replies
  • 817 views

I am using STM32F103C8T6 MCU and STM32CubeMx.

I currently reciving data from UART and printed on PC's serial but it will work only when data terminated by '\n' character.

But what if the data from uart not terminated with '\n'??

Can anybody help??

Please find the code below:

 if (huart->Instance == USART2) //current UART
 {
 if (Rx_indx==0)
 {
 	 //for (i=0;i<100;i++)
 		 Rx_Buffer[256] ="\0";
 } //clear Rx_Buffer before receiving new data
 
 if (Rx_data[0]!='\n') //if received data different from ascii 13 (enter)
 {
 Rx_Buffer[Rx_indx++]=Rx_data[0]; //add data to Rx_Buffer
 
 }
 else //if received data = 13
 {
 	 Rx_Buffer[Rx_indx++] = '\n';
 	 memcpy(Tx_Buffer,Rx_Buffer,Rx_indx);
 	 UART2_print(Tx_Buffer);
 Rx_indx=0;
 memset(Tx_Buffer, 0, 256);
 }
 
 HAL_UART_Receive_DMA(&huart2, Rx_data, 1); //activate UART receive interrupt every time
 
 }

This topic has been closed for replies.

4 replies

parth kothiya
Associate III
May 5, 2019

check every char you received in mcu when special char(enter ,space, : any terminating char) received then do operation on received data otherwise put char

in to buffer in next position till you don't receive special char .

i am using cool term pc software to watch UART received data and send data to mcu.

JPanc.1
JPanc.1Author
Associate III
May 5, 2019

I already know that man but my question is different....

Tesla DeLorean
Guru
May 5, 2019

So what would terminate the data in your hypothetical? A different character, intersymbol time, protocol defined length?

Don't do things requiring multiple character times in callback/interrupt. ​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JPanc.1
JPanc.1Author
Associate III
May 5, 2019

I am asking for communicating with GSM/GPS module SIM7000

whose response is as below:

"\r\n"<response data>"\r\n"