Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmxCfgSampClkTiming when setting up a digital input channel using the nidaqmx drivers

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?
0 Kudos
Message 1 of 6
(6,614 Views)
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 2 of 6
(6,606 Views)
Deirhack,

As a starting point I suggest that you have a look at the DOexample attached. The example sets a single digital line value, there are other examples in the C:\Program Files\National Instruments\NI-DAQ\Examples\VisualC\Do directory.

Let me know if this helps you.

Kind Regards

Tristan
Applications Engineer
National Instruments UK & Ireland
www.ni.com/uk
0 Kudos
Message 3 of 6
(6,596 Views)
I'm not sure what to do with this as i don't get an error for my DAQmxCreateDIChan function and I'm not sure what else this program that you suggested does. I have check the digital line on the test panel and there seems to be data triggering it.
0 Kudos
Message 4 of 6
(6,582 Views)
Hi,

The 6024E digital lines are not hardware timed, you will have to use software polling at the rate you require to read digital data. Therefore you will not be able to use the DAQmxCfgSampClkTiming function. The example I attached to the last message was actually Traditional DAQ, I have attached a DAQmx example to read from a digital line. The example is in C rather than C++ but the function calls will be the same.

If you need to do hardware timed DIO then the M-Series cards or one of the dedicated digital cards.

Kind Regards

Tristan
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 5 of 6
(6,574 Views)
Thanks very much for your help. I think that may solve my problems. I wasn't aware of the timing issue as am new to this.
Much appreciate.
0 Kudos
Message 6 of 6
(6,562 Views)