RT-Thread: 自定义 printf 函数

说明: rt_kprintf 函数使用时只是指向设置好的调试串口,如果希望其他串口也有类似 rt_kprintf 的功能,本文介绍如何实现。

1.自定义 printf 代码部分

模仿 rt_kprintf 函数,创建一个指向别的 UART 口的自定义函数。

RT-Thread 自定义 printf 函数

/**
 * This function will print a formatted string on system console
 * 本函数基于 系统的 rt_kprintf 改动而来,主要改动了 指向串口硬件的变量  yl_uart_device
 * @param fmt the format
 */
void yl_kprintf(const char *fmt, ...)
{
    rt_device_t  yl_uart_device = serial_uart3;   /* 定义一个本函数用的串口硬件句柄 */

    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 = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
    if (length > RT_CONSOLEBUF_SIZE - 1)
        length = RT_CONSOLEBUF_SIZE - 1;

    if (yl_uart_device == RT_NULL)
    {
        rt_hw_console_output(rt_log_buf);
    }
    else
    {
        rt_uint16_t old_flag = yl_uart_device->open_flag;

        yl_uart_device->open_flag |= RT_DEVICE_FLAG_STREAM;

        //rt_device_write(serial_uart3, 0, rt_log_buf, length); /* 串口对应 RS232 */
        yl_rt_device_write(serial_uart3, 0, rt_log_buf, length);/* 串口对应硬件是RS485接口 */

        yl_uart_device->open_flag = old_flag;
    }

    va_end(args);
}
RTM_EXPORT(yl_kprintf);

2. 自定义printf函数 适配 RS485 接口功能

/* 自定义串口打印函数,调用这个函数会同时把数据从对应的串口输出 */
/* 这个函数针对 RS485 接口做了适配 */

/* 自定义串口打印函数,调用这个函数会同时把数据从对应的串口输出 */
/* 这个函数针对 RS485 接口做了适配 */

rt_size_t yl_rt_device_write(rt_device_t dev,
                          rt_off_t    pos,
                          const void *buffer,
                          rt_size_t   size)
{
    rt_pin_write(RS485_RE  , 1);
    rt_device_write(dev, pos, buffer, size);    
    rt_pin_write(RS485_RE  , 0);
    return 0;
}

3.说明

        函数定义了一个串口专用的句柄,这个句柄可以根据需要从哪个串口打印出数据对应的赋值,或者在其他位置赋值。

   rt_device_t  yl_uart_device = serial_uart3;   /* 定义一个本函数用的串口硬件句柄 */

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RT-Thread是一个实时操作系统,可以在嵌入式系统上运行。要在RT-Thread中使用多个串口,并使用rt_kprintf函数打印输出,您可以按照以下步骤进行操作: 1. 配置串口设备:在RT-Thread的配置文件(rtconfig.h)中,找到`RT_SERIAL_DEVICE`宏定义。如果需要使用多个串口,可以将该宏定义的值设置为串口设备的数量,例如`#define RT_SERIAL_DEVICE 2`表示有两个串口设备。 2. 初始化串口设备:在应用程序初始化的地方,通过调用`rt_device_init`函数初始化每个串口设备。例如,如果有两个串口设备,可以使用以下代码初始化它们: ```c rt_device_t serial1, serial2; serial1 = rt_device_find("uart1"); serial2 = rt_device_find("uart2"); rt_device_init(serial1); rt_device_init(serial2); ``` 3. 打开串口设备:在需要使用串口的地方,通过调用`rt_device_open`函数打开相应的串口设备。例如,如果要使用第一个串口设备,可以使用以下代码打开它: ```c rt_device_open(serial1, RT_DEVICE_FLAG_RDWR); ``` 4. 使用rt_kprintf函数打印输出:在需要打印输出的地方,可以使用rt_kprintf函数来代替标准库的printf函数。例如,使用以下代码在第一个串口设备上打印输出: ```c rt_kprintf("Hello, RT-Thread!\n"); ``` 通过上述步骤,您可以在RT-Thread中使用多个串口,并使用rt_kprintf函数进行打印输出。请确保配置文件和代码中的串口设备名称与实际硬件一致,并根据需求进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值