Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading Analog Input with NIDaqmx base on C#

Hy everybody!
 
I am trying to write a program to read multiple anaolg inputs SINGLE ENDED MODE on C# and i've been reading a lot about it.
I'm starting by trying to read only one channel guessing that it will be easier to read the others after this.
 
I'm using a NI 6008 USB DAQ with NI-Daqmx base. My program must use the base version because i want it to run on a old laptop runing only windows 2000.
 
So far the best i could assemble is the code bellow, but i can't read anything from the board.
I know it is hard to write this program but it is possible to do it. After having a few problems with LoaderLock on the compiler, i still could not read anything (i was expecting to read 5 Volts with some precision).
 
I have in an attachement my code and a text file i got from the internet on wich i have based my program.
 
Please help me.
 
Thank you very much.
 
Code:

[DllImport("nidaqmxbase.dll")]

public static extern int DAQmxBaseCreateTask(string taskName, out TaskHandle taskHandle);

// int32 DAQmxBaseCreateTask (const char taskName[ ], TaskHandle *taskHandle);

[

DllImport("nidaqmxbase.dll")]

public static extern int DAQmxBaseCreateAIVoltageChan(TaskHandle taskHandle, string physicalChannel, string nameToAssignToChannel, int terminalConfig, double minVal, double maxVal, int units, string customScaleName);

// int32 DAQmxBaseCreateAIVoltageChan (TaskHandle taskHandle, const char physicalChannel[ ], const char nameToAssignToChannel[ ], int32 terminalConfig, float64 minVal, float64 maxVal, int32 units, const char customScaleName[ ]);

[

DllImport("nidaqmxbase.dll")]

public static extern int DAQmxBaseStartTask(TaskHandle taskHandle);

// int32 DAQmxBaseStartTask (TaskHandle taskHandle);

[

DllImport("nidaqmxbase.dll")]

public static extern int DAQmxBaseReadAnalogF64(TaskHandle taskHandle, int numSampsPerChan, double timeout, System.UInt32 fillMode, out double readArray, System.UInt32 arraySizeInSamps, out int sampsPerChanRead, out System.UInt32 reserved);

// int32 DAQmxBaseReadAnalogF64 (TaskHandle taskHandle, int32 numSampsPerChan, float64 timeout, bool32 fillMode, float64 readArray[], uInt32 arraySizeInSamps, int32 *sampsPerChanRead, bool32 *reserved);

[

DllImport("nidaqmxbase.dll")]

public static extern int DAQmxBaseStopTask(TaskHandle taskHandle);

// int32 DAQmxBaseStopTask (TaskHandle taskHandle);

[

DllImport("nidaqmxbase.dll")]

public static extern int DAQmxBaseClearTask(TaskHandle taskHandle);

// int32 DAQmxBaseClearTask (TaskHandle taskHandle);

 

private void Form1_Load(object sender, EventArgs e)

{

TaskHandle taskHandle = 0;

double data;

int pointsread;

int DAQmx_Val_Volts = 10348; // Volts

System.

UInt32 reserved;

 

 

DAQmxBaseCreateTask(

"",out taskHandle);

DAQmxBaseCreateAIVoltageChan(taskHandle,

"/Dev1/ai0", "", -1, -5, 5, DAQmx_Val_Volts, null);

DAQmxBaseStartTask(taskHandle);

DAQmxBaseReadAnalogF64(taskHandle, 1, 1, 0,

out data, 1000,out pointsread, out reserved);

DAQmxBaseStopTask(taskHandle);

DAQmxBaseClearTask(taskHandle);

textBox1.Text =

Convert.ToString(data);

 

Download All
0 Kudos
Message 1 of 10
(7,370 Views)

Hi henrique-

You should use NI-DAQmx- it supports your device on Windows 2000.  The most recent version is NI-DAQmx 8.6, available here.  The other benefit of using NI-DAQmx is that a .NET API is provided so you do not need to import functions directly from the ANSI .dll.

Once you install NI-DAQmx you can find .NET-based examples in this directory ( C:\Documents and Settings\All Users\Documents\National Instruments\NI-DAQ\Examples\DotNET2.0 ) or similar.  I would recommend this example ( C:\Documents and Settings\All Users\Documents\National Instruments\NI-DAQ\Examples\DotNET2.0\Analog In\Measure Voltage\ContAcqVoltageSamples_IntClk\cs ) to help get up and running with the NI-DAQmx API.

Hopefully this helps-

Message Edited by Tom W [DE] on 11-02-2007 03:20 PM

Tom W
National Instruments
0 Kudos
Message 2 of 10
(7,363 Views)
Thank you very much Tom, i will try your suggestion.
I had tried installing nidaqmx before but the installation was too long so i thought the package was too heavy for use with the laptop, that is why i changed back into nidaqmx base.
 
If i have any success i'll post here the source code to help other users.
 
Have a nice day.
0 Kudos
Message 3 of 10
(7,351 Views)
    Thank you very much, the examples compiled and worked on the first time! It was exactly what i was looking for.


0 Kudos
Message 4 of 10
(7,315 Views)

Hi henrique-

That's great- thank you for the follow-up.Smiley Happy

Tom W
National Instruments
0 Kudos
Message 5 of 10
(7,313 Views)
    Hi Tom, sorry to bother you again.

I have a problem with my program.
I am attempting to read samples with a given frequency  using the  system clock (i can 't use the internal clock for my application because i need the timing from the computer).
I want to read all the 8 Analog Inputs in Single Ended Mode (with a NI6008 USB DAQ) by using the ReadSingleSample() function. The reading is done when the OnTimedEvent event handler is raised by the secondary thread.

I also need to see what values i am reading so i am writing everything into textboxes.

The strangest thing is that the program runs fine until 3Hz. If i increase the acquisition frequency to 4Hz i get the following errors:

"The specified resource is reserved. The operation could not be completed as specified.  Status code: -50103"

and

"Measurements: Task specified is invalid or does not exist. Status code: -200088".

I have searched the forums and i have seen some resolutions for the problem but i have made changes to my code for many days now and i keep getting the same error. I am not a coding expert but i am trying to understand what is wrong with my tasks... i am a bit suspicious of the functions that are writing to the textboxes because if i don't write anything to them the program runs fine.

I have attached the code in the post (runs on Visual studio 2005, C#).

Thank you very much for your help. Smiley Happy
0 Kudos
Message 6 of 10
(7,244 Views)
Hi henrique-
 
I have a couple of suggestions.  First, the current structure of your programming is initiating seperate read operations for each channel in the task.  This operation actually performs a scan of the channels in the task on the hardware level, so it is a better idea to perform one scan and then parse through the data, like this:
 

//Plot Multiple Channels to the textboxes

//Example: x value of accelerometer 1 will be red to Dev1/ai0 (analog input 0 or ai0)
tempData = new double[8];
tempData = reader.ReadSingleSample();
this.SetText_a1x(Convert.ToString(tempData[0]));

...

 

Additionally, you could consider increasing performance of the task by calling Start on task.control once before entering your read processing loop.  This will help to eliminate the overhead of starting and stopping the task with each iteration of the loop.

Hopefully this helps-



Message Edited by Tom W [DE] on 11-16-2007 01:50 PM
Tom W
National Instruments
0 Kudos
Message 7 of 10
(7,239 Views)

Ok, i will try that on monday and then come back with the feedback.

Thanks.

0 Kudos
Message 8 of 10
(7,235 Views)
    Hi Tom,

I have tried to implement your suggestions.

By reading the data into the tempData variable, i can read data at higher frequency but only until 9Hz, and only if i start by reading with 3Hz, and progressivly increasing to 9Hz. If i try higher frequencies i get the same errors i got before.

I also tried to implement the myTak.control(taskaction.start) but i can't even read any value. Am i doing something wrong?

I have attached the program with the changes you have suggested and a screen shot (Error.jpg) so that you get a look at what i am getting.

Once again thank you very much for you help, it has been very helpfull.

Have a nice day.


Message Edited by henrique21 on 11-19-2007 08:39 AM
0 Kudos
Message 9 of 10
(7,211 Views)
    The problem has been solved. I just quit using threads, a friend told me about the timer object and it's tick property. It worked like a charm.

Smiley Very Happy
0 Kudos
Message 10 of 10
(7,169 Views)