RS485 Receive Buffer Corruption
I have two device on the Rs485 line, one of them transmit the data, another receiving the data.
Transmitter, transmit rs485TxBuffer, it is include `uint8_t rs485TxBuffer[2]={0xAA,0xBB}`.I expect the receiver fill the receive buffer with these data. But I have a problem in here.
My problem is:
I get receiver side very interesting printing value, I get rxbuffer like this:
Received Message From Rs485 line : aa, 00
Received Message From Rs485 line : bb, 00
Received Message From Rs485 line : aa, 00
Received Message From Rs485 line : bb, 00
Received Message From Rs485 line : aa, 00
But I expected the message get like this
aa,bb
These are my codes:
uint8_t rs485TxBuffer[2]={0xAA,0xBB};
uint8_t rs485RxBuffer[2] ={0x00, 0x00};
Transmitter codes:
while(1){
RS485_Set_Transmit_Mode();
while(HAL_UART_Transmit_IT(&huart2, (uint8_t*)rs485TxBuffer, 2) != HAL_OK)
{
// ...
}
uartReady = RESET;
while (uartReady != RESET);
}
void RS485_Set_Transmit_Mode(void)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, SET); // DE pin
}
void RS485_Set_Receive_Mode(void)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, RESET); // RE pin
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, RESET); // DE pin
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
uartReady = SET;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
uartReady = SET;
}Receiver codes:
while (1)
{
RS485_Set_Receive_Mode();
HAL_Delay(1);
while (HAL_UART_Receive_IT(&huart2, (uint8_t*)rs485RxBuffer, 2) != HAL_OK)
{
// ...
}
SerialPrint("Received Message From Rs485 line : %02x, %02x\n",rs485RxBuffer[0],rs485RxBuffer[1]);
}
void RS485_Set_Transmit_Mode(void)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, SET); // DE pin
}
void RS485_Set_Receive_Mode(void)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, RESET); // RE pin
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, RESET); // DE pin
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
uartReady = SET;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
uartReady = SET;
}When I didnt use the delay in my function, I didnt get correct values.
How can I solve the receiving values?
/* USART2 init function */
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}