📄 ai_cmd.c
字号:
//*************************************************************************
//
// Copyright (c) 2001 IAG Software Team,
//
// Beijing R&D Center
//
// Advantech Co., Ltd.
//
// User program for Linux comedi device driver
//
// This is An Unpublished Work Containing Confidential And Proprietary
// Information Which Is The Property Of Advantech Automation Corp.
//
// Any Disclosure, Use, Or Reproduction, Without Written Authorization From
// Advantech Automation Corp., Is Strictly Prohibit.
//
//
//*************************************************************************
// Program : ai_cmd.c
// Revision: 1.0
// Author : Edwin
// Date : 10/17/02
//
// Description: Perform analog input of comedi driver with cmd mode.
//
//-------------------------------------------------------------------------
#include <stdio.h>#include <stdlib.h>#include <comedilib.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <getopt.h>#include <ctype.h>#include <string.h>#define N_SCANS 20 #define N_CHANS 16char *filename="/dev/comedi0";int subdevice = 0;int chan=0;int range = 0;int aref = AREF_GROUND;int n_chan = 2;double freq = 1000;#define BUFSZ 10000char buf[BUFSZ];sampl_t capdata[50000];unsigned int chanlist[N_CHANS];void prepare_cmd_1(comedi_t *dev,comedi_cmd *cmd);void do_cmd(comedi_t *dev,comedi_cmd *cmd);void dump_cmd(comedi_cmd *cmd);int main(int argc, char *argv[]){ comedi_t *dev; comedi_cmd cmd; int i; printf("************** ai_cmd.c****************\n"); printf("\n Comedi driver test program\n\n"); printf("****************************************\n"); dev = comedi_open(filename); if(!dev){ perror(filename); exit(1); }// fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK); for(i=0; i<50; i++) capdata[i] = 10; prepare_cmd_1(dev,&cmd); do_cmd(dev,&cmd); comedi_cancel(dev,subdevice);
comedi_close(dev); return 0;}void do_cmd(comedi_t *dev,comedi_cmd *cmd){ comedi_range *srange[N_CHANS]; lsampl_t maxdata[N_CHANS]; int total=0; int ret, i; int go; ret=comedi_command(dev,cmd); if(ret<0){ printf("errno=%d\n",errno); comedi_perror("comedi_command"); printf("has error\n"); return; } for(i=0; i<n_chan; i++) { srange[i] = comedi_get_range(dev, subdevice, CR_CHAN(chanlist[i]), range); maxdata[i] = comedi_get_maxdata(dev, subdevice, i); } //printf("");#if 1 for(i=0; i<n_chan; i++) printf("Chan %2d\t\t\t|\t", CR_CHAN(chanlist[i])); printf("\n"); for(i=0; i<n_chan; i++) printf("Raw data Physical value|\t "); printf("\n");#endif go=1; while(go){ ret=read(comedi_fileno(dev),buf,BUFSZ); if(ret<0){ if(errno==EAGAIN){ printf("Read file buffer errno =EADAIN!\n"); usleep(10000); // go--; }else{ printf("ret <0\n"); go = 0; perror("read"); } }else if(ret==0){ go = 0; }else{ static int col = 0; int i; printf("Read %d samples.\n",ret); total+=ret;#if 1 for(i=0;i<ret/2;i++){ printf("0x%x\t\t",((sampl_t *)buf)[i]&0x0fff); printf("%f |\t ",(comedi_to_phys((float)(((sampl_t *)buf)[i]&0x0fff), srange[col], maxdata[col]+1))); col++; if(col==n_chan){ printf("\n"); col=0; } } go=0;#endif } } printf("\n");}/* * This part of the demo measures channels 1, 2, 3, 4 at a rate of * 10 khz, with the inter-sample time at 10 us (100 khz). The number * of scans measured is 10. This is analogous to the old mode2 * acquisition. */void prepare_cmd_1(comedi_t *dev,comedi_cmd *cmd){ memset(cmd,0,sizeof(cmd)); /* the subdevice that the command is sent to */ cmd->subdev = subdevice; /* flags */ cmd->flags =0;// TRIG_WAKE_EOS; //cmd.flags = 0; /* each event requires a trigger, which is specified by a source and an argument. For example, to specify an external digital line 3 as a source, you would use src=TRIG_EXT and arg=3. */ /* In this case, we specify using TRIG_NOW to start * acquisition immediately when the command is issued. * The argument of TRIG_NOW is "number of nsec after * NOW", but no driver supports it yet. Also, no driver * currently supports using a start_src other than * TRIG_NOW. */ cmd->start_src = TRIG_NOW; cmd->start_arg = 0; /*The scan_begin_arg is the acquisition mode of the A/D or in D/A. */ cmd->scan_begin_src = TRIG_FOLLOW; cmd->scan_begin_arg = 0; /* The timing between each sample in a scan is controlled * by convert. Like above, TRIG_TIMER specifies that * convert events occur periodically at a rate of convert_arg * nanoseconds between scans. */ cmd->convert_src = TRIG_TIMER; cmd->convert_arg = 100000; /* The end of each scan is almost always specified using * TRIG_COUNT, with the argument being the same as the * number of channels in the chanlist. You could probably * find a device that allows something else, but it would * be strange. */ cmd->scan_end_src = TRIG_COUNT; cmd->scan_end_arg = n_chan; /* number of channels */ /* The end of acquisition is controlled by stop_src and * stop_arg. The src will typically be TRIG_COUNT or * TRIG_NONE. Specifying TRIG_COUNT will stop acquisition * after stop_arg number of scans, or TRIG_NONE will * cause acquisition to continue until stopped using * comedi_cancel(). */ cmd->stop_src = TRIG_COUNT; cmd->stop_arg = N_SCANS; /* the channel list determined which channels are sampled. In general, chanlist_len is the same as scan_end_arg. Most boards require this. */ cmd->chanlist = chanlist; cmd->chanlist_len = n_chan; cmd->data = capdata; cmd->data_len = 30*sizeof(sampl_t); chanlist[0]=CR_PACK(chan+0,range,aref); chanlist[1]=CR_PACK(chan+1,range,aref); chanlist[2]=CR_PACK(chan+2,range,aref); chanlist[3]=CR_PACK(chan+3,range,aref); printf("chanlist %d\n", chanlist[1]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -