5
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
7
* This file may be freely modified, distributed, and combined with
8
* other software, as long as proper attribution is given in the
12
This example shows how to use instructions, i.e., comedi_insns.
14
Using instructions directly, as in this example, is not recommended
15
for the beginner. Use the higher-level functions such as
16
comedi_data_read(), comedi_data_write(), etc., as demonstrated
17
in the inp, outp, and dio examples. Then, if you need the
18
additional flexibility that using instructions directly
19
provides, study this example and the implementations of
20
comedi_data_read(), etc.
24
#include <comedilib.h>
34
* This example does 3 instructions in one system call. It does
35
* a gettimeofday() call, then reads N_SAMPLES samples from an
36
* analog input, and the another gettimeofday() call.
39
#define MAX_SAMPLES 128
43
int main(int argc, char *argv[])
49
lsampl_t data[MAX_SAMPLES];
51
n_scan = 10; /* override default n_scan value to something more suitable */
52
parse_options(argc,argv);
53
if( n_scan > MAX_SAMPLES ){
54
fprintf( stderr, "Requested too many samples, reducing to %i\n", MAX_SAMPLES );
58
device=comedi_open(filename);
60
comedi_perror(filename);
65
printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n",
66
filename,subdevice,channel,range,aref);
69
/* Set up a the "instruction list", which is just a pointer
70
* to the array of instructions and the number of instructions.
75
/* Instruction 0: perform a gettimeofday() */
76
insn[0].insn=INSN_GTOD;
78
insn[0].data=(void *)&t1;
80
/* Instruction 1: do 10 analog input reads */
81
insn[1].insn=INSN_READ;
84
insn[1].subdev=subdevice;
85
insn[1].chanspec=CR_PACK(channel,range,aref);
87
/* Instruction 2: perform a gettimeofday() */
88
insn[2].insn=INSN_GTOD;
90
insn[2].data=(void *)&t2;
92
ret=comedi_do_insnlist(device,&il);
94
comedi_perror(filename);
98
printf("initial time: %ld.%06ld\n",t1.tv_sec,t1.tv_usec);
99
for(i=0;i<n_scan;i++){
100
printf("%d\n",data[i]);
102
printf("final time: %ld.%06ld\n",t2.tv_sec,t2.tv_usec);
104
printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec)*1000000+
105
(t2.tv_usec-t1.tv_usec));