Specifications

Comedi
9 / 148
{
comedi_t
*
it;
int chan = 0;
lsampl_t data;
int retval;
it = comedi_open("/dev/comedi0");
if(it == NULL)
{
comedi_perror("comedi_open");
return -1;
}
retval = comedi_data_read(it, subdev, chan, range, aref, &data);
if(retval < 0)
{
comedi_perror("comedi_data_read");
return -1;
}
printf("%d\n", data);
return 0;
}
The source code file for the above program can be found in Comedilib, at demo/tut1.c. You can compile the program using
cc tut1.c -lcomedi -o tut1
The (comedi_open ) call can only be successful if the comedi0 device file is configured with a valid Comedi driver. Section 2.1
explains how this driver is linked to the ‘device file’.
The range variable tells Comedi which gain to use when measuring an analog voltage. Since we don’t know (yet) which numbers
are valid, or what each means, we’ll use 0, because it won’t cause errors. Likewise with aref, which determines the analog
reference used.
3.2 Converting between integer data and physical units
If you selected an analog input subdevice, you probably noticed that the output of tut1 is an unsigned number, for example
between 0 and 65535 for a 16 bit analog input. Comedi samples are unsigned, with 0 representing the lowest voltage of the
ADC, and a hardware-dependent maximum value representing the highest voltage. Comedi compensates for anything else the
manual for your device says (for example, many boards represent bipolar analog input voltages as signed integers). However,
you probably prefer to have this number translated to a voltage. Naturally, as a good programmer, your first question is: ‘How
do I do this in a device-independent manner?’
The functions comedi_to_physical(), comedi_to_phys(), comedi_from_physical() and comedi_from_ph-
ys() are used to convert between Comedis integer data and floating point numbers corresponding to physical values (voltages,
etc.).
3.3 Your second Comedi program
Actually, this is the first Comedi program again, except we’ve added code to convert the integer data value to physical units.
/
*
*
Tutorial example #2
*
Part of Comedilib
*