The serial port reception is normal at the beginning, but after a period of time no data can be received, other programs are normal, and there is no crash

After initialization, if the reception has been normal, it means that there is no problem with your basic configuration. After a period of time, you will not receive any data, and your program is still running, and there is no hardware error or the like. This situation is generally It is because the amount of data is too large that the serial port is overloaded and downtime.

How to determine whether your serial port is overloaded and down? Connect your single-chip microcomputer to enter the simulation, select your serial port number that may be down at present in peripherals->system view->USART, and the register of the serial port will appear, find the ISR register in the new page card, and scroll down Find ORE and see if it is marked with √. This phenomenon will occur when a serial port overload error occurs.

Give a simple and direct solution. You can use this method as a basis and change according to your needs:

Use the timer already opened in your program, the cycle does not need to be too long or too short, 1 second to 1 minute is fine, as long as you can accept it, directly judge whether ORE is set, the method of judging ORE is as follows:

Standard library query function USART_GetITStatus(USARTx, USART_IT_ORE)

hal library query function __HAL_UART_GET_FLAG(&huart1,UART_FLAG_ORE);

If the query result is 1, directly re-initialize the corresponding serial port. I don’t need to list the functions for initializing the serial port. It is the one called at the beginning of the program, such as usart1_init(). The hal library has its own generated init. If you call it again, the serial port will restart.

The solution of the hal library is posted below. Taking serial port 2 as an example, the standard library is similar, and even the initialization of the standard library is simpler, just call the serial port initialization function starting from main again.

//清理ORE的标志位
__HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
//可能需要在这插入
//如果你存储过初始化的数据,并且不想修改,直接重新初始化串口2
HAL_UART_Init(huart2);

//如果有你想修改,或者之前没有初始化的话,需要将下面的一段放到上面的插入处
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;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
    {
        Error_Handler();
    }

It's that simple.

If it is useful to you, leave a comment or like it~

おすすめ

転載: blog.csdn.net/u013400631/article/details/114594878