Question
UART Recive for unknown length
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
}