Chinaunix首页 | 论坛 | 博客
  • 博客访问: 714737
  • 博文数量: 104
  • 博客积分: 4320
  • 博客等级: 上校
  • 技术积分: 1948
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-30 14:42
文章分类

全部博文(104)

文章存档

2012年(4)

2011年(65)

2010年(35)

分类: LINUX

2010-08-11 12:50:49

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
#include
#include
#include
#include
#include
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

 
阅读(3026) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~