void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { int i; HAL_StatusTypeDef ret; int32_t data_len = 0; recv_bytes += Size; if(!parse_head_flag && recv_bytes >= PACKAGE_HEAD_SIZE){ data_len = parse_package_head(RX_IDLE_Buffer, PACKAGE_HEAD_SIZE); parse_head_flag = 1; } if(recv_bytes >= PACKAGE_HEAD_SIZE + data_len + 2){ int size; int err = parse_package(RX_IDLE_Buffer, PACKAGE_HEAD_SIZE + data_len + 2); memcpy(TX_ACK_Buffer, RX_IDLE_Buffer, PACKAGE_HEAD_SIZE- 2); size = make_ack_package(TX_ACK_Buffer, TX_ACK_BUFFER_SIZE, err); HAL_UART_Transmit(&huart2, TX_ACK_Buffer, size, 1000); parse_head_flag = 0; recv_bytes = 0; } ret = HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_IDLE_Buffer + recv_bytes, RX_IDLE_BUFFER_SIZE- recv_bytes); if(ret != HAL_OK){ printf("Fail to HAL_UARTEx_ReceiveToIdle_IT,ret:%d\r\n",ret); return; } return; }写注释
时间: 2023-08-29 13:08:16 浏览: 306
以下是对这段代码的注释:
```c
/**
* @brief UART接收中断回调函数
* @param huart: UART句柄
* @param Size: 接收数据的字节数
*/
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
int i;
HAL_StatusTypeDef ret;
int32_t data_len = 0;
recv_bytes += Size; // 更新接收字节数
// 如果还未解析头部并接收到了足够的字节数,则解析头部
if(!parse_head_flag && recv_bytes >= PACKAGE_HEAD_SIZE){
data_len = parse_package_head(RX_IDLE_Buffer, PACKAGE_HEAD_SIZE);
parse_head_flag = 1;
}
// 如果接收到了一个完整的数据包,则解析数据包并生成ACK包,并通过UART发送ACK包
if(recv_bytes >= PACKAGE_HEAD_SIZE + data_len + 2){
int size;
int err = parse_package(RX_IDLE_Buffer, PACKAGE_HEAD_SIZE + data_len + 2);
memcpy(TX_ACK_Buffer, RX_IDLE_Buffer, PACKAGE_HEAD_SIZE- 2);
size = make_ack_package(TX_ACK_Buffer, TX_ACK_BUFFER_SIZE, err);
HAL_UART_Transmit(&huart2, TX_ACK_Buffer, size, 1000); // 通过UART发送ACK包
parse_head_flag = 0;
recv_bytes = 0;
}
// 继续异步接收UART数据
ret = HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_IDLE_Buffer + recv_bytes, RX_IDLE_BUFFER_SIZE- recv_bytes);
// 如果接收失败,则输出错误信息
if(ret != HAL_OK){
printf("Fail to HAL_UARTEx_ReceiveToIdle_IT,ret:%d\r\n",ret);
return;
}
return;
}
```
阅读全文