Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

NI USB 6361 C programming

Hello. I have NI USB 6361 board and I want to write C program (I use Embarcadero C++ Builder) for acquiring analog data from this board (a0 channel). I connect signal generator to ao channel.

I read documentation and examples for my board. My program allows to acquire data, but I have one problem.

 

I can see waveform, but values of data are differs from data, that I acquired from board with Measurement Studio. I acquired data and export this to Excel, and find min and max values. Signal acquired with Measurement Studio have stable values for max and min. But my program give me different values and I dont know why. 

 

Can anybody expect my C code and help me? I attached my code below. Excuse me for my Enlgish and thanks for help.

 

 

	float64		max,min,value;
	int32		points,written,error;
	char        errBuff[2048]={'\0'};
	char		*channel;
	wchar_t		*ch;
	int 		i=0;
	TVarRec 	args[1] = {0};
	max = StrToFloat(EditAOMaxValue->Text);
	if (max>10.0) {
		max = 10.0;
		EditAOMaxValue->Text = "10,0";
	}
	min = StrToFloat(EditAOMinValue->Text);
	if (min<-10.0) {
		min = -10.0;
		EditAOMinValue->Text = "-10,0";
	}

	value = StrToFloat(EditAOValue->Text);
	if (value>10.0) value = 10.0;
	if (value<-10.0) value = -10.0;
	args[0] = value;
	EditAOValue->Text = Format("%1.1f",args,0);

	points = StrToInt(EditAOPoints->Text);
	for(;i<points;i++)
		data[i] = value;
		//5.0*(double)i/points;
	ch =  ("Dev1/"+ ComboBoxAOChan->Items->Strings[ComboBoxAOChan->ItemIndex]).t_str();
	channel = AnsiString(ch).c_str();

	// DAQmx Configure Code
	DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
	DAQmxErrChk (DAQmxCreateAOVoltageChan(taskHandle,channel,"",min,max,DAQmx_Val_Volts,NULL));
	DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",1000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,points));
	// DAQmx Write Code
	DAQmxErrChk (DAQmxWriteAnalogF64(taskHandle,points,0,10.0,DAQmx_Val_GroupByChannel,data,&written,NULL));
	// DAQmx Start Code
	DAQmxErrChk (DAQmxStartTask(taskHandle));
	// DAQmx Wait Code
	DAQmxErrChk (DAQmxWaitUntilTaskDone(taskHandle,10.0));
	args[0] = written;
	LblStatus->Caption = Sysutils::Format("Status: written %d points\n",args,0);
Error:
	if( taskHandle!=0 ) {
		// DAQmx Stop Code
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) ) {
		DAQmxGetExtendedErrorInfo(errBuff,2048);
		args[0] = errBuff;
		ShowMessage(Sysutils::Format("DAQmx Error: %s\n",args,0));
	}

 

 

 

 

0 Kudos
Message 1 of 8
(4,740 Views)

Hello Skyer,

 

Just so that I understand, does the exact some code give you different results between the two development environments?

 

Also, this code appears to only output values. How are you reading these values back in?

--------------------------------------
0 Kudos
Message 2 of 8
(4,703 Views)

Hello OlliesCamel,

 

I'm sorry, but I posted the wrong code. Below my code for acquiring analog data in finite mode. 

 

	float64		max,min;
	int32		points,mode,read,error;
	char        errBuff[2048]={'\0'};
	char*		channel;
	wchar_t		*ch;
	int 		i=0;
	TVarRec 	args[1] = {0};
	char*		sample;
	char*		buf;

	fprintf(LogFile,"Calibtrating...\n");
	max = StrToFloat(EditAIMaxValue->Text);
	if (max>10.0) {
		max = 10.0;
		EditAIMaxValue->Text = "10,0";
	}
	min = StrToFloat(EditAIMinValue->Text);
	if (min<-10.0) {
		min = -10.0;
		EditAIMinValue->Text = "-10,0";
	}
	switch (ComboBoxAIMode->ItemIndex) {
		case 0:	mode = DAQmx_Val_Diff;
		case 1:  mode = DAQmx_Val_RSE;
		case 2:  mode = DAQmx_Val_NRSE;
		default: mode = DAQmx_Val_Cfg_Default;
	}
	points = StrToInt(EditAIPoints->Text);
	ch =  ("Dev1/"+ ComboBoxAIChan->Items->Strings[ComboBoxAIChan->ItemIndex]).t_str();
	channel = AnsiString(ch).c_str();

	// DAQmx Configure Code
	DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
	DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,channel,"",mode ,min,max,DAQmx_Val_Volts,NULL));
	DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",100000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,points));
	// DAQmx Start Code
	DAQmxErrChk (DAQmxStartTask(taskHandle));
	// DAQmx Read Code
	DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,points,10.0,DAQmx_Val_GroupByChannel,data,points,&read,NULL));

	max = -30.0;
	min = 30.0;
	args[0] = read;
	LblStatus->Caption = Sysutils::Format("Status: read %d points\n",args,0);

	for (i = 0; i < read; i++) {
		args[0] = data[i];
		Memo1->Lines->Add(IntToStr(i)+"  "+Format("%2.4f",args,0));

		sample = StrFmt(buf,"%2.4f \n",args,0);
		Series1->AddXY(i,data[i],"",clBlue);
		if (data[i]>max) { max = data[i];}
		if (data[i]<min) { min = data[i];}
	}
	args[0] = max;
	LblMaxValue->Caption = "Max value = " + Format("%2.4f",args,0)+ " V";
	args[0] = min;
	LblMinValue->Caption = "Min value = " + Format("%2.4f",args,0)+ " V";
Error:
	if( taskHandle!=0 ) {
		// DAQmx Stop Code
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) ) {
		DAQmxGetExtendedErrorInfo(errBuff,2048);
		args[0] = errBuff;
		ShowMessage(Sysutils::Format("DAQmx Error: %s\n",args,0));
	}
	fclose (LogFile);

 

0 Kudos
Message 3 of 8
(4,690 Views)

How do you know that the Measurement Studio values are the correct values?

 

Which language are you using with Measurement Studio?

0 Kudos
Message 4 of 8
(4,669 Views)

Skyer,

 

Just so that I understand, does the exact some code give you different results between the two development environments?

--------------------------------------
0 Kudos
Message 5 of 8
(4,663 Views)

1) No, I use single development enviroment - Embarcadero RAD Studio C++ Builder.

 

2) I connected waveform generator to board. And I know values from generator. Also I use oscilloscope to check the signal.

0 Kudos
Message 6 of 8
(4,626 Views)

Maybe I'm missing something, but how are you using Measurement Studio, if you aren't using the Visual Studio IDE? Are we talking about the same product?: http://www.ni.com/mstudio/

--------------------------------------
0 Kudos
Message 7 of 8
(4,599 Views)

@Skyer wrote:

1) No, I use single development enviroment - Embarcadero RAD Studio C++ Builder.

 

2) I connected waveform generator to board. And I know values from generator. Also I use oscilloscope to check the signal.


You mentioned Measurement Studio in your first post.

 

Yet you now say you are using Embarcadero RAD Studio C++ Builder.

 

Which is right?

 

It seems you are confused.

0 Kudos
Message 8 of 8
(4,588 Views)