|Download Help (Windows Only)|
You can associate the following types of channels with a Task in the NI-DAQmx .NET class library:
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.
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:
myTask.AIChannels.CreateVoltageChannel( _ "dev1/ai1", _ "myChannel", _ AITerminalConfiguration.Differential, _ -10.0, _ 10.0, _ AIVoltageUnits.Volts _ )
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 );
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:
Dim channel As AIChannel = myTask.AIChannels(0) MyChannel.Description = "Accessing channel" MyChannel.Dispose() Dim description As String = MyChannel.Description
AIChannel channel = myTask.AIChannels; MyChannel.Description = "Accessing channel"; MyChannel.Dispose(); string description = MyChannel.Description;
myTask.AIChannels(0).Description = "Accessing channel" myTask.AIChannels(0).Dispose() Dim description As String = myTask.AIChannels(0).Description
myTask.AIChannels.Description = "Accessing channel"; myTask.AIChannels.Dispose(); string description = myTask.AIChannels.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 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.|
You can use the indexer to retrieve a channel associated with a Task object, as shown in the following example:
'Retrieve an AI channel with the virtual name "myChannel" Dim ch as AIChannel ch = myTask.AIChannels.Item("myChannel")
// 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:
'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
// 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;
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:
'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