http://blog.chinaunix.net/uid-14753126-id-93946.html
scull驱动模拟设备。编写读写设备的控制文件testscull.c向scull发送和接收数据。
1 安装scull.ko
在ldd3驱动源码文件夹中,进入scull文件夹,make,生成scull.ko,运行其中的shell文件:scull_load,完成scull.ko的安装。
# cd /sys/module/scull/parameters/
#cat scull_major
253
#cat scull_minor
0
可知scull.ko的主次设备号为253 0
进入/dev 创建设备文件:
#cd /dev
#mknod sculldev c 253 0
2 编写测试文件testscull.c
代码:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main()
{
int fd,len;
char inbuf[20];
char outbuf[20]="scull dev test!";
fd=open("/dev/sculldev",O_WRONLY);
if(fd<0)
{printf("Error openning the device of sculldev for wrITing!\n");
exit(1);
}
len=write(fd,outbuf,strlen(outbuf));
if(len<0)
{ printf("Error writing to the device!\n");
close(fd);
exit(1);
}
printf("writing %d bytes to the device!\n",len);
close(fd);
fd=open("/dev/sculldev",O_RDONLY);
if(fd<0)
{
printf("Error openning the device of sculldev for reading!\n");
exit(1);
}
len=read(fd,inbuf,len);
if(len<0)
{printf("Error reading from the device!\n ");
close(fd);
exit(1);
}
printf("reading %d bytes from the device!\n",len);
printf("%s\n",outbuf);
}
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main()
{
int fd,len;
char inbuf[20];
char outbuf[20]="scull dev test!";
fd=open("/dev/sculldev",O_WRONLY);
if(fd<0)
{printf("Error openning the device of sculldev for wrITing!\n");
exit(1);
}
len=write(fd,outbuf,strlen(outbuf));
if(len<0)
{ printf("Error writing to the device!\n");
close(fd);
exit(1);
}
printf("writing %d bytes to the device!\n",len);
close(fd);
fd=open("/dev/sculldev",O_RDONLY);
if(fd<0)
{
printf("Error openning the device of sculldev for reading!\n");
exit(1);
}
len=read(fd,inbuf,len);
if(len<0)
{printf("Error reading from the device!\n ");
close(fd);
exit(1);
}
printf("reading %d bytes from the device!\n",len);
printf("%s\n",outbuf);
}
3 测试
#gcc testscull.c -o testscull
用管理员权限运行下面程序
#./testscull