cancel
Showing results for 
Search instead for 
Did you mean: 

Usart received data misaligned in buffer when I try to receive data from 2 modbus slaves. It works fine with 1 slave but when both the slaves are connected, after a period of time, usart stars misaligning received data. Can someone help?

sne_123
Associate III

This is my usart handler

 Also I clear the 1st received frame before receiving the 2nd modbus frame.

void HAL_UART_IRQHandler1(UART_HandleTypeDef *huart)

{

__HAL_UART_CLEAR_PEFLAG(huart);

__HAL_UART_ENABLE_IT(huart, UART_IT_ERR);

__HAL_UART_ENABLE_IT(huart,UART_IT_LBD);

__HAL_UART_ENABLE_IT(huart, UART_IT_PE);

__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);

__HAL_UART_CLEAR_OREFLAG(huart); 

// check if the USART1 receive interrupt flag was set

if( __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) )

t = (uint8_t)(huart->Instance->DR );

if (t != '\n')

{

// Concat char to buffer

if (k < 13 -1)

{

received_string[k] = t;

k++;

}

else

{

received_string[k] = t;

k = 0;

}

}

}

__HAL_UART_CLEAR_PEFLAG(huart);

__HAL_UART_DISABLE_IT(huart, UART_IT_PE);

__HAL_UART_DISABLE_IT(huart, UART_IT_ERR);

__HAL_UART_DISABLE_IT(huart,UART_IT_LBD);

HAL_NVIC_ClearPendingIRQ(USART3_IRQn);

}

11 REPLIES 11

> The problem was modbus slave sends me incomplete frame at times (last byte was missing in the frame).

> So now before receiving a new frame, every time I am resetting the count 'k'. I Also inserted a delay between receiving frames from 2 modbus slaves.

A proper and robust device must be able to handle corrupted frame.

With Modbus RTU, the spec. defines an inter-frame delay where the bus is idle.

If this period is up (including a grace tolerance of some percent), process the frame, or discard it if invalid.

Okay. ..