📄 ao_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 : ao_cmd.c// Revision: 1.0 // Author : jingang, Edwin // Date : 09/20/01 //// Description: Perform analog output of comedi driver with cmd mode. // //-------------------------------------------------------------------------#include <stdio.h>#include <comedilib.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <getopt.h>#include <ctype.h>#include <string.h>#include<math.h>#define N_SCANS 10 #define N_CHANS 16char *filename="/dev/comedi0";int subdevice = 1;int chan=0;int range = 0;int aref = AREF_GROUND;int n_chan = 1;double freq = 1000;#define BUFSZ (1024*32) //unsigned short buf[BUFSZ];sampl_t buf[BUFSZ];sampl_t capdata[1000];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; unsigned long i,amp=0; int type; dev = comedi_open(filename); if(!dev){ perror(filename); exit(1); } type=comedi_get_subdevice_type(dev,subdevice); if(type != COMEDI_SUBD_AO) printf("type=%d\n",type); fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK); for(i=0; i<1000; i++) capdata[i] = 2000; for(i=0; i<32*1024; i++) {// if(i %5000000)// amp=3000-amp;// buf[i]=amp; buf[i]=2048*sin(i*3.1415926/2048);// buf[i]=0; } prepare_cmd_1(dev,&cmd); //printf("The address of cmd is(test):0x%x",cmd); do_cmd(dev,&cmd); sleep(5); comedi_cancel(dev,subdevice); 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; int loopcount=0; //int go; printf("buf[0]=%d,buf[1]=%d,sizeof(buf[0])=%d\n",buf[0],buf[1],sizeof(buf[0])); ret=write(comedi_fileno(dev),buf,BUFSZ); if(ret<0) { perror("write"); exit(0); } ret=comedi_command(dev,cmd); if(ret<0){ printf("errno=%d\n",errno); comedi_perror("comedi_command"); printf("has error\n"); return; } /* printf("fileno:%d\n",comedi_fileno(dev)); while(loopcount<100) { //ret=write(comedi_fileno(dev),buf,BUFSZ); if(ret<0){// printf("red=%d\n",ret); perror("write");// comedi_perror("write");// printf("Device write error!\n"); } loopcount++; } 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 = 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 = 3; /*The scan_begin_arg is the mode of D/A function. *1=ch0 continuous output.ch1 single Value operation mode *2=ch1 continuous output.ch0 single value operation mode *3=ch0 and ch1 both continuous output mode */ 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 = 10000000; /* 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; //printf("The data (0 and 1) is:%d and %d\n",*((sampl_t *)cmd->data),*(((sampl_t *)cmd->data)+1)); cmd->data_len = 10000*sizeof(sampl_t); //cmd->data printf("chanlist %d\n", chanlist[1]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -