1. Use CubeMX to configure serial port 1
(1) Configure the baud rate of serial port 1 to 9600. If the baud rate is set too high, sometimes the message will not be received. Encountered. Then open the serial port interrupt and receive data in the interrupt. But don’t process the data during the interruption, try to do as little as possible during the interruption
2. Functions used in programming
(1) The HAL library provides many serial port operation functions, including polling mode for sending and receiving, and interrupt mode Send and receive and DMA mode to send and receive, I use interrupt mode here
(2 ) The three functions used here, let me talk about their meaning, other functions are similar to
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef huart, uint8_t pData, uint16_t Size);
//The function of this function To enable the serial port receiving interrupt and specify the buffer area to store the received data, set the data size received from the serial port. Pay special attention here, this function will only return after receiving Size data from the serial port, otherwise it will block.
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef huart, uint8_t pData, uint16_t Size, uint32_t Timeout);
//The function of this function is to send data to the specified serial port, pData is the data address, and Size is the data size , Timeout is the timeout period.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
//This function is the callback function after the serial port receives the data. In the serial port interrupt, the following functions will be called in sequence: USART1_IRQHandler(), HAL_UART_IRQHandler(&huart1), UART_Receive_IT (), HAL_UART_RxCpltCallback(). The callback function can be implemented by itself in the main function. The HAL library has only definitions and no specific implementation. We can do some processing in the callback function to process the data we get.
3.代码实现
(1)这里做一个简单的串口与上位机串口助手通信的实验,将串口助手发送给开发板串口1 The data is sent back to the serial port assistant. Note that when programming with the code generated by CubeMX, the code you add must be added between the various “BEGIN” and “END”.
(2) First set the global variables uint8_t RxCounter1=0, RxBuffer1[50]={0}, RxTemp1=0, F_Usart1=0 in main; it is used to save the data received from the serial port, F_Usart1 is the reception completion flag , In the while, it will always check whether it has changed, and then perform related operations. RxTemp1 is a single buffer, available when receiving one byte
(3) In the main() function, turn on the serial port reception interrupt before while(), fill in the relevant parameters, HAL_UART_Receive_IT(&huart1,&RxTemp1,1); Here is the setting, first save the data to RxTemp1, and then save it to RxBuffer1 in the callback function , Is to store the data sent from the serial port in RxBuffer1 completely, so that I can perform other processing
(4) To implement the callback function, there are two methods here, method one can save the data and then operate, method two send the data directly without operation, carefully look at the notes in the picture
(5) Processing in while
4.补充说明
(1)至此,串口实验基本完成. Let me talk about another problem encountered in my project. I have two serial communication to deal with. First, I need to send data to serial port 3 in the program, and then send the feedback content of serial port 3 to the serial port after some processing. 1. At this time, you need to set the data buffer in the interrupt function (HAL_UART_Receive_IT(&huart3,&RxBuffer3[RxCounter3++],1);) directly as a global array, and then directly write the function to enable the interrupt in the callback function, which is no longer needed RxTemp1 flag bit, but please pay attention to delay 200ms every time the program sends data, otherwise the data will not be received.看图中针对串口3的操作即可
HAL library serial interrupt reception
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1226 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC