Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the available analog input channel names in NI DAQmx by using c API provided by NI.

I haven't made a list of channels, but I have made a routine to count the AI channels on a board. It only works if you have no AI task currently defined for the board. It creates a task, then adds channels to the task one at a time starting with device/ai0. When it encounters an error it stops and returns the loop counter. It could be adapted to making a list of channel names in a string.

It looks like this:

"p" points to a structure that's used to communicate with a host application. Add_NIDAQ_error_ToList() is error handling for a specific environment. Simply think "error handling". Note that it tries RSE, NRSE, and Diff modes before giving up. StringFromHandleAndDispose() is another artifact of interfacing to the host application.

p->result = 0;

string devname = StringFromHandleAndDispose(p->DevName);
if (devname.empty())
{
Add_NIDAQ_error_ToList("Bad Device", EXPECTED_DEVICE_NAME);
p->result = 1;
return 0;
}

int32 error = 0;
TaskHandle h;
if (error = DAQmxCreateTask("CountAIInputsTask", &h))
{
Add_NI_error_ToList(devname, "DAQmx_CreateTask", error);
p->result = nan();
}

int i = 0;
do
{
ostringstream channel;
channel << devname << "/ai" << i;
if (error = DAQmxCreateAIVoltageChan(h, channel.str().c_str(), "", DAQmx_Val_RSE, -10, 10, DAQmx_Val_Volts, NULL))
if (error = DAQmxCreateAIVoltageChan(h, channel.str().c_str(), "", DAQmx_Val_NRSE, -10, 10, DAQmx_Val_Volts, NULL))
if (error = DAQmxCreateAIVoltageChan(h, channel.str().c_str(), "", DAQmx_Val_Diff, -10, 10, DAQmx_Val_Volts, NULL))
break;

++i;
} while (!error);

p->result = static_cast(i);
DAQmxClearTask(h);

return 0;
John Weeks

WaveMetrics, Inc.
Phone (503) 620-3001
Fax (503) 620-6754
www.wavemetrics.com
0 Kudos
Message 11 of 11
(943 Views)