Channels

NI-DAQmx 17.1 .NET Class Library Help

Edition Date: June 2017

Part Number: 370473J-01

»View Product Info
Download Help (Windows Only)

You can associate the following types of channels with a Task in the NI-DAQmx .NET class library:

  • Analog input channels—AIChannel class
  • Analog output channels—AOChannel class
  • Digital input channels—DIChannel class
  • Digital output channels—DOChannel
  • Counter input channels—CIChannel class
  • Counter output channels—COChannel class

These classes contain properties that are specific to a particular type of channel. For instance, properties such as Count are only applicable to counters and are only available on the CIChannel and COChannel classes.

Example: Simple Analog Input Voltage Channel

To create a channel, you can use one of the many create channel methods on the channel collections. The following example creates a simple analog input voltage channel:

VB.NET

myTask.AIChannels.CreateVoltageChannel( _
    "dev1/ai1", _
    "myChannel", _
    AITerminalConfiguration.Differential, _
    -10.0, _
    10.0, _
    AIVoltageUnits.Volts _
)

C#

myTask.AIChannels.CreateVoltageChannel(
    "dev1/ai1", // The physical name of the channel 
    "myChannel", // The name to associate with this channel
    AITerminalConfiguration.Differential, // Differential wiring 
    -10.0, // 10 V minimum 
    10.0, // 10 V maximum
    AIVoltageUnits.Volts // Use volts
);

Example: Channel Indexer

Each collection provides an indexer in Visual C# (the Item method in Visual Basic) that you can use to retrieve a channel associated with a Task object.

Each channel indexer in NI-DAQmx returns a new Channel object which acts as a proxy to configure the Task in terms of the referenced channel. As a result, you can call methods or access properties on a channel, and the effect of these methods and properties persists for the lifetime of the Task. However, NI-DAQmx does not support the removal of channels from existing tasks, so calling Dispose on a Channel releases resources used by the channel proxy object, not the channel itself.

Consider the following two cases, assuming that we have an instance of a Task containing an analog input channel and the Task has been verified:

Case 1

VB.NET

Dim channel As AIChannel = myTask.AIChannels(0)
MyChannel.Description = "Accessing channel"
MyChannel.Dispose()
Dim description As String = MyChannel.Description

C#

AIChannel channel = myTask.AIChannels[0];
MyChannel.Description = "Accessing channel";
MyChannel.Dispose();
string description = MyChannel.Description;

Case 2

VB.NET

myTask.AIChannels(0).Description = "Accessing channel"
myTask.AIChannels(0).Dispose()
Dim description As String = myTask.AIChannels(0).Description

C#

myTask.AIChannels[0].Description = "Accessing channel";
myTask.AIChannels[0].Dispose();
string description = myTask.AIChannels[0].Description;

The channel being accessed in both Case 1 and Case 2 is the same, but Case 1 throws an exception because you cannot access an object once it has been disposed.

In contrast, the Dispose call in Case 2 essentially creates a new AIChannel proxy object and then immediately disposes it. The next line creates a new AIChannel proxy object and then retrieves its Description property. The value of this Description property is set to the same value as it was before the AIChannel proxy object was disposed, which ensures that the state of the Task is preserved.

Note Note   The above example uses the AIChannelCollection in a Task but the same behavior is exhibited by the other channel collections, AOChannelCollection, DIChannelCollection, DOChannelCollection, CIChannelCollection, and COChannelCollection.

Example 2: Channel Indexer

You can use the indexer to retrieve a channel associated with a Task object, as shown in the following example:

VB.NET

'Retrieve an AI channel with the virtual name "myChannel" 
Dim ch as AIChannel
ch = myTask.AIChannels.Item("myChannel")

C#

// Retrieve an AI channel with the virtual name "myChannel"
AIChannel ch = myTask.AIChannels["myChannel"];

The name you pass to the indexer must be the virtual name of the channel, not the physical name. If you use a comma or colon in the name, you also can use the indexer to retrieve a channel object that represents multiple channels, as shown in the following example:

VB.NET

'The AIChannel object, myAIChannel, represents both myChannel0 and myChannel1 
Dim myAIChannel as AIChannel
myAIChannel = myTask.AIChannels.Item("myChannel0,myChannel1")
'Setting this property on myAIChannel sets the property for both channels
myAIChannel.Maximum = 10 
'This is an equivalent approach using colon: 
Dim myAIChannel as AIChannel
myAIChannel = myTask.AIChannels.Item("myChannel0:1")
myAIChannel.Maximum = 10

C#

// The AIChannel object, myAIChannel, represents both myChanne0 and myChannel1
AIChannel myAIChannel = myTask.AIChannels["myChannel0,myChannel1"];
// Setting this property on myAIChannel sets the property for both channels
myAIChannel.Maximum = 10;
// This is an equivalent approach using colon:
AIChannel myAIChannel = myTask.AIChannels["myChannel0:1"];
myAIChannel.Maximum = 10;

Example: Channel Indexer Overload

Each channel collection also provides an overload of the indexer that accepts an integer parameter. You can use the overload to retrieve the nth channel in the collection, or use it in a for loop to iterate over the collection, as shown in the following example:

VB.NET

'Retrieve the fourth channel 
Dim fourthLine as DOChannel
fourthLine = myTask.DOChannels.Item(3) 
'Note that the index is zero-based 

'Iterate over the all of the channels 
For i As Integer = 0 to myTask.DOChannels.Count - 1
    Console.WriteLine(myTask.DOChannels.Item(i).VirtualName)
Next

C#

// Retrieve the fourth channel
DOChannel fourthLine = myTask.DOChannels[3]; 
// Note that the index is zero-based 

// Iterate over the all of the channels 
for(int i = 0; i < myTask.DOChannels.Count; i++ )
{
    Console.WriteLine(myTask.DOChannels[i].VirtualName);
}
Related information

WAS THIS ARTICLE HELPFUL?

Not Helpful