Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to multiple DO lines in a task configured in MAX

I configured a DO task in MAX that writes to lines 0-3 and 5 of port 1 (dev2). The reason I do it this way is because I don't want to mess with the states of the other lines in the port.

 

Using VB2008, how do I write to each of these lines? Can it be done so all lines change states simultaneously?

 

Thanks!

0 Kudos
Message 1 of 8
(5,011 Views)

Hi Steverino,

 

I've attached a modified version of the VB examples that are included with NI-DAQ (you can find the unmodified ones at Start»Programs»National Instruments»NI-DAQ»Text-Based Code Support). What I have modified is the number of lines to write out. For the line definition you can include noncontiguous line by using a comma in between. For example for your system you can select "Dev2/Port1/line0:3,Dev2/Port1/line5" This will control lines 0-3 and 5 on port 1. From this example, you should be able to create a program that writes individual lines as well.

Steven K.
National Instruments
Software Engineer
0 Kudos
Message 2 of 8
(4,997 Views)

Thanks, Steven.

 

I tried the example you provided and it does what I hoped.

 

A follow-up question, however: In MAX I defined an NI-DAQmx Task under Data Neighborhood. I have a digital out task called, taskSetGain. I set up four channels in the task called Gain1, Gain2, Gain3 and Gain4. These are assigned to Port1/Line0, Port1/Line1, Port1/Line2, and Port1/Line5, respectively. When I change gain I'd like them all to switch at the same time. Obviously, the beauty behind defining tasks in MAX is the code becomes device independent. My code no longer cares if I am using Dev1 or Dev8, Port0 or Port3, etc.

 

In VB2008, how do I address the lines of a MAX-defined task?

 

Thanks!

0 Kudos
Message 3 of 8
(4,975 Views)

Hi Steverino,

 

You can access the tasks in Measurement & Automation (MAX) using the DaqSystem.Local.LoadTask("<taskname>"). This will pull a reference to your DAQmx task created in MAX. For example if you wanted to call your task myTask using an task in MAX called aiTask, you would use:

 

Dim myTask As Task = DaqSystem.Local.LoadTask("aiTask")

 

Hope this helps.

Steven K.
National Instruments
Software Engineer
0 Kudos
Message 4 of 8
(4,960 Views)

Thanks, Steven.

 

I understand that I can reference the task in the way you describe. Having done that, how do I read from or write to the various lines in the task?

 

 

0 Kudos
Message 5 of 8
(4,956 Views)

Hi Steverino,

 

In this example that Steven sent to you, the writing is done as follows: 

 

Dim writer As New DigitalSingleChannelWriter(digitalWriteTask.Stream)
writer.WriteSingleSampleMultiLine(True, dataArray)

 

It pulls in the values from the boolean array on the form into the stream, and then writes the array to hte lines.

 

best,

Adam
Academic Product Manager
National Intruments
0 Kudos
Message 6 of 8
(4,903 Views)

Adam,

 

This is my code:

 

            Dim dataArray() As Boolean = {False, False, False, False, False, False, False, False}
            digitalWriteTask = DaqSystem.Local.LoadTask(_diTaskName)
            Dim writer As New DigitalSingleChannelWriter(digitalWriteTask.Stream)
            writer.WriteSingleSampleMultiLine(True, dataArray)

 

_doTaskName refers to a digital out task defined in MAX having 8 seperate, non-consecutive DO lines. When I execute this code, I get error -200524.

 

What am I doing wrong?

 

Thanks!

 

0 Kudos
Message 7 of 8
(4,880 Views)

Hi Steverino,

 

The programming is slightly different if you are using a Measurement & Automation Explorer (MAX) task. Since each line is a different channel, you need to use a DigitalMultipleChannelWriter. I've written some code in C#. The structure should be almost the same for VB

 

 

bool[,] dataArray = new bool[4,1];
                    dataArray[0,0] = bit0CheckBox.Checked;
                    dataArray[1,0] = bit1CheckBox.Checked;
                    dataArray[2,0] = bit2CheckBox.Checked;
                    dataArray[3,0] = bit3CheckBox.Checked;
                    
                    
                    DigitalMultiChannelWriter writer = new 			 DigitalMultiChannelWriter(digitalWriteTask.Stream);
                    
                    writer.WriteSingleSampleMultiLine(true, dataArray);

The array format that WriteSingleSampleMultiLine takes is a 2D array. The first index is the channel index, which is in the order of how the channels were arranged in MAX. The next index is the line index within the channel. Since there is only one physical line in each channel, keep this index 0.

Steven K.
National Instruments
Software Engineer
0 Kudos
Message 8 of 8
(4,854 Views)