Page 1 of 1

Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Mon Jun 24, 2024 4:17 pm
by AlexSV
Hi everyone!

I encountered strange behavior of UART when sending data from KFLOP.

I'm trying to send two doubles(double a=23.45006, b=9e6;) using the SendDouble.c program, 2ms before the start of the exchange, 0x00 flies over the bus and ends up in the receiver buffer, how can I fix this?

In this screenshot, exchange with STM32

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Mon Jun 24, 2024 5:22 pm
by TomKerekes
Hi Alex,

That should only happen once when the UART is first initialized to flush the output buffer. It shouldn't be a problem if the receiver ignores data until a STARTCODE (0xAA) is received.

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Mon Jun 24, 2024 8:23 pm
by AlexSV
Thanks for the answer, I'll rewrite the code.

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Mon Jun 24, 2024 8:48 pm
by TomKerekes
Hi Alex,

You might see the ReceiveDoubles.c example.

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Mon Jun 24, 2024 9:19 pm
by AlexSV
Yes, I saw it. I rewrote the function, now everything works as it should.

Code: Select all

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
	if(huart == &huart1) {  
		if (RxData == 0xAA) {flag0 = 1;}
		if (RxData == 0x01 && flag0) {flag1 = 1;}
		if (flag0 && flag1)
		{
			RxData0[n] = RxData;
			n++;
			if(n==9)
			{
				n=0; flag0=0; flag1=0;
				HAL_UART_Transmit_IT(&huart1, (uint8_t *)TxData, sizeof(TxData));
				int i;
				for (i=1;i<9;i++){
				*(((unsigned char *)&a)+i-1) = RxData0[i] ;}
			}
		}
//------------------------------------------------------------------------------
		if (RxData == 0x02 && flag0) {flag2 = 1;}
		if (flag0 && flag2)
		{
			RxData0[n] = RxData;
			n++;
			if(n==9)
			{
				n=0; flag0=0; flag2=0;
				HAL_UART_Transmit_IT(&huart1, (uint8_t *)TxData, sizeof(TxData));
				int i;
				for (i=1;i<9;i++){
				*(((unsigned char *)&b)+i-1) = RxData0[i] ;}
			}
		}
		HAL_UART_Receive_IT (&huart1, &RxData, sizeof(RxData));
	}
}
Thanks!

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Mon Jun 24, 2024 9:32 pm
by TomKerekes
I think that might have a flaw if the double data contains a command code 0x01 or 0x02.

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Tue Jun 25, 2024 5:38 am
by AlexSV
Possibly, but only if there is no 0x01 or 0x02 command code before it.

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Tue Jun 25, 2024 5:55 am
by TomKerekes
For example the sequence:

0xAA 0x01 0xDE 0x02

At this point all 3 flags are set causing double stores and so on.

I think once a command is received and receiving binary data it should not look for the other command until the first command is complete.

Such as:

Code: Select all

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
	if(huart == &huart1) {  
		if (RxData == 0xAA) {flag0 = 1;}
		if (RxData == 0x01 && flag0 && !flag2) {flag1 = 1;}
		if (flag0 && flag1)
		{
			RxData0[n] = RxData;
			n++;
			if(n==9)
			{
				n=0; flag0=0; flag1=0;
				HAL_UART_Transmit_IT(&huart1, (uint8_t *)TxData, sizeof(TxData));
				int i;
				for (i=1;i<9;i++){
				*(((unsigned char *)&a)+i-1) = RxData0[i] ;}
			}
		}
//------------------------------------------------------------------------------
		if (RxData == 0x02 && flag0 && !flag1) {flag2 = 1;}
		if (flag0 && flag2)
		{
			RxData0[n] = RxData;
			n++;
			if(n==9)
			{
				n=0; flag0=0; flag2=0;
				HAL_UART_Transmit_IT(&huart1, (uint8_t *)TxData, sizeof(TxData));
				int i;
				for (i=1;i<9;i++){
				*(((unsigned char *)&b)+i-1) = RxData0[i] ;}
			}
		}
		HAL_UART_Receive_IT (&huart1, &RxData, sizeof(RxData));
	}
}
HTH

Re: Unexpected 0x00 on the UART bus when starting data exchange.

Posted: Tue Jun 25, 2024 6:27 am
by AlexSV
Thanks Tom, great point!