简单字符设备驱动程序(3)

(3)编写用户态的测试程序如下:

/*

*=======================================================================

* Filename: usr_test.c

*

* Description:

*

* Version: 1.0 (02/26/2013)

* Created: xhzuoxin(xiahouzuoxin@163.com)

* Compiler: gcc

*=======================================================================

*/

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

intmain(void)

{

int testdev;

int n, i, ret;

char write_buf[] = "xiahouzuoxin";

char buf[12];

testdev =open("/dev/mycdev", O_RDWR);

if (testdev == -1) {

printf("cannot open file.\n");

exit(1);

}

n = sizeof(write_buf)/sizeof(char);

if (ret = write(testdev, write_buf, n) < n) {

printf("write error!\n");

exit(1);

}

// close(testdev);

// testdev =open("/dev/mycdev", O_RDWR);

lseek(testdev,0, SEEK_SET);

if (testdev == -1) {

printf("cannot open file.\n");

exit(1);

}

if (ret = read(testdev, buf, n) < n) {

printf("read error!\n");

exit(1);

}

for (i=0; i<n; i++) {

printf("%c", buf[i]);

}

printf("\n");

close(testdev);

return 0;

}

在PC机上使用arm-linux-gcc usr_test.c –ousr_test编译,通过NFS共享或ftp下载到Tiny6410开发板上执行./usr_test(超级终端输入)。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c16ab294b611fbab13f44542a5d705f2.html