cancel
Showing results for 
Search instead for 
Did you mean: 

Why elements in an array elements shift?

ahmetgunduz
Associate III

Hi, i am using stm32f4. in my project i have one transmitter and one receiver.

i transmit an 2 byte integer value using

HAL_UART_Transmit(&huart1,&temp_byte1,sizeof(temp_byte1), 500);
HAL_UART_Transmit(&huart1,&temp_byte2,sizeof(temp_byte2), 500);

and on the receiver part. i read these values using

void USART6_IRQHandler(void)
{
HAL_UART_Receive_IT(&huart6,&rx_data,1);
temp_data[rx_index]=rx_data;
			rx_index++;
			if(rx_index==2)
			{
			rx_index=0;			
			temp = ((temp_data[0]<<8)|temp_data[1]);
			if(temp!=79){ //sometimes i see a 79 in my buffer!
			hc05_flag = 3; //irrelevant flag with this question
			nm=0; //irrelevant flag with this question				
			}
}

it works fine for one time. but after, i see  temp_data[1]  in  temp_data[0]. it shift 8 bits and i see a big  temp value (63744 etc.) . where is my problem?

3 REPLIES 3
raptorhal2
Lead

My guess is that this is a classroom exercise.

Your problem is that you have two processes and no method to synchronize related transmits and receives.

Its your project. I will leave it up to you to figure out how to keep them in sync.

Cheers, Hal

HAL_UART_Receive_IT() does not work like this. You need to collect the data in the callback, as the function you're using should return immediately, not when it actually has data.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Piranha
Chief II

You must use HAL_UART_IRQHandler() and HAL_UART_RxCpltCallback().