cancel
Showing results for 
Search instead for 
Did you mean: 

UART Recive for unknown length

JPanc.1
Associate II

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
 
       }

4 REPLIES 4
parth kothiya
Senior

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.

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

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

I am asking for communicating with GSM/GPS module SIM7000

whose response is as below:

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