Solution to the problem that rt_kprintf function and ulog in RT-Thread cannot output floating-point numbers

Recently, I found that RT-Thread cannot output floating-point numbers normally. When debugging, I found that the calculation results are correct, but the output numbers are not correct. The specific performance is that %f is not supported. The problem occurs in rt_kprintf() and ulog log output. .

The current solution after consulting the data is as follows:

rt_kprintf() cannot output floating point number problem solved

For details, please refer to the blog here: RT-thread rt_kprintf() function formats and outputs floating-point numbers

Modification method: Find the specific location of the rt_kprintf() function (under the /rt-thread/src/kservice.c file), and add the header file #include <stdio.h> at the top (remember to enable it in RT-Thread Settings libc support), and then in the specific implementation of this function, the original

length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);

change into

length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);

Then the internal content becomes

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);
}

Recompile and then print out floating-point numbers.

ulog log floating point output

In the more options of the RT-Thread Settings configuration software package function, choose to turn on floating point support (tossing to death, I don’t know if I have to manually turn on this function)
Insert picture description here
after turning it on, you can use LOG_I() and other functions to perform floating point numbers normally. Output.

Guess you like

Origin blog.csdn.net/moumde/article/details/108037790