新建通道,选择板卡以及输入输出信息
调用代码
//==============================================================================
//
// Title: Source File.c
// Purpose: A short description of the implementation.
//
// Created on: 2021/10/2 at 17:01:45 by Hong.
// Copyright: . All Rights Reserved.
//
//==============================================================================
//==============================================================================
// Include files
//#include "Source File.h"
#include <utility.h>
#include <userint.h>
#include <ansi_c.h>
#include <NIDAQmx.h>
#include <DAQTaskInProject.h>
#include <DAQTaskInProject1.h>
#include <math.h>
#include <time.h>
#include<stdio.h>
//==============================================================================
// Constants
void float2char(float,char*,int);
void mergelist(int* a,int len_a,int* b,int len_b,int* c);
//==============================================================================
// Types
//==============================================================================
// Static global variables
//==============================================================================
// Static functions
char buf[1000];
//static char buffer[100];
//static char BUFFER[40000];
static int signalpoints = 2000;//ADC读取点数
double SendWavebias[6000]={0};//送电压值初始矩阵
static int signalwritenumber;
static const int samplerate = 2000;
double readarray[8000]={0};//ADC采集数据数组
static int32 sampsPerChanRead = 0;
//static CmtTSQHandle tsqHdl; //线程安全队列句柄
//static CmtThreadPoolHandle gThreadPoolHandle;
//static int writemumber = 0;
int t = 0;
int delaytime=0.2;//设置DA输出保持时间
double *savebuffer;
double c[20000]= {0};
double xarray1[10000]= {0};
double yarray1[10000]= {0};
double zarray1[10000]= {0};
double xarray2[10000]= {0};
double yarray2[10000]= {0};
double zarray2[10000]= {0};
double xarray3[10000]= {0};
double yarray3[10000]= {0};
double zarray3[10000]= {0};
double xarray4[10000]= {0};
double yarray4[10000]= {0};
double zarray4[10000]= {0};
double xarray5[10000]= {0};
double yarray5[10000]= {0};
double zarray5[10000]= {0};
double xarray6[10000]= {0};
double yarray6[10000]= {0};
double zarray6[10000]= {0};
char Xallc[100] ;
double Xall1 = 0,Yall1 = 0,Zall1 = 0;
double Xall2 = 0,Yall2 = 0,Zall2 = 0;
double Xall3 = 0,Yall3 = 0,Zall3 = 0;
double Xall4 = 0,Yall4 = 0,Zall4 = 0;
double Xall5 = 0,Yall5 = 0,Zall5 = 0;
double Xall6 = 0,Yall6 = 0,Zall6 = 0;
double Xmean1 = 0,Ymean1 = 0 ,Zmean1 = 0;
double Xmean2 = 0,Ymean2 = 0 ,Zmean2 = 0;
double Xmean3 = 0,Ymean3 = 0 ,Zmean3 = 0;
double Xmean4 = 0,Ymean4 = 0 ,Zmean4 = 0;
double Xmean5 = 0,Ymean5 = 0 ,Zmean5 = 0;
double Xmean6 = 0,Ymean6 = 0 ,Zmean6 = 0;
TaskHandle taskHandlebias0=0;//outhhand
TaskHandle taskHandlebias1=0;//inhandle
//==============================================================================
// Global variables
//==============================================================================
// Global functions
/// HIFN What does your function do?
/// HIPAR x/What inputs does your function expect?
/// HIRET What does your function return?
int Define_Your_Functions_Here (int x)
{
return x;
}
int main (int argc, char *argv[])
{
//=============================================================OutPut1===================================================================================
for(int i = 0;i<2000;i++)
{
SendWavebias[i] = 0.5;
}
for(int i = 2000;i<4000;i++)
{
SendWavebias[i] = 0.5;
}
for(int i = 4000;i<6000;i++)
{
SendWavebias[i] = 0.5;
}
TaskHandle taskOut; //DA输出
DAQmxCreateTask(buf, &taskOut);
DAQmxCreateAOVoltageChan(taskOut, "cDAQ1Mod2/ao0","电压输出_0", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxCreateAOVoltageChan(taskOut, "cDAQ1Mod2/ao1","电压输出_1", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxCreateAOVoltageChan(taskOut, "cDAQ1Mod3/ao0","电压输出_2", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxSetChanAttribute(taskOut, "电压输出_0", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxSetChanAttribute(taskOut, "电压输出_1", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxSetChanAttribute(taskOut, "电压输出_2", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxCfgSampClkTiming(taskOut, "", 2000, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 2000);
DAQmxSetWriteAttribute(taskOut, DAQmx_Write_RegenMode, DAQmx_Val_AllowRegen);
DAQmxWriteAnalogF64 (taskOut, signalpoints, 0, 0, DAQmx_Val_GroupByChannel , SendWavebias, &signalwritenumber, NULL);
DAQmxStartTask (taskOut);
DAQmxClearTask (taskOut);
Delay(delaytime);
TaskHandle taskInbias; //AD输入
TaskHandle taskHandlebias1=0;
DAQmxCreateTask(buf, &taskInbias);
DAQmxCreateAIVoltageChan(taskInbias, "cDAQ1Mod1/ai0", "电压_0",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias, "cDAQ1Mod1/ai1", "电压_1",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias, "cDAQ1Mod1/ai2", "电压_2",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCfgSampClkTiming(taskInbias, "", samplerate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, samplerate*3);
taskHandlebias1 = taskInbias;
DAQmxStartTask (taskHandlebias1);
DAQmxReadAnalogF64(taskHandlebias1,samplerate,2.0,DAQmx_Val_GroupByScanNumber ,&readarray,samplerate*3,&sampsPerChanRead ,NULL);
for(int i = 0; i<sampsPerChanRead*3; i+=3)
{
xarray1[t] = (readarray[i]);
Xall1 += readarray[i];
yarray1[t] = (readarray[i + 1]);
Yall1 += (readarray[i + 1]);
zarray1[t] = (readarray[i + 2]);
Zall1 += (readarray[i + 2]);
t++;
}
if(t == samplerate )
{
Xmean1 = (Xall1/ sampsPerChanRead);
Ymean1 = (Yall1/ sampsPerChanRead);
Zmean1 = (Zall1/ sampsPerChanRead);
//InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,lineIndex,buf);
//lineIndex++;
Xall1=0;
Yall1=0;
Zall1=0;
t = 0;
}
Delay(0.02);
DAQmxStopTask (taskHandlebias1);
DAQmxClearTask (taskHandlebias1);
savebuffer=(double*)calloc(readarray,sizeof(double));
for(int i=0;i<6000;i++)
{
savebuffer[i]=readarray[i];
}
//=============================================================OutPut2===================================================================================
for(int i = 0;i<2000;i++)
{
SendWavebias[i] = 1;
}
TaskHandle taskOut1; //DA输出
DAQmxCreateTask(buf, &taskOut1);
DAQmxCreateAOVoltageChan(taskOut1, "cDAQ1Mod2/ao0","电压输出", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxSetChanAttribute(taskOut1, "电压输出", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxCfgSampClkTiming(taskOut1, "", 2000, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 2000);
DAQmxSetWriteAttribute(taskOut1, DAQmx_Write_RegenMode, DAQmx_Val_AllowRegen);
DAQmxWriteAnalogF64 (taskOut1, signalpoints, 0, 0, DAQmx_Val_GroupByChannel , SendWavebias, &signalwritenumber, NULL);
DAQmxStartTask (taskOut1);
DAQmxClearTask (taskOut1);
Delay(delaytime);
TaskHandle taskInbias2; //AD输入
TaskHandle taskHandlebias2=0;
DAQmxCreateTask(buf, &taskInbias2);
DAQmxCreateAIVoltageChan(taskInbias2, "cDAQ1Mod1/ai0", "电压_0",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias2, "cDAQ1Mod1/ai1", "电压_1",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias2, "cDAQ1Mod1/ai2", "电压_2",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCfgSampClkTiming(taskInbias2, "", samplerate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, samplerate*3);
taskHandlebias2 = taskInbias2;
DAQmxStartTask (taskHandlebias2);
DAQmxReadAnalogF64(taskHandlebias2,samplerate,2.0,DAQmx_Val_GroupByScanNumber ,readarray,samplerate*3,&sampsPerChanRead ,NULL);
for(int i = 0; i<sampsPerChanRead*3; i+=3)
{
xarray2[t] = (readarray[i]);
Xall2 += readarray[i];
yarray2[t] = (readarray[i + 1]);
Yall2 += (readarray[i + 1]);
zarray2[t] = (readarray[i + 2]);
Zall2 += (readarray[i + 2]);
t++;
}
if(t == samplerate )
{
Xmean2 = (Xall2/ sampsPerChanRead);
Ymean2 = (Yall2/ sampsPerChanRead);
Zmean2 = (Zall2/ sampsPerChanRead);
//InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,lineIndex,buf);
//lineIndex++;
Xall2=0;
Yall2=0;
Zall2=0;
t = 0;
}
Delay(0.02);
DAQmxStopTask (taskHandlebias2);
DAQmxClearTask (taskHandlebias2);
for(int i=0;i<6000;i++)
{
savebuffer[i+6000]=readarray[i];
}
//=============================================================OutPut3===================================================================================
for(int i = 0;i<2000;i++)
{
SendWavebias[i] = 1.5;
}
TaskHandle taskOut2; //DA输出
DAQmxCreateTask(buf, &taskOut2);
DAQmxCreateAOVoltageChan(taskOut2, "cDAQ1Mod2/ao0","电压输出", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxSetChanAttribute(taskOut2, "电压输出", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxCfgSampClkTiming(taskOut2, "", 2000, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 2000);
DAQmxSetWriteAttribute(taskOut2, DAQmx_Write_RegenMode, DAQmx_Val_AllowRegen);
DAQmxWriteAnalogF64 (taskOut2, signalpoints, 0, 0, DAQmx_Val_GroupByChannel , SendWavebias, &signalwritenumber, NULL);
DAQmxStartTask (taskOut2);
DAQmxClearTask (taskOut2);
Delay(delaytime);
TaskHandle taskInbias3; //AD输入
TaskHandle taskHandlebias3=0;
DAQmxCreateTask(buf, &taskInbias3);
DAQmxCreateAIVoltageChan(taskInbias3, "cDAQ1Mod1/ai0", "电压_0",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias3, "cDAQ1Mod1/ai1", "电压_1",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias3, "cDAQ1Mod1/ai2", "电压_2",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCfgSampClkTiming(taskInbias3, "", samplerate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, samplerate*3);
taskHandlebias3 = taskInbias3;
DAQmxStartTask (taskHandlebias3);
DAQmxReadAnalogF64(taskHandlebias3,samplerate,2.0,DAQmx_Val_GroupByScanNumber ,readarray,samplerate*3,&sampsPerChanRead ,NULL);
for(int i = 0; i<sampsPerChanRead*3; i+=3)
{
xarray3[t] = (readarray[i]);
Xall2 += readarray[i];
yarray3[t] = (readarray[i + 1]);
Yall2 += (readarray[i + 1]);
zarray3[t] = (readarray[i + 2]);
Zall2 += (readarray[i + 2]);
t++;
}
if(t == samplerate )
{
Xmean3 = (Xall3/ sampsPerChanRead);
Ymean3 = (Yall3/ sampsPerChanRead);
Zmean3 = (Zall3/ sampsPerChanRead);
//InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,lineIndex,buf);
//lineIndex++;
Xall3=0;
Yall3=0;
Zall3=0;
t = 0;
}
Delay(0.02);
DAQmxStopTask (taskHandlebias3);
DAQmxClearTask (taskHandlebias3);
for(int i=0;i<6000;i++)
{
savebuffer[i+12000]=readarray[i];
}
//===============================================================OutPut4=================================================================================
for(int i = 0;i<2000;i++)
{
SendWavebias[i] = 2;
}
TaskHandle taskOut3; //DA输出
DAQmxCreateTask(buf, &taskOut3);
DAQmxCreateAOVoltageChan(taskOut3, "cDAQ1Mod2/ao0","电压输出", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxSetChanAttribute(taskOut3, "电压输出", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxCfgSampClkTiming(taskOut3, "", 2000, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 2000);
DAQmxSetWriteAttribute(taskOut3, DAQmx_Write_RegenMode, DAQmx_Val_AllowRegen);
DAQmxWriteAnalogF64 (taskOut3, signalpoints, 0, 0, DAQmx_Val_GroupByChannel , SendWavebias, &signalwritenumber, NULL);
DAQmxStartTask (taskOut3);
DAQmxClearTask (taskOut3);
Delay(delaytime);
TaskHandle taskInbias4; //AD输入
TaskHandle taskHandlebias4=0;
DAQmxCreateTask(buf, &taskInbias4);
DAQmxCreateAIVoltageChan(taskInbias4, "cDAQ1Mod1/ai0", "电压_0",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias4, "cDAQ1Mod1/ai1", "电压_1",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias4, "cDAQ1Mod1/ai2", "电压_2",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCfgSampClkTiming(taskInbias4, "", samplerate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, samplerate*3);
taskHandlebias4 = taskInbias4;
DAQmxStartTask (taskHandlebias4);
DAQmxReadAnalogF64(taskHandlebias4,samplerate,2.0,DAQmx_Val_GroupByScanNumber ,readarray,samplerate*3,&sampsPerChanRead ,NULL);
for(int i = 0; i<sampsPerChanRead*3; i+=3)
{
xarray4[t] = (readarray[i]);
Xall4 += readarray[i];
yarray4[t] = (readarray[i + 1]);
Yall4 += (readarray[i + 1]);
zarray4[t] = (readarray[i + 2]);
Zall4 += (readarray[i + 2]);
t++;
}
if(t == samplerate )
{
Xmean4 = (Xall4/ sampsPerChanRead);
Ymean4 = (Yall4/ sampsPerChanRead);
Zmean4 = (Zall4/ sampsPerChanRead);
//InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,lineIndex,buf);
//lineIndex++;
Xall4=0;
Yall4=0;
Zall4=0;
t = 0;
}
Delay(0.02);
DAQmxStopTask (taskHandlebias4);
DAQmxClearTask (taskHandlebias4);
for(int i=0;i<6000;i++)
{
savebuffer[i+18000]=readarray[i];
}
//===============================================================OutPut5=================================================================================
for(int i = 0;i<2000;i++)
{
SendWavebias[i] = 2.5;
}
TaskHandle taskOut4; //DA输出
DAQmxCreateTask(buf, &taskOut4);
DAQmxCreateAOVoltageChan(taskOut4, "cDAQ1Mod2/ao0","电压输出", -4.24264068712, 4.24264068712,DAQmx_Val_Volts, "");
DAQmxSetChanAttribute(taskOut4, "电压输出", DAQmx_AO_TermCfg,DAQmx_Val_PseudoDiff);
DAQmxCfgSampClkTiming(taskOut4, "", 2000, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 2000);
DAQmxSetWriteAttribute(taskOut4, DAQmx_Write_RegenMode, DAQmx_Val_AllowRegen);
DAQmxWriteAnalogF64 (taskOut4, signalpoints, 0, 0, DAQmx_Val_GroupByChannel , SendWavebias, &signalwritenumber, NULL);
DAQmxStartTask (taskOut4);
DAQmxClearTask (taskOut4);
Delay(delaytime);
TaskHandle taskInbias5; //AD输入
TaskHandle taskHandlebias5=0;
DAQmxCreateTask(buf, &taskInbias5);
DAQmxCreateAIVoltageChan(taskInbias5, "cDAQ1Mod1/ai0", "电压_0",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias5, "cDAQ1Mod1/ai1", "电压_1",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCreateAIVoltageChan(taskInbias5, "cDAQ1Mod1/ai2", "电压_2",DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, "");
DAQmxCfgSampClkTiming(taskInbias5, "", samplerate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, samplerate*3);
taskHandlebias5 = taskInbias5;
DAQmxStartTask (taskHandlebias5);
DAQmxReadAnalogF64(taskHandlebias5,samplerate,2.0,DAQmx_Val_GroupByScanNumber ,readarray,samplerate*3,&sampsPerChanRead ,NULL);
for(int i = 0; i<sampsPerChanRead*3; i+=3)
{
xarray5[t] = (readarray[i]);
Xall5 += readarray[i];
yarray5[t] = (readarray[i + 1]);
Yall5 += (readarray[i + 1]);
zarray5[t] = (readarray[i + 2]);
Zall5 += (readarray[i + 2]);
t++;
}
if(t == samplerate )
{
Xmean5 = (Xall5/ sampsPerChanRead);
Ymean5 = (Yall5/ sampsPerChanRead);
Zmean5 = (Zall5/ sampsPerChanRead);
//InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,lineIndex,buf);
//lineIndex++;
Xall5=0;
Yall5=0;
Zall5=0;
t = 0;
}
Delay(0.02);
DAQmxStopTask (taskHandlebias5);
DAQmxClearTask (taskHandlebias5);
for(int i=0;i<6000;i++)
{
savebuffer[i+24000]=readarray[i];
}
//======================================================写文件================================================================================
char output_file[]="data_xxx.csv";
sprintf(output_file,"data_%3d.csv",1); //设置文件名
FILE* fd=fopen(output_file,"w"); //“写”打开文件
if(!fd){
perror("fopen failed!");
return 1;
}
for(int i=0;i<30000;i++)
{
fprintf(fd,"%f, ",savebuffer[i]); //输出数据+换格
fprintf(fd,"\n"); //换行
}
fprintf(fd,"\n");
fclose(fd); //关闭文件
free(savebuffer);
return 0;
}
void float2char(float slope,char*buffer,int n) //浮点型数,存储的字符数组,字符数组的长度
{
int temp,i,j;
if(slope>=0)//判断是否大于0
buffer[0] = '+';
else
{
buffer[0] = '-';
slope = -slope;
}
temp = (int)slope;//取整数部分
for(i=0;temp!=0;i++)//计算整数部分的位数
temp /=10;
temp =(int)slope;
for(j=i;j>0;j--)//将整数部分转换成字符串型
{
buffer[j] = temp%10+'0';
temp /=10;
}
buffer[i+1] = '.';
slope -=(int)slope;
for(i=i+2;i<n-1;i++)//将小数部分转换成字符串型
{
slope*=10;
buffer[i]=(int)slope+'0';
slope-=(int)slope;
}
buffer[n-1] = '\0';
}
void mergelist(int* a,int len_a,int* b,int len_b,int* c)
{
int i=0;//用来判断数组a
int j=0;//用来判断数组b
int k=0;//用来标识数组c
while(i<len_a&&j<len_b)//两个数组都为非空时
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
k++;
}else{
c[k]=b[j];
j++;
k++;
}
}
while(i<len_a)//数组b的值已经被移到c中,数组a的值还没有完全被移到c中
{
c[k]=a[i];
i++;
k++;
}
while(j<len_b)//数组a的值已经完全被移到c中,数组b的值还没有完全被移到c中
{
c[k]=b[j];
j++;
k++;
}
}
Copyright © 2021 by RichardYang. All rights reserved.
仅供参考,严禁转载,感谢。