最近做实验的时候发现RT-Thread无法正常输出浮点数,在调试的时候发现计算结果正确,但是输出数字却对不上,具体表现为不支持 %f,问题出现在rt_kprintf()和ulog日志输出。
查阅资料过后目前解决方法如下:
rt_kprintf()无法输出浮点数问题解决
具体参考这边博客:RT-thread rt_kprintf()函数格式化输出浮点数
修改方法:查找到 rt_kprintf() 这个函数的具体位置(/rt-thread/src/kservice.c文件下),在最上面添加头文件 #include <stdio.h>(记得在RT-Thread Settings里面开启libc支持),然后在这个函数具体实现里面,将原来的
length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
修改为
length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
那么内部具体内容就变为
void rt_kprintf(const char *fmt, ...)
{
va_list args;
rt_size_t length;
static char rt_log_buf[RT_CONSOLEBUF_SIZE];
va_start(args, fmt);
/* the return value of vsnprintf is the number of bytes that would be
* written to buffer had if the size of the buffer been sufficiently
* large excluding the terminating null byte. If the output string
* would be larger than the rt_log_buf, we have to adjust the output
* length. */
length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
if (length > RT_CONSOLEBUF_SIZE - 1)
length = RT_CONSOLEBUF_SIZE - 1;
#ifdef RT_USING_DEVICE
if (_console_device == RT_NULL)
{
rt_hw_console_output(rt_log_buf);
}
else
{
rt_uint16_t old_flag = _console_device->open_flag;
_console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
rt_device_write(_console_device, 0, rt_log_buf, length);
_console_device->open_flag = old_flag;
}
#else
rt_hw_console_output(rt_log_buf);
#endif
va_end(args);
}
重新编译即可使用打印输出浮点数。
ulog日志浮点数输出
在RT-Thread Settings配置软件包功能的更多选项里面,选择开启浮点数支持(折腾死我了,都不知道还要手动开这个功能)
开启之后即可正常使用LOG_I()等函数进行浮点数输出。