I have a problem with an STM23F103 I'm using UART to transmit and receive data.the data is sent and another board replies. I can see the communication with a Logic Analyzer which looks fine on it.but the received data on STM32 have the first byte either from the first byte of the packet send or from last byte of the last received packet.I don't know what's wrong with my code but I can't figure out how to solve the issue
Here is the code in the main
uint8_t b[5] = {0xAA,0xBB,0xCC,0xDD,0xEE};
HAL_UART_Transmit(&huart3,b, sizeof(b), 100);
uint8_t r[5]={0,0,0,0,0};
HAL_UART_Receive(&huart3, r, sizeof(r), 100);
HAL_Delay(100);
uint8_t d[5] = {0x11,0x22,0x33,0x44,0x55};
HAL_UART_Transmit(&huart3,d, sizeof(d), 100);
uint8_t r2[5]={0,0,0,0,0};
HAL_UART_Receive(&huart3, r2, sizeof(r2), 100);
HAL_Delay(100);
and here the Init
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 10400;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
The packet that appears on the logic analyzer is 0xA1,B2,C3,D4,E5 but on the STM32 it receives AA,A1,B2,C3,D4 the first byte is always the same as the packet that I sent.but only on the first packet.
On the other packets are received like this 0xE5,66,77,88,99 while it should be 66,77,88,99,AA but I get E5 from the last received packet. I thought that that packet has not being received so I thought by increasing the size of r and r2 from 5 to 6 it would solve the issue but it doesn't. I receive AA,A1,B2,C3,D4,E5 for r and 0xE5,66,77,88,99,AA for r2.
I hope its detailed enough to see the issue.
I'm using STM32CubeIDE