Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

C - Write array to file

Hi. I'm getting pretty far with my code now to read an analog voltage input, display it and then save the data to a file. I've hit an issue though, as soon as it gets to my writting code block, it comes up with 

VC_Acq_IntClk.exe has triggered a breakpoint.

 When I click continue, it pumps a load of garbage to the specified text document name (May not be garbage, but it looks like that to me. For example

- Visual Studio Error:

Unhandled exception at 0x7728A208 (ntdll.dll) in VC_Acq_IntClk.exe: 0xC0000005: Access violation reading location 0x658B26FD.

 File data:

*Huú…$@WêÄ/$@
WêÄ/$@WêÄ/$@
WêÄ/$@ÄϯßZ$@
ÄϯßZ$@*Huú…$@
*Huú…$@í&‹e2$@
í&‹e2$@GÀ:±$@
GÀ:±$@GÀ:±$@
GÀ:±$@8 0Ü$@
8 0Ü$@8 0Ü$@
8 0Ü$@êP€]$@

 This is the code:

 

 

/*********************************************************************
*
* ANSI C Example program:
*    Acq-IntClk.c
*
* Example Category:
*    AI
*
* Description:
*    This example demonstrates how to acquire a finite amount of data
*    using the DAQ device's internal clock.
*
* Instructions for Running:
*    1. Select the physical channel to correspond to where your
*       signal is input on the DAQ device.
*    2. Enter the minimum and maximum voltages.
*    Note: For better accuracy try to match the input range to the
*          expected voltage level of the measured signal.
*    3. Select the number of samples to acquire.
*    4. Set the rate of the acquisition.
*    Note: The rate should be AT LEAST twice as fast as the maximum
*          frequency component of the signal being acquired.
*
* Steps:
*    1. Create a task.
*    2. Create an analog input voltage channel.
*    3. Set the rate for the sample clock. Additionally, define the
*       sample mode to be finite and set the number of samples to be
*       acquired per channel.
*    4. Call the Start function to start the acquisition.
*    5. Read all of the waveform data.
*    6. Call the Clear Task function to clear the task.
*    7. Display an error if any.
*
* I/O Connections Overview:
*    Make sure your signal input terminal matches the Physical
*    Channel I/O Control. For further connection information, refer
*    to your hardware reference manual.
*
*********************************************************************/

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


#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
FILE *save_file = NULL;

int main(void)
{
	int32       error=0;
	int32		counter[2]={'0'};
	TaskHandle  taskHandle=0;
	int32       read=0;
	int32		amount=0;	
	float64    *i_array=0;
	int			j=0;
	char        errBuff[2048]={'\0'};
	char		c = 64;
	char		filename[256];

	/*********************************************/
	// DAQmx Configure Code
	/*********************************************/

	printf("Please enter the amount of voltage checks you wish to run.\n");

	while(scanf_s("%d%c", &amount, &c) !=2) // This is where the user inputs his data. If it isn't a number, it returns false and goes into the while loop.
	{
		getchar();
		puts("Please enter a number.");
	}

	i_array = malloc(j * sizeof *i_array);

	for (j = 0; j < amount; j++) // Loops through the specified amount
	{
		DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
		DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,1.0,10.0,DAQmx_Val_Volts,NULL));
		DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));

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

		/*********************************************/
		// DAQmx Read Code
		/*********************************************/

		DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByChannel,i_array,1000,&read,NULL));

		printf("Updating... ");
		printf("Data point %d has %f",j, i_array[j]); // Displays the voltage set number of times
		printf(" volts\n");
		Sleep(200); // ms pause between each reading

		if( taskHandle!=0 )
		{
			/*********************************************/
			// DAQmx Stop Code
			/*********************************************/
			DAQmxStopTask(taskHandle);
			DAQmxClearTask(taskHandle);
		}
	}

	if(j==amount)
	{
		printf("\nVoltage readings aquired. Please specify a filename so the data can be saved.\n");
		goto Save;
	}


Save:
	printf("\nPlease enter a filename...\n");
	gets (filename);
	printf("Name is: ");
	puts(filename);
	save_file = fopen(filename,"w+");
	printf("File made\n");
		for (j = 0; j < amount; j++)
		{			
			fwrite(&i_array[j], sizeof(j), sizeof(amount), save_file);
			fputc('\n',save_file);
			printf("Data point %d read",j, i_array[amount]); // Displays the voltage set number of times
			printf("\n");
		}
	printf("Saving...\nSaved.");
	free(i_array);
Error:
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);	

	if( DAQmxFailed(error) )				
		printf("DAQmx Error: %s\n",errBuff);

	return 0;
}

 Any ideas what is going on here? The file is being made and the data is being inputted, but it just seems to be inputting a garbage loads of ASCII characters instead of the voltage readings.

 

0 Kudos
Message 1 of 4
(3,333 Views)

Ok, i think I may have made some head way. I just printed out the pointer i_array[amount] and it printed out a memory location. Is my thinking correct that it is trying to print some memory location to the file? But once it tried to do it the memory location has been overwriten so instead it prints out a load of crap?

 

 

0 Kudos
Message 2 of 4
(3,316 Views)

Can anyone help with this?

0 Kudos
Message 3 of 4
(3,264 Views)

Dear IMLJohn,

 

Could you place breakpoints down on your code to isolate the line that the error is occurring at and post it here? 

 

Kind Regards,

 

 

Sinead B.
Applications Engineer
National Instruments
0 Kudos
Message 4 of 4
(3,251 Views)