In addition to channel specific functions there are several function that affect the list of channels and allow dynamic creation and removal of channels. All these functions start with "Channel.", i.e. strList = Channel.ListAll()
Before we get into the functions a few quick important points:
1) These function ONLY work on the Local connection. Attempting to use them on any other connection will have unpredictable results.
2) These functions will not update the workspace with any added or removed channels. The only way to get the workspace to update is to go to the channel table, make a minor modification and apply your changes. These functions are designed for dynamic applications that typically run in the runtime environment that doesn't have a workspace. To make them quicker, especially when dynamically adding many channels, the workspace is not refreshed.
3) If you add or remove any channel with timing information you must call Channel.Restart() to get DAQFactory to start or stop acquiring data for that channel.
Add(channel name, [device = "Test"], [device number = 0], [i/o type = "A to D"], [channel = 0], [timing = 0], [offset = 0], [specifier = ""], [group = "Main"]): adds a new channel to the local connection with the given name. If the channel already exists another is not created, but instead the current channel is updated with any of the information provided. Only the channel name is a required parameter. In cases where you are dynamically creating channels to use solely with AddValue(), this is all you should need to set, unless you want to group your channels (see ListAll()). For the device and I/O type parameters, you should pass a string with the desired device name or I/O type. Remember if timing equals something other than 0, you must call Restart() to start the timing loop.
Once you have a channel created, you can change parameters dynamically using the execute command:
Execute(strMyChannel + ".HistoryLength = NewHistoryLength")
Where both strMyChannel and NewHistoryLength are your variables. Of course, if you know the channel name at design time, you should just do:
MyChannel.HistoryLength = NewHistoryLength
AddValue(device name, device number, io type name, channel, value) This works just like the channel version of AddValue() described in the last section, except you don't have to know the channel name for it to work. Instead, you should provide the name of the device type, the device number, the name of the I/O type and the channel to specify which channel you wish to add the value to. For example:
Channel.AddValue("Test",0,"A to D",0,5)
This function will return an error if the specified channel does not exist. This function is most commonly used within user and communication devices when you wish to create a generic driver.
ClearAll(): removes all the channels in the Local connection. Remember if any of the channels have a timing other than 0 you must call Restart() to stop all the timing loops.
Delete(channel name): removes the channel with the given name if it exists. Remember, if the removed channel has a timing other than 0, you must call Restart() to remove it from the timing loops. Failure to do so will create excess overhead.
ListAll([group]): returns an array of strings containing the names of all the channels. If group is provided, only those channels in the given group are returned: Channel.ListAll("Main"). Used in conjunction with Execute() or Evaluate() you could easily apply things across groups of channels.
Read(string Array): takes an array of strings containing channel names and reads those channels. For example:
Channel.Read({"myChan", "myOtherChan"})
This is similar to doing mychan.read() and myOtherChan.read(), except that by doing it in a single call, drivers are able to optimize reads, for example Modbus reads can be optimized into a single call.
ReadGroup(groupName): takes a string containing the name of one your channel groups and reads all the channels in that group. For example:
Channel.ReadGroup("myGroup")
This works just like Channel.Read() except uses the channel's group setting instead of taking a list of channels. Make sure you do not have any output channels in the Group!
Restart(): this stops all the timing loops and restarts them with any changes from the above functions or variable changes with channels described in the previous section. Restart() is not terribly fast, especially when dealing with slow devices as it has to wait for the current loop to complete. For this reason, the loops are not restarted automatically when a parameter changes or a new channel is added.
StopAll(): this stops all the timing loops, just like Restart(), but does not restart them. To restart them you either need to subsequently call Restart(), or make a change to the channel list from the Workspace.