1, serial connection error: It may be related to the serial connection mode. Need to check whether the connection is correct, whether the baud rate settings match, and whether the signal line is short-circuited or virtual.
2. Clock setting error: serial communication needs accurate clock control. If the clock is set incorrectly, it will lead to an error in receiving data. In this case, it is necessary to check whether the system clock source, clock frequency division coefficient and other settings are correct.
3. Data format mismatch: If the data formats of the sender and the receiver do not match, the received data may always be all 0 1. It is necessary to check the data format and encoding method of the sender and whether the decoding method of the receiver is correct.
4. Interrupt flag is not cleared: If the interrupt flag is not cleared when the serial port receives data, it will lead to repeated reception of the same data, so the data will always be 0 1. In this case, it is necessary to clear the interrupt flag in the interrupt program clearly in order to process the received data.
5. Buffer overflow: If the receiving buffer capacity is too small or the data is received too fast, it will lead to buffer overflow and data error. It is necessary to check the capacity and receiving rate of the receiving buffer, adjust the buffer size or shorten the data transmission interval to solve the problem.
To realize serial port receiving data in STM32, it is necessary to set serial port parameters, including baud rate, data bit, stop bit, check bit and so on. , need to be set according to the actual situation. You also need to initialize serial port interrupts. Use the function HAL _ UART _ receive _ it () of Hal library to initialize the serial port interrupt and open the serial port to receive the interrupt. HAL_UART_Receive_IT () function can be used to receive data and process the received data.