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

Moderators: TomKerekes, dynomotion

Post Reply
AlexSV
Posts: 5
Joined: Mon Jun 24, 2024 4:05 pm

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

Post by AlexSV » Mon Jun 24, 2024 4:17 pm

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
Attachments
001.jpg

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

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

Post by TomKerekes » Mon Jun 24, 2024 5:22 pm

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.
Regards,

Tom Kerekes
Dynomotion, Inc.

AlexSV
Posts: 5
Joined: Mon Jun 24, 2024 4:05 pm

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

Post by AlexSV » Mon Jun 24, 2024 8:23 pm

Thanks for the answer, I'll rewrite the code.

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

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

Post by TomKerekes » Mon Jun 24, 2024 8:48 pm

Hi Alex,

You might see the ReceiveDoubles.c example.
Regards,

Tom Kerekes
Dynomotion, Inc.

AlexSV
Posts: 5
Joined: Mon Jun 24, 2024 4:05 pm

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

Post by AlexSV » Mon Jun 24, 2024 9:19 pm

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!

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

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

Post by TomKerekes » Mon Jun 24, 2024 9:32 pm

I think that might have a flaw if the double data contains a command code 0x01 or 0x02.
Regards,

Tom Kerekes
Dynomotion, Inc.

AlexSV
Posts: 5
Joined: Mon Jun 24, 2024 4:05 pm

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

Post by AlexSV » Tue Jun 25, 2024 5:38 am

Possibly, but only if there is no 0x01 or 0x02 command code before it.

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

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

Post by TomKerekes » Tue Jun 25, 2024 5:55 am

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
Regards,

Tom Kerekes
Dynomotion, Inc.

AlexSV
Posts: 5
Joined: Mon Jun 24, 2024 4:05 pm

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

Post by AlexSV » Tue Jun 25, 2024 6:27 am

Thanks Tom, great point!

Post Reply