[ create a new paste ] login | about

Link: http://codepad.org/rNTt12V2    [ raw code | output | fork ]

C, pasted on Sep 3:
#include <stdio.h>
#include <math.h>
#include<NIDAQmx.h>

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


int32 CVICALLBACK EveryNCallback1(TaskHandle taskHandleTemperture, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 CVICALLBACK DoneCallback1(TaskHandle taskHandleTemperture, int32 status, void *callbackData);

float64 transferVoltage(float64);

FILE *fp, *fp3;  //temperture
FILE *fp1; //voltage
FILE *fp2; //Current

TaskHandle	taskHandleVoltage = 0;
float64     dataV[1] = { 0.3 };
float64     dataV1[1] = { 0 };

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


	fp = fopen("E:\\Temperturemeasure1.txt", "w");
	fp1 = fopen("E:\\Voltagemeasure.txt", "w");
	//fp2 = fopen("E:\\Currentmeasure.txt", "w");
	//fp3 = fopen("E:\\Temperturemeasure2.txt", "w");


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




	//溫度量測 Temperture Measure (ai0)  (ai3)
	//電壓量測 voltage Measure (ai1)
	//電流量測 Current Measure (ai2)
	DAQmxErrChk(DAQmxCreateTask("", &taskHandleTemperture));
	DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandleTemperture, "Dev1/ai0:1", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
	DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandleTemperture, "", 1000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 1000));



	fprintf(fp, "Times(s)        Volts(V)         Temperture(℃)\n");
	fprintf(fp1, "Times(s)       Volts(V)\n");
	//fprintf(fp2, "Times(s)       Volts(V)         Current(A)\n");
	//fprintf(fp3, "Times(s)        Volts(V)         Temperture(℃)\n");


	DAQmxErrChk(DAQmxRegisterEveryNSamplesEvent(taskHandleTemperture, DAQmx_Val_Acquired_Into_Buffer, 1000, 0, EveryNCallback1, NULL));
	DAQmxErrChk(DAQmxRegisterDoneEvent(taskHandleTemperture, 0, DoneCallback1, NULL));


	DAQmxErrChk(DAQmxStartTask(taskHandleTemperture));

	printf("Acquiring samples continuously. Press Enter to interrupt\n");
	getchar();
	fclose(fp1);
	fclose(fp);
	//fclose(fp2);
	//fclose(fp3);

	DAQmxStopTask(taskHandleTemperture);
	DAQmxClearTask(taskHandleTemperture);

Error:
	if (DAQmxFailed(error))
		DAQmxGetExtendedErrorInfo(errBuff, 2048);

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

//RTD Voltage to Temperture
float64 transferVoltage(float64 V) {
	float64 a = 0.003908, b = 0.00000058019, c, D, Rt, T1, T2, spD;  //Rt = R0 (1 + At +Bt^2)  

	Rt = V / 0.001;
	c = 1 - (Rt / 100);
	D = (a*a) - (4 * b * c);

	spD = sqrt(D);
	T1 = (-a + spD) / (2 * b);
	T2 = (-a - spD) / (2 * b);

	return T1;
}




int32 CVICALLBACK EveryNCallback1(TaskHandle taskHandleTemperture, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
	int32       error = 0, written, j = 0;
	char        errBuff[2048] = { '\0' };
	static int  totalRead = 0, flag = 1, flag1 = 1, flag2 = 1;
	int32       read = 0;
	float64     data[2][1000], tmp, tmpT, dataVout[4000];
	static float k = 0;
	static float64 foreverbig;



	/*********************************************/
	// DAQmx Read Code  
	/*********************************************/
	DAQmxErrChk(DAQmxReadAnalogF64(taskHandleTemperture, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 2000, &read, NULL));
 
	if (read > 0) {
		for (int i = 0; i < 999; i++) {
			fprintf(fp, "%0.3f         %+0.4e        %f\n", k, data[0][i], transferVoltage(data[0][i]));  //Temperture 1
			fprintf(fp1, "%0.3f        %+0.4e\n", k, data[1][i]);  //Voltage
																   
			k = k + 0.001;
		}



		for (int m = 0; m < 998; m++) {
			for (int n = 0; n < 998 - m; n++) {
				if (data[0][n + 1] > data[0][n]) {
					tmpT = data[0][n];
					data[0][n] = data[0][n + 1];
					data[0][n + 1] = tmpT;
				}
			}
		}

		//printf("%f\n", transferVoltage(data[0][0]));
		// Determine if the temperature is higher than 400 ° C
		// The voltage gradually rises
		if (flag == 1) {
			for (int i = 0; i < 999; i++) {

				if ((transferVoltage(data[0][i]) > 400)) {

					for (; j < 4000; j++)
						dataVout[j] = 0.1*(double)j / 4000.0;


					DAQmxErrChk(DAQmxCreateTask("", &taskHandleVoltage));
					DAQmxErrChk(DAQmxCreateAOVoltageChan(taskHandleVoltage, "Dev1/ao0", "", -10.0, 10.0, DAQmx_Val_Volts, NULL));
					DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandleVoltage, "", 1000.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 4000));


					DAQmxErrChk(DAQmxWriteAnalogF64(taskHandleVoltage, 4000, 0, 10.0, DAQmx_Val_GroupByChannel, dataVout, NULL, NULL));

					DAQmxErrChk(DAQmxStartTask(taskHandleVoltage));


					DAQmxErrChk(DAQmxWaitUntilTaskDone(taskHandleVoltage, 10.0));

					DAQmxStopTask(taskHandleVoltage);
					DAQmxClearTask(taskHandleVoltage);

					flag = 0;
					break;
				}
			}
		}

		// Rising voltage
		/*if (flag == 1) {
		//for (int i = 0; i < 999; i++) {
		//if ((transferVoltage(data[0][i]) > 400) || (transferVoltage(data[3][i]) > 400)) {
		// Voltage output (ao0)
		DAQmxErrChk(DAQmxCreateTask("", &taskHandleVoltage));
		DAQmxErrChk(DAQmxCreateAOVoltageChan(taskHandleVoltage, "Dev1/ao0", "", -10.0, 10.0, DAQmx_Val_Volts, ""));
		DAQmxErrChk(DAQmxStartTask(taskHandleVoltage));
		DAQmxErrChk(DAQmxWriteAnalogF64(taskHandleVoltage, 1, 1, 10.0, DAQmx_Val_GroupByChannel, dataV, NULL, NULL));

		DAQmxStopTask(taskHandleVoltage);
		DAQmxClearTask(taskHandleVoltage);

		flag = 0;
		//break;
		//}
		//}
		}*/

		printf("Acquired %d samples. Total %d\n%f\n", (int)read, (int)(totalRead += read), transferVoltage(data[0][0]));
		fflush(stdout);
		// Comparison of size
		/*if (flag1 == 1) {
		for (int m = 0; m < 998; m++) {
		for (int n = 0; n < 998 - m; n++) {
		if (data[2][n + 1] > data[2][n]) {
		tmp = data[2][n];
		data[2][n] = data[2][n + 1];
		data[2][n + 1] = tmp;

		flag1 = 0;
		}
		}
		}
		foreverbig = data[2][999];
		}




		// Determine if each piece of data is currently greater than foreverbig
		for (int i = 0; i < 999; i++) {
		if (data[2][i] > foreverbig) {
		foreverbig = data[2][i];
		}
		}


		if (flag2 == 1) {
		for (int i = 0; i < 999; i++) {
		// Turn off the power if the current is less than 1/10 of the peak value
		if (data[2][i] / foreverbig < 0.1) {
		DAQmxErrChk(DAQmxCreateTask("", &taskHandleVoltage));
		DAQmxErrChk(DAQmxCreateAOVoltageChan(taskHandleVoltage, "Dev1/ao0", "", -10.0, 10.0, DAQmx_Val_Volts, ""));
		DAQmxErrChk(DAQmxStartTask(taskHandleVoltage));
		DAQmxErrChk(DAQmxWriteAnalogF64(taskHandleVoltage, 1, 1, 10.0, DAQmx_Val_GroupByChannel, dataV1, NULL, NULL));

		DAQmxStopTask(taskHandleVoltage);
		DAQmxClearTask(taskHandleVoltage);

		flag2 = 0;
		}
		}
		}*/
	}


Error:
	if (DAQmxFailed(error)) {
		DAQmxGetExtendedErrorInfo(errBuff, 2048);
		/*********************************************/
		// DAQmx Stop Code
		/*********************************************/
		DAQmxStopTask(taskHandleTemperture);
		DAQmxClearTask(taskHandleTemperture);
		printf("DAQmx Error: %s\n", errBuff);
	}
	return 0;
}

int32 CVICALLBACK DoneCallback1(TaskHandle taskHandleTemperture, 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(taskHandleTemperture);
		printf("DAQmx Error: %s\n", errBuff);
	}
	return 0;
}


Output:
Line 19: error: NIDAQmx.h: No such file or directory
Line 8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CVICALLBACK'
Line 9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CVICALLBACK'
Line 11: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'transferVoltage'
Line 17: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'taskHandleVoltage'
Line 18: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'dataV'
Line 19: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'dataV1'
In function 'main':
Line 23: error: 'int32' undeclared (first use in this function)
Line 23: error: (Each undeclared identifier is reported only once
Line 23: error: for each function it appears in.)
Line 23: error: expected ';' before 'error'
Line 24: error: 'TaskHandle' undeclared (first use in this function)
Line 24: error: expected ';' before 'taskHandleTemperture'
Line 44: error: 'error' undeclared (first use in this function)
Line 44: error: 'taskHandleTemperture' undeclared (first use in this function)
Line 45: error: 'DAQmx_Val_Cfg_Default' undeclared (first use in this function)
Line 45: error: 'DAQmx_Val_Volts' undeclared (first use in this function)
Line 46: error: 'DAQmx_Val_Rising' undeclared (first use in this function)
Line 46: error: 'DAQmx_Val_ContSamps' undeclared (first use in this function)
Line 56: error: 'DAQmx_Val_Acquired_Into_Buffer' undeclared (first use in this function)
Line 56: error: 'EveryNCallback1' undeclared (first use in this function)
Line 57: error: 'DoneCallback1' undeclared (first use in this function)
t.c: At top level:
Line 88: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'transferVoltage'
Line 105: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CVICALLBACK'
Line 255: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CVICALLBACK'


Create a new paste based on this one


Comments: