串口输出重定向

日常现象

在调试嵌入式设备的时候需要接串口,通过串口将打印输出到显示终端。但是很多时候接串口是设备是一件很麻烦的事情,比如,串口板,串口线等。

分析

内核的打印不能重定向过来,但是应用层打印可以重定向打印过来查看内核的打印,

cat /proc/kmsg

在输出完缓冲区内容后,会阻塞卡住,内核有新的输出时会继续输出。如果要把内核打印到telnet,那么可能需要修改源文建printk.c。kernel和user空间下都有一个console,关系到kernel下printk的方向和user下printf的方向,实现差别很大。kernel下的console是输入输出设备driver中实现的简单的输出console,只实现write函数,并且是直接输出到设备。user空间下的console,实际就是tty的一个特殊实现,大多数操作函数都继承tty,所以对于console的读写,都是由kernel的tty层来最终发送到设备。

实现代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#define FILE_PATH 512

int main(int argc, char * argv[])
{
    int tty = -1;
    int fd  = -1;
    char * tty_name = NULL;

    if (argc < 2)
    {
    ¦   printf("miss argv \n");
    ¦   return -1;
    }

    /* 获取当前tty名称 */
    tty_name = ttyname(STDOUT_FILENO);
    if (!tty_name)
    {
    ¦   printf(" get error tty_name \n");
    ¦   return -1;
    }
    printf("get ok tty_name: %s \n", tty_name);

    struct timeval lt;
    gettimeofday(&lt, NULL);

    char fname[FILE_PATH];
    memset(fname, 0, FILE_PATH);
    snprintf(fname, FILE_PATH, "/media/tty_%u.log", (unsigned int)lt.tv_usec);

    if (!strcmp(argv[1], "on"))
    {
    ¦   /* 重定向console到当前tty */
    ¦   tty = open(tty_name, O_RDONLY | O_WRONLY);
    ¦   ioctl(tty, TIOCCONS);
        
        // 写到文件/media/tty_%u.log中
    ¦   //fd = open(fname, O_RDWR|O_CREAT|O_APPEND, 0777);
    ¦   //ioctl(fd, TIOCCONS);
    ¦   perror("ioctl TIOCCONS");
    }
    else if (!strcmp(argv[1], "off"))
    {
    ¦   /* 恢复console */
    ¦   tty = open("/dev/console", O_RDONLY | O_WRONLY);
    ¦   ioctl(tty, TIOCCONS);
    ¦   perror("ioctl TIOCCONS");
    }
    else
    {
    ¦   printf("error argument\n");
    ¦   return -1;
    }

    //close(fd);
    close(tty);
    return 0;
}

根据以上代码注释可以有两种选择,第一将其重定向到终端显示,第二将其重定向到.log文件中参考 https://github.com/xbalabala/ttyrpld

编译

mips-linux-uclibc-gnu-gcc -g console.c -o ./sharenfs/console.out

说明

    mips-linux-uclibc-gnu-gcc 是交叉编译工具,编译之后跑在君正平台。

在STM32 HAL中,可以通过重定向串口输出来实现打印功能。具体步骤如下: 1. 首先,在工程中打开stm32f4xx_hal_uart.c文件,找到`HAL_UART_Transmit`函数。 2. 在该函数中,找到以下代码: ```c /* Send the string character by character */ while (*pData != '\0') { /* Wait for the UART Transmit Data Register to be empty */ while (!__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE)) { } /* Transmit one byte */ huart->Instance->DR = (uint8_t)(*pData & 0xFF); pData++; } ``` 将其替换为以下代码: ```c /* Send the string character by character */ while (*pData != '\0') { /* Wait for the UART Transmit Data Register to be empty */ while (!__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE)) { } /* Transmit one byte */ ITM_SendChar(*pData); pData++; } ``` 3. 接下来,在main.c文件中添加以下代码,以重定向printf函数: ```c #ifdef __GNUC__ /* With GCC, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } ``` 4. 最后,在main函数中添加以下代码,以启用重定向功能: ```c /* Enable USART2 interrupt */ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* Enable ITM printf redirection */ ITM_Init(); /* Redirect STDOUT to USART2 */ stdout = &huart2; ``` 通过以上步骤,你就可以在STM32 HAL中实现串口输出重定向了。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeathXian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值