📄 int_1761.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 : int-1761.c// Revision: 1.0 // Author : Edwin // Date : 12/11/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 8 char *filename="/dev/comedi0";int subdevice = 15;//FAKE interrupt deviceint chan = 0;int range = 0;int aref = AREF_GROUND;int n_chan = 1;double freq = 1000;#define BUFSZ 16384//#define BUFSZ 2 char 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("************** int_1761.c****************\n"); printf("\n Comedi driver test program\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]; lsampl_t data[2]; comedi_insn insn; int total=0,n_channels; int ret, i; int go; int count[8]; 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); }#if 0 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 for (i=0;i<8;i++) count[i]=0; 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(1000); }else{ printf("ret < 0, ret=%d\n",ret); go = 0; perror("read"); } }else if(ret==0){ go = 0; printf("ret = 0!!\n"); }else{ int tempvalue; total+=ret; printf("ret = %d\n",ret/2);#if 1 for(i=0;i<ret/2;i++){ //printf("0x%x\t\t",((sampl_t *)buf)[i]&0x0fff); tempvalue = ((sampl_t *)buf)[i]&0x0fff; if (tempvalue & 0x01) count[0]++; if (tempvalue & 0x02) count[1]++; if (tempvalue & 0x04) count[2]++; if (tempvalue & 0x08) count[3]++; if (tempvalue & 0x10) count[4]++; if (tempvalue & 0x20) count[5]++; if (tempvalue & 0x40) count[6]++; if (tempvalue & 0x80) count[7]++; printf("\t\tcount0:%6d |count1%6d |count2%6d |count3%6d |count4%6d |count5%6d |count6%6d\|count7%6d\r", count[0],count[1],count[2],count[3],count[4],count[5],count[6],count[7]); } //go=0;#endif } memset(&insn,0,sizeof(insn)); memset(&data,0,sizeof(data)); n_channels = comedi_get_n_channels(dev, 2); insn.subdev = 2; insn.insn = INSN_BITS; insn.n = 2; insn.data = data; comedi_do_insn(dev, &insn); // printf("The Sample data is 0x%x:\n", data[1]); } 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_src = TRIG_EXT; 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; cmd->convert_src = TRIG_FOLLOW; cmd->convert_arg = 0; /* 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; cmd->stop_src = TRIG_NONE; cmd->stop_arg = 0; /* 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 + -