2 * Example of using commands - asynchronous input
5 * Copyright (c) 1999,2000,2001 David A. Schleef <ds@schleef.org>
6 * 2008 Bernd Porr <berndporr@f2s.com>
8 * This file may be freely modified, distributed, and combined with
9 * other software, as long as proper attribution is given in the
14 * The program is used to test the usbdux sigma board
18 #include <comedilib.h>
26 extern comedi_t *device;
48 static unsigned int chanlist[N_CHANS];
49 static comedi_range * range_info[N_CHANS];
50 static lsampl_t maxdata[N_CHANS];
53 int prepare_cmd_lib(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd);
55 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
57 void print_datum(lsampl_t raw, int channel_index);
59 char *cmdtest_messages[]={
68 int main(int argc, char *argv[])
78 struct parsed_options options;
80 /* The following variables used in this demo
81 * can be modified by command line
82 * options. When modifying this demo, you may want to
83 * change them here. */
84 options.filename = "/dev/comedi0";
85 options.subdevice = 0;
88 options.aref = AREF_GROUND;
90 options.n_scan = 10000;
91 options.freq = 1000.0;
94 dev = comedi_open(options.filename);
96 comedi_perror(options.filename);
100 // Print numbers for clipped inputs
101 comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);
103 /* Set up channel list */
104 for(i = 0; i < options.n_chan; i++){
105 chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
106 range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range);
107 maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel);
110 /* prepare_cmd_lib() uses a Comedilib routine to find a
111 * good command for the device. prepare_cmd() explicitly
112 * creates a command, which may not work for your device. */
113 prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
115 /* comedi_command_test() tests a command to see if the
116 * trigger sources and arguments are valid for the subdevice.
117 * If a trigger source is invalid, it will be logically ANDed
118 * with valid values (trigger sources are actually bitmasks),
119 * which may or may not result in a valid trigger source.
120 * If an argument is invalid, it will be adjusted to the
121 * nearest valid value. In this way, for many commands, you
122 * can test it multiple times until it passes. Typically,
123 * if you can't get a valid command in two tests, the original
124 * command wasn't specified very well. */
125 ret = comedi_command_test(dev, cmd);
127 comedi_perror("comedi_command_test");
129 fprintf(stderr,"Ummm... this subdevice doesn't support commands\n");
133 ret = comedi_command_test(dev, cmd);
135 comedi_perror("comedi_command_test");
138 fprintf(stderr,"second test returned %d (%s)\n", ret,
139 cmdtest_messages[ret]);
141 fprintf(stderr, "Error preparing command\n");
145 /* start the command */
146 ret = comedi_command(dev, cmd);
148 comedi_perror("comedi_command");
151 subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice);
153 ret = read(comedi_fileno(dev),buf,BUFSZ);
155 /* some error occurred */
159 /* reached stop condition */
163 int bytes_per_sample;
165 if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total);
166 if(subdev_flags & SDF_LSAMPL)
167 bytes_per_sample = sizeof(lsampl_t);
169 bytes_per_sample = sizeof(sampl_t);
170 for(i = 0; i < ret / bytes_per_sample; i++){
171 if(subdev_flags & SDF_LSAMPL) {
172 raw = ((lsampl_t *)buf)[i];
174 raw = ((sampl_t *)buf)[i];
176 print_datum(raw, col);
178 if(col == options.n_chan){
189 * This prepares a command in a pretty generic way. We ask the
190 * library to create a stock command that supports periodic
191 * sampling of data, then modify the parts we want. */
192 int prepare_cmd_lib(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned scan_period_nanosec, comedi_cmd *cmd)
196 memset(cmd,0,sizeof(*cmd));
198 /* This comedilib function will get us a generic timed
199 * command for a particular board. If it returns -1,
201 ret = comedi_get_cmd_generic_timed(dev, subdevice, cmd, n_chan, scan_period_nanosec);
203 printf("comedi_get_cmd_generic_timed failed\n");
207 /* Modify parts of the command */
208 cmd->chanlist = chanlist;
209 cmd->chanlist_len = n_chan;
210 if(cmd->stop_src == TRIG_COUNT) cmd->stop_arg = n_scan;
215 void print_datum(lsampl_t raw, int channel_index) {
216 double physical_value;
217 physical_value = comedi_to_phys(raw, range_info[channel_index], maxdata[channel_index]);
218 printf("%#8.6g ",physical_value);