AnsweredAssumed Answered

HAL UART frame: data out of order

Question asked by Sannecy.Prathik on Apr 22, 2017
Latest reply on Apr 27, 2017 by Sannecy.Prathik

Hey guys,

 

Newbie here. I am having some issues using the HAL library to receive data through UART. Data is being received continuously (ie there is almost no delay between one frame and another). Below is some code showing my receiving code:

 

void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */

/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */

//receive_man is an array of uint16_t elements and contains 6 elements
HAL_UART_Receive_IT(&huart2, (uint8_t*) receive_man, 2*_RECEIVE);

//output_ready is a flag showing that data is ready for processing.
output_ready=1;
/* USER CODE END USART2_IRQn 1 */
}

 

Here is how I initialized UART:

 

/* USART2 init function */
static void MX_USART2_UART_Init(void)
{

huart2.Instance = USART2;
huart2.Init.BaudRate = 230400;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_2;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_8;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}

}

 

 

 

 When I was using a debugger for testing, I noticed that most of the time, the data I was receiving was not the same as the data that I was transmitting. However, some of the time, the data matched. I suspect that this may be because most of the time, UART doesn't correctly latch on to start/stop bits.

 

Is it possible that UART taking using bits from packet 1 and packet 2 as start/stop bits? For example, is it possible for UART to take the last X data bits from packet one to be start bits, and the first X bits of packet 2 to be stop bits? If so, is there a way to fix this (besides slowing down the transmission)? If not, is there any other reason why the received data wouldn't match the transmitted data? Any help would be appreciated. Thank you!

Outcomes