001- linux - 字符设备控制之ioctl --- ADC 读数据

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(int argc , char* argv[])
{

	int fd ;
	char *adc = "/dev/adc";
	char buffer[512];//字符缓冲数组
	int len = 0 , r = 0;
	
	memset(buffer,0,sizeof(buffer));//清空所有缓冲数组中的数据	

	if((fd = open(adc,O_RDWR|O_NOCTTY|O_NDELAY)) < 0)//打开adc设备
	{
		printf("open %s failed \n",adc);
		exit(1);
	}else{
		printf("open %s success \n ",adc);
		len = read(fd,buffer,10);//读取adc传送过来的数据
		if(len == 0)
		{
			printf("return null \n");		
		}else{
			r = atoi(buffer);
			r = (int)(r*10000/4096);//传递过来的数据为浮点型 ,进行转换和去分辨率4096
			printf("res value is %f \n",r);	
		}	
	}
	close(fd);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Stone_Xin_H_T/article/details/81437662
今日推荐