设为首页 加入收藏

TOP

如何编写Linux操作系统下的设备驱动程序(二)
2014-11-17 10:40:02 来源: 作者: 【 】 浏览:45
Tags:如何 编写 Linux 操作系统 设备 驱动程序


驱动程序已经编译好了,现在把它安装到系统中去。


$ insmod –f test.o


如果安装成功,在/proc/devices文件中就可以看到设备test,并可以看到它的主设备号。要卸载的话,运行 :


$ rmmod test


下一步要创建设备文件。


mknod /dev/test c major minor


c 是指字符设备,major是主设备号,就是在/proc/devices里看到的。


用shell命令


$ cat /proc/devices


就可以获得主设备号,可以把上面的命令行加入你的shell script中去。


minor是从设备号,设置成0就可以了。


我们现在可以通过设备文件来访问我们的驱动程序。写一个小小的测试程序。


#include


#include


#include


#include


main()


{


int testdev;


int i;


char buf[10];


testdev = open(“/dev/test”,O_RDWR);


if ( testdev == -1 )


{


printf(“Cann”t open file \n”);


exit(0);


}


read(testdev,buf,10);


for (i = 0; i < 10;i++)


printf(“%d\n”,buf[i]);


close(testdev);


}


编译运行,看看是不是打印出全1 ?



首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇J2EE都包括哪些技术? 下一篇2012.11.5 苏州思科面试

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: