|
4# 亚瑟王
我试过,真的可以啦。哈哈!!!!
再次感谢亚瑟王!
照例把我的代码贴出来。虽然简单,但希望以后与我有相同问题的朋友能看到,能够有所帮助。驱动里面添加的ioctl函数:
static int tq2440_adc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
if(cmd<=0)
{
return -EINVAL;
}
else
{
adcdev.channel=arg;
return 0;
}
}
这个结构体也添加了
static struct file_operations dev_fops = {
owner: THIS_MODULE,
open: tq2440_adc_open,
read: tq2440_adc_read,
ioctl: tq2440_adc_ioctl,
release: tq2440_adc_release,
};
应用程序里面修改为:(我这里是显示两路AD的值,分别是channel1和channel2)
*************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
int main(void)
{
int fd ;
char temp = 1;
fd = open("/dev/adc", 0);
if (fd < 0)
{
perror("open ADC device !");
exit(1);
}
for( ; ; )
{
char buffer[30];
char len_r ;
char channel1=1,channel2=2;
ioctl(fd,1,channel1);
len_r = read(fd, buffer, sizeof buffer -1);
if (len_r )
{
buffer[len_r] = '\0';
int value;
sscanf(buffer, "%d", &value);
printf("ADC Value1: %d\n", value);
}
else
{
perror("read ADC device !");
exit(1);
}
ioctl(fd,1,channel2);
len_r = read(fd, buffer, sizeof buffer -1);
if (len_r )
{
buffer[len_r] = '\0';
int value;
sscanf(buffer, "%d", &value);
printf("ADC Value2: %d\n", value);
}
else
{
perror("read ADC device !");
exit(1);
}
sleep(1);
}
adcstop:
close(fd);
}
哈哈!!!!!太开心啦! |
|