舞之韵艺术培训中心:教你如何在开发驱动时在图形界面下的终端打印字符!!

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 05:52:11
我们平时在图形界面下开一个终端(xterm),然后在程序里面开发程序时,用printk()打印一些我们需要打印的信息,但是这些信息不能在当前终端下面显示出来,只能通过dmesg才能查看,很麻烦,这两天从书上学了一个方法,拿出来和大家分享一下,简单的几句代码就可以解决这个问题,代码如下:
#include
#include
#include
#include
#include
#include

MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZHANG FEI LONG");//这些信息可以通过modinfo  XXX.o查看

static void print_string(char * str)
{
        struct tty_struct * my_tty;   //将要显示字符的终端
#if(LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,5))//条件编译判断kernel版本
        my_tty = current->tty;
#else
        my_tty = current->signal->tty;
#endif
        if(my_tty!=NULL)
        {
                ((my_tty->driver)->ops->write)(my_tty,
#if(LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9))
                0,
#endif
                str,
                strlen(str));
#if(LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9))
                ((my_tty->driver)->ops->write)(my_tty,0,"\015\012",2);
                #else
                ((my_tty->driver)->ops->write)(my_tty,"\015\012",2);
#endif
        }
}
static int __init print_string_init(void)
{
        print_string("The module has been inserted,Hello World");
        return 0;
}
static void __exit print_string_exit(void)
{
        print_string("The module has been removed,Farewell world");
}
module_init(print_string_init);
module_exit(print_string_exit);

代码里面的关键部分就是print_string(char *)这个函数了,以后我们就可以调用这个信息随便的在自己xterm里面打印信息了。