I'll take a shot at this one.
Remember that the power of Nidaqmx is not in it's simplicity but in it's commonality. When you can use almost identical code between environments, and across instrumentation, then it starts to really pay off. What that means is it's harder to do simple stuff but worth the pain in the long run.
I like to make calls to my digital devices with something like :
digital.WritePort(3,0xAA);
to write a value of 0xAA to port 3 of my digital IO device.
To achieve this I use the digital class at the end of this post: (cut and paste it into a .NET window so you can close the #region/#endregion blocks and make more sense of it)
I've tried to put the stuff I think would be most interesting to you in
red. There is a lot of stuff that's probably not interesting to you but I didn't want to take the time to try and figure out the most relevant stuff.
I want you to see the similarity between all of the digital reads and writes though so I've included everything. Try to look past all of the extra stuff and focus on the basic NiDAQmx approach. This consists of :
1.) Creating a task
2.) Creating the device Name
3.) Initializing the task
4.) Creating an object of the desired type to read or write from
5.) Performing the data transfer
Here are some examples. The first two are taken from the digital class at the end of this post.
Example # 1 : Write a HIGH value to line 5 of port 2 of a device named "dev2" in MAX.
1.) Create a task :
Task t = new Task();2.) Creating the device Name :
string strDevice = "dev2/port2/line5";
3.) Initializing the Task :
t.DOChannels.CreateChannel(strDevice,"",ChannelLineGrouping.OneChannelForEachLine);4.) Creating an object to write out on :
DigitalSingleChannelWriter writer = new DigitalSingleChannelWriter(t.Stream);5.) Getting data ready to write out :
bool[] LineSate = new bool[1]; LineState[0] = true;6.) Writing the value out. :
writer.WriteSingleSampleMultiLine(true, LineState);
Example #2 : Read the contents of port 7 from a device named Dev3 in MAX )
1.) Create a task :
Task t = new Task();
2.) Creating the device Name :
string devName = "Dev3/Port7";
3.) Initializing the Task :
t.DIChannels.CreateChannel(devName, "", ChannelLineGrouping.OneChannelForAllLines);
4.) Creating an object to read in on :
DigitalSingleChannelReader reader = new DigitalSingleChannelReader(t.Stream);5.) Read the value :
byte portVal = reader.ReadSingleSamplePortByte();Example #3: Peform an analog write of 1.25V to the third channel (index 0) of a device named "Dev1" in MAX :
1.) Create a task :
Task t = new Task();
2.) Creating the device Name :
string strDevice = "Dev1/ao2";
3.) Initializing the Task : t.AOChannels.CreateVoltageChannel(strDevice, "", outputMin, outputMax, AOVoltageUnits.Volts);
4.) Creating an object to write out analog out on : AnalogSingleChannelWriter writer = new AnalogSingleChannelWriter(t.Stream);
5.) Write the value : writer.WriteSingleSample(true, 1.25);
See the similartity? I have a lot of C++ driver code that I took to C#. Since it was all in NiDAQmx it was really a very simple change to get the same functionality in C#. Mostly changing CString to string type stuff.
Anyway, I hope some of this helps. I probably shouldn't even paste in all the rest of this but I'm going to anyway. Let me know how it goes okay?
Grant
//---------------MY DIGITAL CLASS CODE FOR WHAT IT'S WORTH --------------------------------
Too large. Included it as an attached file.
// --------------------------------
Grant M. Johnson
Project Engineer
LECO Corporation