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.

PXI

cancel
Showing results for 
Search instead for 
Did you mean: 

Task specified is invalid or does not exist

I have a C file with the following contents

 

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <string>
#include <iostream>
#include <Windows.h>
#include <setjmp.h>
#include "NIDAQmx.h"
#include "nisyscfg.h"
#include "nisyscfg_pxi.h"
#include "nisyscfg_errors.h"
#include "pxi_driver.h"
using namespace std;
TaskHandle task = 0;
char	errBuff[2048]={'\0'};
int32       error=0;
struct fileData{
	int channel1;
	int channel2;
	int channel3;
	int channel4;
	int channel5;
	int channel6;
	int channel7;
	struct fileData *next;
} *data;
int *index;
int listSize = 0;
int tempArray[1];
int *channel1_index;
int *channel2_index;
int *channel3_index;
int *channel4_index;
int *channel5_index;
int *channel6_index;
int *channel7_index;
int *channel8_index;
int *channel9_index;
int *channel10_index;
int *channel11_index;
int *channel12_index;
int *channel13_index;
int *channel14_index;
int *channel15_index;
int *channel16_index;
int *channel17_index;
int *channel18_index;
int *channel19_index;
int *channel20_index;
int *channel21_index;
int *channel22_index;
int *channel23_index;
int *channel24_index;
int *channel25_index;
int *channel26_index;
int *channel27_index;
int *channel28_index;
int *channel29_index;
int *channel30_index;
int *channel31_index;
int *channel32_index;
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) errorCheck(); else
#define DAQmxErrChk2(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(int argc, char *argv[])
{
	int returnCode = 0;
	if(argc < 7){
		printf("%d arguments provided, need 6 arguments",argc);
		exit(1);
	}
	printf("opening file %s to read\n",argv[1]);
	FILE *replayFile = openFile(argv[1]);
	if(replayFile != NULL){
		printf("finished reading file\n");
	} else {
		printf("error reading file %s...exiting\n",argv[1]);
		exit(1);
	}
	readFile(replayFile);
	printf("creating task...\n");
	printf("Entering debug mode...press enter once connected\n");
	getchar();
	createTask();
	if(returnCode == 0){
		printf("task created\n");
	} else {
		printf("unable to create task...exiting\n");
		exit(returnCode);
	}
	printf("Opening device %s on port %s with lines %s:%s...\n",argv[2],argv[3],argv[4],argv[5]);
	startTask();
	openDevice(argv[2],argv[3],argv[4],argv[5]);
	if(returnCode == 0){
		printf("Device opened\n");
	} else {
		printf("Device opened with warnings\n");
	}
	printf("starting task\n");
	startTask();
	if(returnCode == 0){
		printf("Task started\n");
	} else {
		printf("Task started with warnings\n");
	}
	printf("replaying file\n");
	filePlayBack();
	printf("End of program, press Enter key to quit\n");
	getchar();
	return 0;
}

FILE *openFile(char filePath[]){
	int returnCode = -2;
	FILE *replayFile;
	if(replayFile = fopen(filePath, "r")){
		printf("Reading file %s\n",filePath);
	}
	return replayFile;
}

void openDevice(char device[],char port[],char lineHigh[],char lineLow[]){
	printf("value of task before open device is %d\n",&task);
	//DAQmxErrChk(DAQmxCreateDOChan(task,strcat(device,"/port0"),"",DAQmx_Val_ChanForAllLines));
	DAQmxErrChk(DAQmxCreateDOChan(&task,"DEV5/port0","",DAQmx_Val_ChanForAllLines));
	printf("value of task after opening device is %d\n",&task);
}

void readFile(FILE *replayFile){
	if(&replayFile != NULL){
		char buff[1024];
		fileData *new_node, *current = NULL;
		new_node = (fileData*)malloc(sizeof(fileData));
		new_node->next=NULL;
		data = new_node;
		current = new_node;
		while(fgets(buff,1024,replayFile) != NULL){
			char * delim = strtok(buff, ",");
			new_node->channel1 = atoi(delim);
			delim = strtok(buff, ",");
			new_node->channel2 = atoi(delim);
			delim = strtok(buff, ",");
			new_node->channel3 = atoi(delim);
			delim = strtok(buff, ",");
			new_node->channel4 = atoi(delim);
			listSize++;
			current->next=new_node;
			current=new_node;
			new_node = (fileData*)malloc(sizeof(fileData));
			new_node->next=NULL;
		}
		fclose(replayFile);
	} else {
		printf("no file was opened...exiting\n");
		exit(1);
	}
}

void filePlayBack(){
	printf("writing data...\n");
	int32 *numSamples;
	int32 temp = 4;
	numSamples = &temp;
	int numofChannels = 4;
	int *fileArray = listToArray();
	int i = 0;
	for(i = 0; i < listSize; i++){
		printf("writing value %d to channel\n",*(fileArray+i));
		//int value = *(channel1_index+i);
		//printf("writing value %d to channel\n",value);
		int value = *(fileArray+i);
		tempArrayValue(value);
		DAQmxErrChk(DAQmxWriteRaw(task,10000,true,-1,tempArray,numSamples,NULL));
		/*value = *(channel2_index+i);
		printf("writing value %d to channel\n",value);
		tempArrayValue(value);
		DAQmxWriteRaw(task,10000,true,-1,tempArray,numSamples,NULL);
		value = *(channel3_index+i);
		printf("writing value %d to channel\n",value);
		tempArrayValue(value);
		DAQmxWriteRaw(task,10000,true,-1,tempArray,numSamples,NULL);
		value = *(channel4_index+i);
		printf("writing value %d to channel\n",value);
		tempArrayValue(value);
		DAQmxWriteRaw(task,10000,true,-1,tempArray,numSamples,NULL);*/
		Sleep(10);
	}
}

void tempArrayValue(int value){
	if(value >= 5){
		tempArray[0] = 1;
	}
	else if(value < 5){
		tempArray[0] = 0;
	}
}

int *listToArray(){
	struct fileData *temp;
	temp = data;
	int *channel1_index = (int*)calloc(listSize,sizeof(int));
	int *channel2_index = (int*)calloc(listSize,sizeof(int));
	int *channel3_index = (int*)calloc(listSize,sizeof(int));
	int *channel4_index = (int*)calloc(listSize,sizeof(int));
	int i = 0;
	for(i=0;i<listSize;i++){
		*(i+channel1_index) = temp->channel1;
		*(i+channel2_index) = temp->channel2;
		*(i+channel3_index) = temp->channel3;
		*(i+channel4_index) = temp->channel4;
		temp=temp->next;
	}
	while(temp!=NULL){
		temp=temp->next;
		free(temp);
	}
	return channel1_index;
}

void createTask(){
	DAQmxErrChk2(DAQmxCreateTask("",&task));
	printf("value of task when creating task is %d\n",&task);
	Error:
		if( DAQmxFailed(error) )
			DAQmxGetExtendedErrorInfo(errBuff,2048);
		if( task!=0 ) {
			/*********************************************/
			// DAQmx Stop Code
			/*********************************************/
			DAQmxStopTask(task);
			DAQmxClearTask(task);
		}
		if( DAQmxFailed(error) ){
			printf("DAQmx Error: %s\n",errBuff);
			printf("End of program, press Enter key to quit\n");
			getchar();
			exit(1);
		}
}

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

void errorCheck(){
	printf("in error check\n");
	if( DAQmxFailed(error) )
		DAQmxGetExtendedErrorInfo(errBuff,2048);
	if( task!=0 ) {
		DAQmxStopTask(task);
		DAQmxClearTask(task);
	}
	if( DAQmxFailed(error) )
		printf("DAQmx Error: %s\n",errBuff);
	printf("End of program, press Enter key to quit\n");
	getchar();
	exit(1);
}

The program was working fine now when I run the program I get the following output

 

value of task when creating task is 3814276
task created
Opening device DEV5 on port 0 with lines 1:2...
DAQmx Error: Task specified is invalid or does not exist.
Status Code: -200088
End of program, press Enter key to quit

What in my code could be causing this issue, or is it an issue external to the code and possibly in my hardware I am using?

0 Kudos
Message 1 of 2
(2,710 Views)

Hi jgr208,

 

Have you made any changes to the hardware or code after it was working properly?

 

Also, if you try to use these channels or create a task using this device in NI MAX are you able to do this successfully?

 

Thanks,

 

ShaneK

Applications Engineering

0 Kudos
Message 2 of 2
(2,657 Views)