# pt_uart **Repository Path**: qianqi_gitee/pt_uart ## Basic Information - **Project Name**: pt_uart - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-28 - **Last Updated**: 2025-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pt_uart #### 介绍 pt协程+串口队列发送实现的基本任务框架 #### 原型代码 ``` int printf_task(struct pt *pt) { static uint32_t sec = 0; PT_BEGIN(pt); sec = HAL_GetTick(); PT_WAIT_UNTIL(pt, HAL_GetTick() > sec + 100); // 等待1秒,但不阻塞 usart_send_str("hello", &cmd); PT_END(pt); } ``` #### 宏展开后的代码 ``` int printf_task(struct pt *pt) { static uint32_t sec = 0; switch ((pt)->lc) { case 0: sec = HAL_GetTick(); (pt)->lc = __LINE__; case __LINE__: if (!(HAL_GetTick() > sec + 100)) return 0; usart_send_str("hello", &cmd); } (pt)->lc = 0; return 1; } ``` #### 格式调整后 调整格式后的代码 会出问题 因为__LINE__已经变了 不在同一行 在执行case 0之后 并没有退出switch,因为 没有break,所以会接着执行对应的line的case, 然后下一次进入协程时,这个line还会保存下来,就会直接跳入line对应的case中,而不是又从case0开始 ``` int printf_task(struct pt *pt) { static uint32_t sec = 0; switch ((pt)->lc) { case 0: sec = HAL_GetTick(); (pt)->lc = __LINE__; case __LINE__: if (!(HAL_GetTick() > sec + 100)) return 0; usart_send_str("hello", &cmd); } (pt)->lc = 0; return 1; } ``` #### 仿真中查看 已经将行号赋给变量 ![输入图片说明](image2.png) #### 实际效果 ![输入图片说明](image.png)