Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with digital input

I am using VC++ .net to program an acquisition program for my 6024E board.
I am using the nidaqmx drivers for C and have already programmed an analog
input channel which works fine. However i now want to program a digital
channel and am wondering if i am using the wrong functions. I have kept the
DAQmxCfgSampClkTiming function from the previous program but there seems to be
a problem with it. Does anyone have the basics of writing a digital input
program?

It gives an error for the DAQmxCfgSampClkTiming function of "Requested value is not a supported value for this property". Perhaps i shouldn't set any timing for a digital channel.

Also if it any help, here is the code.

#include
#include
#include

using namespace std;

int main()
{
TaskHandle hMyTask;
bool32 isTaskDone;

int32 errorCode;
char errorString[1000];

float64 readArray[288];
int32 sampsPerChanRead;
int32 numBytesPerSamp;
float64 samprate = 32;
uInt64 noofsamps = 288;

cerr << "Hello from tedacq!" << endl;

// Open file to record data
ofstream data_file("test.dat", ios::binary);

// Create task
cerr << "Creating task...";
if (DAQmxCreateTask("my task", &hMyTask) == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
return 1;
}

//Add digital channel to task
cerr << "Creating digital input channel...";
errorCode = DAQmxCreateDIChan(
hMyTask,
"Dev1/port0/line0",
NULL,
DAQmx_Val_ChanPerLine);
if (errorCode == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
DAQmxGetErrorString (errorCode, errorString, 1000);
cerr << errorString << endl;
}


//set sampling rate etc for task
/*cerr << "Configuring sampling rate etc...";
errorCode = DAQmxCfgImplicitTiming(
hMyTask,
DAQmx_Val_FiniteSamps, //samplemode
noofsamps);
if (errorCode == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
DAQmxGetErrorString (errorCode, errorString, 1000);
cerr << errorString << endl;
}*/



// Set sampling rate etc for task
cerr << "Configuring sampling rate etc...";
errorCode = DAQmxCfgSampClkTiming(
hMyTask,
NULL, // Clock source - NULL for internal
32, // sampling rate
DAQmx_Val_Rising, // Acquire sample on rising clock edge
DAQmx_Val_FiniteSamps, // Acquire a finite number of samples
288);

if(errorCode == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
DAQmxGetErrorString (errorCode, errorString, 1000);
cerr << errorString << endl;
}

// Start task
cerr << "Starting task...";
if (DAQmxStartTask(hMyTask) == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
}

// Wait for task to finish
cerr << "Querying task status...";
while (true)
{
if (DAQmxIsTaskDone(hMyTask, &isTaskDone) == 0)
{
// Query was successful
if (isTaskDone)
{
cerr << "Done" << endl;
break;
}
else
{
cerr << ".";
}
}
else
{
// Query was unsuccessful
cerr << endl << "Error querying task.";
break;
}
}

//Read samples
cerr << "Reading Samples...";
errorCode = DAQmxReadCounterF64(
hMyTask,
288, //int32 numSampsPerChan
-1, //float64 timeout
readArray, //float64 readArray[]
288, //uInt32 arraySizeInSamps
&sampsPerChanRead, //int32 *sampsPerChanRead
NULL);//bool32 *reserved
if (errorCode == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
DAQmxGetErrorString (errorCode, errorString, 1000);
cerr << errorString << endl;
}


//// Read samples
//cerr << "Reading samples...";
//errorCode = DAQmxReadDigitalLines(
// hMyTask, //taskhandle
// 288, // int32 numSampsPerChan
// 1, // float64 timeout,
// DAQmx_Val_GroupByChannel, // bool32 fillMode,
// readArray, // uInt8 readArray[]
// 288, // uInt32 arraySizeInBytes,
// &sampsPerChannelRead, // int32 *sampsPerChanRead,
// &numBytesPerSamp, //int32 *numBytesPerSamp,
// NULL // bool32 *reserved,
// );
//if (errorCode == 0)
//{
// cerr << "OK" << endl;
//}
//else
//{
// cerr << "Error" << endl;
// DAQmxGetErrorString (errorCode, errorString, 1000);
// cerr << errorString << endl;
//}

// Stop task
cerr << "Stopping task...";
if (DAQmxStopTask(hMyTask) == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error" << endl;
}

// Clear task
cerr << "Clearing task...";
if (DAQmxClearTask(hMyTask) == 0)
{
cerr << "OK" << endl;
}
else
{
cerr << "Error destroying task" << endl;
}

// Write data to file
for (int n=0 ; n<288 ; ++n)
{
data_file << readArray[n] << endl;
}

// Close file
data_file.close();
}
0 Kudos
Message 1 of 2
(2,930 Views)
Hi Deirhack,

Thanks for your post.

You may find examples of code to read in from digital lines in the NI-DAQ directory. The default is:

C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Digital

From here you have two options- 1)examples to read from cahnnels & 2)writing examples.
Let me know how you get on.
Many thanks,
National Instruments | Northern California
0 Kudos
Message 2 of 2
(2,914 Views)