[demo程序] STM32的HAL库UART_Receive解决不同数据长度

[复制链接]
 楼主| 杨寅辉 发表于 2019-10-30 22:03 | 显示全部楼层 |阅读模式
  1. 在stm32f1xx_hal_uart.c文件下修改
  2. /**
  3.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Receives an amount of data in non blocking mode
  4.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  5.   *                the configuration information for the specified UART module.
  6.   * @retval HAL status
  7.   */
  8. //---------------------------------------------------------------------------------
  9. uint16_t flag,Rx_Size;
  10. //---------------------------------------------------------------------------------
  11. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  12. {
  13.   uint16_t* tmp;
  14.   uint32_t tmp_state = 0;
  15.   
  16.   tmp_state = huart->State;
  17.   if((tmp_state == HAL_UART_STATE_BUSY_RX) || (tmp_state == HAL_UART_STATE_BUSY_TX_RX))
  18.   {
  19.     if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  20.     {
  21.       tmp = (uint16_t*) huart->pRxBuffPtr;
  22.       if(huart->Init.Parity == UART_PARITY_NONE)
  23.       {
  24.         *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  25.         huart->pRxBuffPtr += 2;
  26.       }
  27.       else
  28.       {
  29.         *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  30.         huart->pRxBuffPtr += 1;
  31.       }
  32.     }
  33.     else
  34.     {
  35.       if(huart->Init.Parity == UART_PARITY_NONE)
  36.       {
  37.         *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  38.                 //--------------------------------STA-------------------------------------------------
  39.                 if(flag == 100)
  40.                 {                    
  41.                         if(((uint8_t)(huart->Instance->DR & (uint8_t)0x00FF))==0x05)Rx_Size=1;
  42.                         if(((uint8_t)(huart->Instance->DR & (uint8_t)0x00FF))==0x06)Rx_Size=0;
  43.                         flag = 101;
  44.                 }
  45.                 else if(flag < 100) Rx_Size =0;
  46.                
  47.                 if(huart->RxXferCount ==15)
  48.                 {
  49.                     if(((uint8_t)(huart->Instance->DR & (uint8_t)0x00FF))==0x1A) flag += 10;        
  50.                     else Rx_Size =255;
  51.                 }
  52.                 if(huart->RxXferCount ==14)
  53.                 {
  54.                     if(((uint8_t)(huart->Instance->DR & (uint8_t)0x00FF))==0x5A) flag += 20;
  55.                     else Rx_Size =255;
  56.                 }
  57.                 if(huart->RxXferCount ==11)
  58.                 {
  59.                     if(((uint8_t)(huart->Instance->DR & (uint8_t)0x00FF))==0x04) flag += 30;
  60.                     else Rx_Size =255;
  61.                 }
  62.                 if(huart->RxXferCount ==9)
  63.                 {
  64.                     if(((uint8_t)(huart->Instance->DR & (uint8_t)0x00FF))==0x00) flag += 40;
  65.                     else Rx_Size =255;
  66.                 }
  67.                 //---------------------------------END------------------------------------------------
  68.       }
  69.       else
  70.       {
  71.         *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  72.       }
  73.     }
  74.     if(--huart->RxXferCount == Rx_Size || Rx_Size == 255)//-------------------------
  75.     {
  76.       __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);

  77.       /* Check if a transmit process is ongoing or not */
  78.       if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  79.       {
  80.         huart->State = HAL_UART_STATE_BUSY_TX;
  81.       }
  82.       else
  83.       {
  84.         /* Disable the UART Parity Error Interrupt */
  85.         __HAL_UART_DISABLE_IT(huart, UART_IT_PE);

  86.         /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  87.         __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);

  88.         huart->State = HAL_UART_STATE_READY;
  89.       }
  90.             //---------------------------------------------------------------------------------
  91.             flag = 0;    ////////////////
  92.             Rx_Size = 0; //////////////
  93.             //---------------------------------------------------------------------------------
  94.       HAL_UART_RxCpltCallback(huart);
  95.       return HAL_OK;
  96.     }
  97.     return HAL_OK;
  98.   }
  99.   else
  100.   {
  101.     return HAL_BUSY;
  102.   }
  103. }


aoyi 发表于 2019-11-13 10:03 | 显示全部楼层
非常感谢分享
drer 发表于 2019-11-13 10:23 | 显示全部楼层
非常感谢分享
gwsan 发表于 2019-11-13 10:51 | 显示全部楼层
非常不错的资料
天灵灵地灵灵 发表于 2020-2-10 18:05 | 显示全部楼层
两者通用?
internally 发表于 2020-2-11 11:14 | 显示全部楼层
解决不同长度通讯数据问题,可以采用超时断帧方法
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

295

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

39

主题

295

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部