Specifications

Comedi
11 / 148
}
return 0;
}
The source code file for the above program can be found in the Comedilib source at demo/tut2.c and if installed as a package
usually at /usr/share/doc/libcomedi-dev/demo/ with all the other tutorial/demo files.
3.4 Asynchronous acquisition
Of special importance is the so called "asynchronous data acquisition" where Comedi is sampling in the background at a given
sample rate. The user can retrieve the data whenever it is convenient. Comedi stores the data in a ring-buffer so that programs can
perform other tasks in the foreground, for example plotting data or interacting with the user. This technique is used in programs
such as ktimetrace or comedirecord.
There are two different ways how a sequence of channels is measured during asynchronous acquisition (see also the Figure in
the introduction):
The channels are measured with the help of a multiplexer which switches to the next channel after each measurement. This
means that the sampling rate is divided by the number of channels.
The channels are all measured at the same time, for example when every channel has its own converter. In this case the
sampling rate need not to be divided by the number of channels.
How your Comedi device handles the asynchronous acquisition can be found out with the command comedi_board_info -v.
The program demo/tut3.c demonstrates the asynchronous acquisition. The general strategy is always the same: first, we tell
Comedi all sampling parameters such as the sampling rate, the number of channels and anything it needs to know so that it can
run independently in the background. Then Comedi checks our request and it might modify it. For example we might want to
have a sampling rate of 16kHz but we only get 1kHz. Finally we can start the asynchronous acquisition. Once it has been started
we need to check periodically if data is available and request it from Comedi so that its internal buffer won’t overrun.
In summary the asynchonous acquisition is performed in the following way:
Create a command structure of type comedi_cmd
Call the function comedi_get_cmd_generic_timed() to fill the command structure with your comedi device, subdevice,
sampling rate and number of channels.
Create a channel-list and store it in the command structure. This tells comedi which channels should be sampled in the
background.
Call comedi_command_test() with your command structure. Comedi might modify your requested sampling rate and
channels.
Call comedi_command_test() again which now should return zero for success.
Call comedi_command() to start the asynchronous acquisition. From now on the kernel ringbuffer will be filled at the
specified sampling rate.
Call periodically the standard function read() and receive the data. The result should always be non zero as long as the
acquisition is running.
Convert the received data either into lsampl_t or sampl_t depending on the subdevice flag SDF_LSAMPL.
Poll for data with read() as long as it returns a positive result or until the program terminates.
The program below is a stripped down version of the program cmd.c in the demo directory. To compile it run:
gcc tut3.c -lcomedi -lm -o tut3