⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ai_insn.c

📁 安装comedi板卡驱动之后的测试程序
💻 C
字号:
//*************************************************************************//				//	         Copyright (c) 2001 IAG Software Team, ////		           Beijing R&D Center //	//		           Advantech Co., Ltd.////	      User program for Linux comedi device driver//// This is An Unpublished Work Containing Confidential  And Proprietary//    Information Which Is The Property Of Advantech  Automation Corp.//// Any Disclosure, Use, Or Reproduction, Without Written Authorization From//           Advantech Automation Corp., Is Strictly Prohibit.//////*************************************************************************// Program :    ai_insn.c// Revision:    1.0                                        // Author  :    jingang                                             // Date    :    09/20/01                                              //// Description:  Perform analog input of comedi driver with insn mode.                             // 		 This program reads N_SAMPLES samples from an analog//		 input each time and return the time it's used.//                                                  //-------------------------------------------------------------------------#include <stdio.h>		//standard input output library#include <stdlib.h>#include <comedilib.h>		//comedilib library#include <unistd.h>		//standard symbolic constants and types library#include <errno.h>		//error codes report library #include <getopt.h>		//argument analyse function library#include <sys/time.h>		//time and date library #include <string.h>		//string function library#define N_SAMPLE 1 		//number of data to be sampledchar *filename="/dev/comedi0";	//comedi device file namecomedi_t *device;		//comedi deviceint subdevice = 0;		//subdevice numberint channel = 3;		//channel index		int range = 0;			//range indexintmain(int argc, char *argv[]){        lsampl_t data[16];	//buffer used to save sample data        lsampl_t maxdata;		//the max data        int save_errno;			//error number        int ret, i;				comedi_insn insn[9];		//comedi insn task        comedi_insnlist il;		//insn list	comedi_range * srange;		//range information        int n_channels;			//channel index        int n_range;			//range index        struct timeval t1,t2;		//system time	printf("************** ai_insn.c****************\n");        printf("\n     Comedi driver test program\n\n");        printf("****************************************\n");                device = comedi_open(filename);        if(!device){                comedi_perror(filename);		return 1;        }                n_channels = comedi_get_n_channels(device, subdevice);        n_range = comedi_get_n_ranges(device, subdevice, channel);	for (i=0; i<16; i++)		data[i] = 0;	memset(&il,0,sizeof(il));        memset(insn,0,sizeof(insn));        	// Set up a the "instruction list", which is just a pointer	// to the array of instructions and the number of instructions.        il.n_insns = 9;        il.insns = insn;        	// Instruction 0: perform a gettimeofday()        insn[0].insn = INSN_GTOD;		        insn[0].n=2;        insn[0].data = (void *)&t1;        	// Instruction 1: do analog input reads	for(i=1; i<=7; i++) {	insn[i].subdev = subdevice;		        insn[i].insn = INSN_READ;        insn[i].n = 1;        insn[i].chanspec = CR_PACK(i-1, range, 0);        insn[i].data = &data[i-1];	} /*         insn[1].subdev = subdevice;        insn[1].insn = INSN_READ;        insn[1].n = 1;        insn[1].chanspec = CR_PACK(0, range, 0);        insn[1].data = &data[0];	insn[2].subdev = subdevice;        insn[2].insn = INSN_READ;        insn[2].n = 1;        insn[2].chanspec = CR_PACK(1, range, 0);        insn[2].data = &data[1];*/								// Instruction 2: perform a gettimeofday()        insn[8].insn = INSN_GTOD;		        insn[8].n=2;        insn[8].data = (void *)&t2;	while(1) {		printf("subdevice: %d\n", subdevice);#if 0			while(1) {			printf("Please key in the Channel number:");			gets(inputchar);			channel = atof(inputchar);			if(channel >= 0 && channel < n_channels)				break;			printf("Bad channel number!\n");		}#endif		//printf("channel = %d\n", channel);				for(i=0; i<7; i++)		insn[i+1].chanspec = CR_PACK(i, range, 0);		maxdata = comedi_get_maxdata(device, subdevice, channel);		srange = comedi_get_range(device, subdevice, channel, range);        			ret = comedi_do_insnlist(device,&il);	//sample data		save_errno = errno;	        if(ret<0){	        	comedi_perror(filename);			exit(0);        	}          //  	printf("\nrange: [%f,%f]\n", srange->min, srange->max);	//	printf("data = %d,  maxdata = %d\n", data[0], maxdata);		//print sample data in physic format			for(i=0; i<7; i++) {		printf("channel %d:  %d  %fV\n", i, data[i], comedi_to_phys(data[i], srange, maxdata));		}		//print the time used	//	printf("\ntime used: %ld us\navr time:  %ld us\n",          //      	(t2.tv_sec-t1.tv_sec)*1000000+(t2.tv_usec-t1.tv_usec),             //    	(t2.tv_sec-t1.tv_sec)*1000000+(t2.tv_usec-t1.tv_usec)/N_SAMPLE);                              //  printf("Press 'Q' to quit,  press other key to continue...\n");//                gets(&argument);  //              if(argument == 'Q' || argument == 'q')    //            	break;	}
	comedi_close(device);        return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -