From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

export data from NI 9234 into txt file using ASNI C

Hi there,

 

I just started learning how to control the NI9234 to acquire sound/vibration data.

 

Based on NI's sample ANSI code, I could control the NI 9234 to read the analog input from the channel connected with PCB microphone. However, if I saved it into a txt file and plotted the txt data in Matlab, the figure looked like random noises other than sound signal (human voices). Code with which I am using, and figure of acquired txt data are shown as below.

 

May anyone tell me if the code is correct? If yes, what happened to the txt data?

 

Many thanks in advance for your time and help. Waiting for the reply.

 

Environment: Visual studio express 2008, win 7 (64 bits)

 

Screen Shot 2012-04-11 at 2.56.44 PM.png

 

Figure 1. the whole sound data acquired from NI 9234 (sampling rate 25600 Hz, with time length of 10 seconds)

 

Screen Shot 2012-04-11 at 2.57.13 PM.png

 

Figure 2 Zoomin of Figure 1.

 

Code with which I am using

 

/*********************************************************************
*
* ANSI C Example program:
*    ContAcqSndPressSamps-IntClk.c

*********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <NIDAQmx.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);

int main(void)
{
	int32       error=0;
	TaskHandle  taskHandle=0;
	char        errBuff[2048]={'\0'};

	/*********************************************/
	// DAQmx Configure Code
	/*********************************************/
	// Note:  DSA devices now support including channels from multiple 
	// devices in a single task. DAQmx automatically synchronizes the 
	// devices in such a task.  See the DAQmx Help >> Device Considerations >>
	// Multi Device Tasks section for further details.
	DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
	DAQmxErrChk (DAQmxCreateAIMicrophoneChan(taskHandle,"cDAQ1Mod1/ai1","",DAQmx_Val_PseudoDiff,DAQmx_Val_Pascals,10,120.0,DAQmx_Val_Internal,0.002,NULL));
	DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",25600.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,256000));
	

	DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,256000,0,EveryNCallback,NULL));
	DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));

	/*********************************************/
	// DAQmx Start Code
	/*********************************************/
	DAQmxErrChk (DAQmxStartTask(taskHandle));

	printf("Acquiring samples continuously. Press Enter to interrupt\n");
	getchar();

Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( taskHandle!=0 ) {
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
	}
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	getchar();
	return 0;
}

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
	int32       error=0;
	char        errBuff[2048]={'\0'};
	static int  totalRead=0;
	int32       read=0;
//	float64     data[256000];
	float64		*data, *line;
	
	FILE *fp;
	int i;
	data = (float64*)malloc(nSamples * sizeof(float64));
	line = (float64*)malloc(nSamples * sizeof(float64));

	/*********************************************/
	// DAQmx Read Code
	/*********************************************/
	DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,256000,10.0,DAQmx_Val_GroupByScanNumber,data,256000,&read,NULL));
	if( read>0 ) 
	{
		printf("Acquired %d samples. Total %d\r",read,totalRead+=read);

		fp = fopen("result.txt", "w");
		if(!fp)
		{
			printf("create and open file failed\n");
			return;
		}else
		{
			printf("file opened successfully\n");
		}
		 
		for (i=0;i<256000;i++) 
		{ 
				sprintf(&line[0],"%d\n",data[i]); 
				fputs(line, fp);			
		}
		fclose(fp);
		printf("file saved successfully\n");
		fflush(stdout);
	}
	free(data);
Error:
	if( DAQmxFailed(error) ) {
		DAQmxGetExtendedErrorInfo(errBuff,2048);
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandle);
		DAQmxClearTask(taskHandle);
		printf("DAQmx Error: %s\n",errBuff);
	}
	return 0;
}

int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
	int32   error=0;
	char    errBuff[2048]={'\0'};

	// Check to see if an error stopped the task.
	DAQmxErrChk (status);

Error:
	if( DAQmxFailed(error) ) {
		DAQmxGetExtendedErrorInfo(errBuff,2048);
		DAQmxClearTask(taskHandle);
		printf("DAQmx Error: %s\n",errBuff);
	}
	return 0;
}

 

0 Kudos
Message 1 of 2
(5,381 Views)

Problem solved.

 

It's my own stupid mistake during the saving session. It should be

sprintf(&line[0],"%f\n",data[i]); 

 instead of

sprintf(&line[0],"%d\n",data[i]); 

 

cheers

0 Kudos
Message 2 of 2
(5,367 Views)