linux重定向串口打印到telnet ssh遠程終端


如果要實時顯示printk 信息 可以參考  https://www.cnblogs.com/ChenChangXiong/p/11357416.html

 

有時候調試需要  但是沒有串口    使用telnet  ssh遠程登錄的時候 不能顯示啟動時候運行的程序的打印  這個時候需要重定向  

 

源碼:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <fcntl.h>
 5 #include <sys/ioctl.h>
 6 #include <unistd.h>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     int tty = -1;
11     char *tty_name = NULL;
12 
13     if(argc < 2)
14     {
15         printf("miss argument\n");
16         return 0;
17     }
18 
19     /* 獲取當前tty名稱 */
20     tty_name = ttyname(STDOUT_FILENO);
21     printf("tty_name: %s\n", tty_name);
22 
23     if(!strcmp(argv[1], "on"))
24     {
25         /* 重定向console到當前tty */
26         tty = open(tty_name, O_RDONLY | O_WRONLY);
27         ioctl(tty, TIOCCONS);
28         perror("ioctl TIOCCONS");
29     }
30     else if(!strcmp(argv[1], "off"))
31     {
32         /* 恢復console */
33         tty = open("/dev/console", O_RDONLY | O_WRONLY);
34         ioctl(tty, TIOCCONS);
35         perror("ioctl TIOCCONS");
36     }
37     else
38     {
39         printf("error argument\n");
40         return 0;
41     }
42 
43     close(tty);
44     return 0;
45 }

 

 

參考以下:  https://blog.csdn.net/lqxandroid2012/article/details/79165141

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM