RT-thread(2)RT-Thread 控制台与重载void rt_hw_console_output()函数
按照RT-thread(1)cubemx +keil5环境,使用RT-Thread nano 。
https://www.cnblogs.com/excellentHellen/articles/16951617.html
1)在cubemx配置串口为异步模式 ,GPIO配置为上拉,高电平。

2)修改 rtcongfig.h 文件
/*rtconfig.h*/ // <h>Console Configuration // <c1>Using console // <i>Using console #define RT_USING_CONSOLE
3)重载void rt_hw_console_output()函数
在 board.c文件中添加 #include "usart.h"
并修改与添加如下代码
#ifdef RT_USING_CONSOLE #define UartHandle huart1 static int uart_init(void) { //#error "TODO 2: Enable the hardware uart and config baudrate." MX_USART1_UART_Init();// 串口初始化,中断方式接收字节,查询方式发送字节 // __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_IDLE);// 开空闲中断 // __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);// 开接收非空中断 return 0; } INIT_BOARD_EXPORT(uart_init); void rt_hw_console_output(const char *str) { //#error "TODO 3: Output the string 'str' through the uart." //#error "TODO 3: Output the string 'str' through the uart." rt_size_t i = 0, size = 0; char a = '\r'; __HAL_UNLOCK(&UartHandle); rt_enter_critical(); /*锁定任务调度器*/ size = rt_strlen(str); for (i = 0; i < size; i++) { if (*(str + i) == '\n') { HAL_UART_Transmit(&UartHandle, (uint8_t *)&a, 1, 1); } HAL_UART_Transmit(&UartHandle, (uint8_t *)(str + i), 1, 1); } rt_exit_critical(); /*解锁任务调度器*/ }
4)根据需要,调用rt_kprintf() 。
注意添加 #include <rtthread.h>
rt_kprintf ( "rt_kprintf()可以用来打印调试信息!\n");
rt_kprintf("%s %s:%d here!\r\n", __FILE__, __FUNCTION__, __LINE__);//当前行所在文件名、函数名、第**行代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)