1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>3.3. Your second Comedi program</title><link rel="stylesheet" type="text/css" href="comedilib.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="index.html" title="Comedi"><link rel="up" href="writingprograms.html" title="3. Writing Comedi programs"><link rel="prev" href="convertingsamples.html" title="3.2. Converting between integer data and physical units"><link rel="next" href="asyncprogram.html" title="3.4. Asynchronous acquisition"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.3.
Your second <acronym class="acronym">Comedi</acronym> program
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="convertingsamples.html">Prev</a> </td><th width="60%" align="center">3.
Writing <acronym class="acronym">Comedi</acronym> programs
</th><td width="20%" align="right"> <a accesskey="n" href="asyncprogram.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="secondprogram"></a>3.3.
Your second <a class="ulink" href="http://www.comedi.org" target="_top"><acronym class="acronym">Comedi</acronym></a> program
</h3></div></div></div><p>
Actually, this is the first <a class="ulink" href="http://www.comedi.org" target="_top"><acronym class="acronym">Comedi</acronym></a> program again, except
we've added code to convert the integer data value to physical units.
</p><pre class="programlisting">
/*
* Tutorial example #2
* Part of Comedilib
*
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
* Copyright (c) 2008 Frank Mori Hess <fmhess@users.sourceforge.net>
*
* This file may be freely modified, distributed, and combined with
* other software, as long as proper attribution is given in the
* source code.
*/
#include <stdio.h> /* for printf() */
#include <stdlib.h>
#include <comedilib.h>
#include <ctype.h>
#include <math.h>
int subdev = 0; /* change this to your input subdevice */
int chan = 0; /* change this to your channel */
int range = 0; /* more on this later */
int aref = AREF_GROUND; /* more on this later */
const char filename[] = "/dev/comedi0";
int main(int argc, char *argv[])
{
comedi_t *device;
lsampl_t data;
double physical_value;
int retval;
comedi_range *range_info;
lsampl_t maxdata;
device = comedi_open(filename);
if (device == NULL) {
comedi_perror(filename);
return 1;
}
retval = comedi_data_read(device, subdev, chan, range, aref,
&data);
if (retval < 0) {
comedi_perror(filename);
return 1;
}
comedi_set_global_oor_behavior(COMEDI_OOR_NAN);
range_info = comedi_get_range(device, subdev, chan, range);
maxdata = comedi_get_maxdata(device, subdev, chan);
printf("[0,%d] -> [%g,%g]\n", maxdata,
range_info->min, range_info->max);
physical_value = comedi_to_phys(data, range_info, maxdata);
if (isnan(physical_value)) {
printf("Out of range [%g,%g]",
range_info->min, range_info->max);
} else {
printf("%g", physical_value);
switch(range_info->unit) {
case UNIT_volt: printf(" V"); break;
case UNIT_mA: printf(" mA"); break;
case UNIT_none: break;
default: printf(" (unknown unit %d)",
range_info->unit);
}
printf(" (%lu in raw units)\n", (unsigned long)data);
}
return 0;
}
</pre><p>
The source code file for the above program can be found in
the Comedilib source at <code class="filename">demo/tut2.c</code> and if
installed as a package usually at
<code class="filename">/usr/share/doc/libcomedi-dev/demo/</code> with all the
other tutorial/demo files.
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="convertingsamples.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="writingprograms.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="asyncprogram.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.2.
Converting between integer data and physical units
</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.4.
Asynchronous acquisition
</td></tr></table></div></body></html>
|