UART Read misbehaving after reading some Bytes

Hi Team ,

I am experiencing an issue while reading UART data of 48 bytes continuously with 100usec delay where in we have observed the data is getting lost in between while reading 48bytes.

For Example :

Read 1 : Received Correct 48 Bytes successfully
Read 2 : Received Uncorrect 48 Bytes // Corruption
Read 3 : Received Correct 48 Bytes successfully
Read 4 : Received Uncorrect 48 Bytes // Corruption
Read 5 : Received Correct 48 Bytes successfully
Read 6 : Received Correct 48 Bytes successfully
Read 7 : Received Uncorrect 48 Bytes // Corruption
Read 8 : Received Correct 48 Bytes successfully
Read 9 : Received Correct 48 Bytes successfully
Read 10 : Received Uncorrect 48 Bytes // Corruption
Read 11 : Received Correct 48 Bytes successfully
Read 12 : Received Correct 48 Bytes successfully
Read 13 : Received Correct 48 Bytes successfully
Read 14 : Received Correct 48 Bytes successfully

Note : I did not observe any losses when tried with microcom

Below is my UART Initialization and UART receive / read code snippet
Let me know if i am missing something ? or there is something wrong

Note : UART here refers to CP210x which is a USB → UART Bridge enumerating 3 UARTS

=======> UART INITIALIZATION <=========

void ExtUart_Init(void)
{

 	 	uint32_t ret;//,flags;//size_pipe;

 		fd = le_tty_Open("/dev/ttyUSB1", O_RDWR | O_NOCTTY);

 		ExtUART_fd=fd;
 		if(fd<0)
 		{
 			LE_ERROR("ECUConnect: EXT_UART SERIAL OPEN FAILED %d",fd);
 			return;
 		}
 		else
 		{
 			LE_INFO("ECUConnect: EXT_UART SERIAL OPEN SUCCESS");
 		}
 		usleep(100000);

 		if((ret=le_tty_SetBaudRate(fd,LE_TTY_SPEED_2000000))!=LE_OK)
 		{
 			LE_INFO("ECUConnect: Baud rate:%d",GLOB_IRU_CurSettings.UART_1Setting.UART_Baudrate);
 			LE_ERROR("EXT_UART SETBAUDRATE FAILED ");
 			LE_INFO("%d",ret);
 			if(ret==LE_NOT_FOUND)
 			{
 				LE_INFO("BRT NOT supported");
 			}
 		}
 		else
 		{
 			LE_INFO("ECUConnect: Baud rate:%d",GLOB_IRU_CurSettings.UART_1Setting.UART_Baudrate);
 			LE_INFO("ECUConnect: EXT_UART SETBAUDRATE SUCCESS");
 		}

 		usleep(10000);

 		if(le_tty_SetFraming(fd,GLOB_IRU_CurSettings.UART_1Setting.parity,GLOB_IRU_CurSettings.UART_1Setting.DataBits,GLOB_IRU_CurSettings.UART_1Setting.stopBits)!=LE_OK)
 		{
 			LE_ERROR("ECUConnect: EXT_UART SET_FRAMING FAILED");
 		}
 		else
 		{
 			LE_INFO("ECUConnect: EXT_UART SET_FRAMING SUCCESS");
 		}


 		if(le_tty_SetFlowControl(fd,LE_TTY_FLOW_CONTROL_NONE)!=LE_OK)
 		{
 			LE_ERROR("ECUConnect: EXT_UART SET_FLOW CONTROL FAILED");
 		}
 		else
		{
			LE_INFO("ECUConnect: EXT_UART SET_FLOW SUCCESS");
		}
 		if(le_tty_SetRaw(fd,48,UART_READ_TIMEOUT)!=LE_OK)
 		{

 			LE_ERROR("ECUConnect: EXT_UART SET_RAW FAILED");
 		}
 		else
		{
			LE_INFO("ECUConnect: EXT_UART SET_RAW SUCCESS");
		}

}

===============> UART READ <====================

	 ret=read(fd, &UART_Recv_Buffer_1,48);

	 if(ret==48)
	 {

if((UART_Recv_Buffer_1[0]==0xAA) && (UART_Recv_Buffer_1[1]==0x55) && (UART_Recv_Buffer_1[46]==0x55) && (UART_Recv_Buffer_1[47]==0XAA))
{
//Read successful…
}
else
{
//Read fail…
}

             }

Thanks & Regards

  • Swapy

I think you need to enable hardware flow control.

Also if you decrease the data rate, does it have improvement?

Hi @jyijyi ,

I did not observe any loss in microcom .

I think your program has some problem in this line:
ret=read(fd, &UART_Recv_Buffer_1,48);

It cannot already read 48 bytes, sometimes it may be chopped into two frames,
for example: 40 and 8 bytes,

hi @jyijyi

If that is the condition what will be solution…???

NOTE
Here i am calling ret=read(fd, &UART_Recv_Buffer_1,48); function every 100 MicroSec. also try with more…

Thanks
Swapy

you check ret==48, but that might not be always true, right?
As said before, the frame may be chopped into two

if(ret==48) This is always true The problem is when we are comparing with start and end characters
if(ret==48)
{
if((UART_Recv_Buffer_1[0]==0xAA) && (UART_Recv_Buffer_1[1]==0x55) && (UART_Recv_Buffer_1[46]==0x55) && (UART_Recv_Buffer_1[47]==0XAA))
{
//Read successful…
}
else
{
//Read fail…
}
}

if the frame is chopped into two, then it will go to two times with “READ FAILED” ,right?

Can you also tune down the data rate speed so that you will know if your program logic is OK or not?

NO…when Start 2byte and end 2 bytes are not match that time only it will print READ FAILED

then if one frame is chopped into 28+20, what will happen?

the first 28 bytes will be discarded, and then when you read the seconds time, the later 20 bytes will be concatenated with another frame, right?

If this is the issue then how can we solve it…??
I must have to read 48 Bytes only…!!

you should use a buffer to save all the data you received.
in your current program, there is a chance that it read a frame smaller than 48 bytes and then you will discard this frame.

Right i am doing same but here i’m facing issue in read time only…!!
I tried read char by char also but after some time same issue happened.

do you mean there is data missing?
Did you enable hardware flow control in your code?

BTW, you might compile your C program with the toolchain and see if there is any problem without the legato