Additional Example 1b: Reading in an Unspecified Number of Channels
- Updated2024-09-12
- 3 minute(s) read
DataPlugin > Examples > Additional Example 1b: Reading in an Unspecified Number of Channels
Additional Example 1b: Reading in an Unspecified Number of Channels
The following example shows a DataPlugin for reading in the text files DataPlugin_AdditionalEx1_1.aex1 and DataPlugin_AdditionalEx1_2.aex1. The DataPlugin script file is located at DataPlugin_AdditionalEx1b.vbs
Data Format Description
These two text files contain metadata and channel data. The metadata includes the channel name and the channel unit. The channel names are in the first line of the file to be read in and the associated units are in the second line. The channel data follows:
The names, units, and data are separated by a tabulator. The specified numbers use a point as the decimal symbol. The empty rows in the file and the leading and trailing blanks are irrelevant. The end of the line is indicated by Carriage Return and Line Feed.
The DataPlugin_AdditionalEx1_1.aex1 file contains specifications for 2 channels and the DataPlugin_AdditionalEx1_2.aex1 file specifications for 3 channels.
Special Features of the DataPlugin
-
Line feed: CrLf
-
Separators: <Tab>
-
Ignoring spaces and empty rows
-
Using the Split function to read in the channel names and the channel units. You do not need to know the number of channels to be read in before.
-
Using DirectAccess channels to read in channel data
Option Explicit Sub ReadStore(oFile) ' Set string formatter oFile.Formatter.LineFeeds = vbCrLf oFile.Formatter.TrimCharacters = " " oFile.Formatter.IgnoreEmptyLines = True oFile.Formatter.Delimiters = vbTab oFile.Formatter.DecimalPoint = "." ' Read channel header Dim saChannelName : saChannelName = Split(oFile.GetNextLine, vbTab) Dim saChannelUnit : saChannelUnit = Split(oFile.GetNextLine, vbTab) ' Create channel group object and set property Dim oChannelGroup : Set oChannelGroup = Root.ChannelGroups.Add(oFile.Info.FileName) ' Get object for block stored data Dim oBlock : Set oBlock = oFile.GetStringBlock() ' Import all other channels with fast access Dim iLoop : iLoop = 0 Dim oChn, oElem For each oElem in saChannelName ' Create channel in string block (file) Set oChn = oBlock.Channels.Add(oElem, eR64) Call oChn.Properties.Add("Unit_String", saChannelUnit(iLoop)) ' Add defined channel to DIAdem channel group Call oChannelGroup.Channels.AddDirectAccessChannel(oChn) iLoop = iLoop + 1 Next End Sub
DataPlugin Description
The DataPlugin first specifies the general format of the file through the File object. The DataPlugin uses the VBS constant vbCrLf as the word-wrap and ignores empty rows and spaces which are not within the text. The VBS constant vbTab separates single values. A point is the decimal symbol.
Then the DataPlugin reads in the descriptions of the channels. The first line of the file contains the names of the channels and the second line the corresponding units. The DataPlugin uses the Split function to check the read in data for the vbTab separator and stores the substrings in a one-dimensional, zero-based array. The number of arrays specifies the number of channels that the file contains.
Then the DataPlugin generates a new channel group whose name corresponds to the name of the file to be read in.
The DataPlugin transfers the other file contents into a StringBlock. If DirectAccess channels are associated with a StringBlock, the order of the values in a row determine which value belongs to which channel. The first value of the row belongs to the first channel, the second value of the row belongs to the second channel and so on. The value limits are specified by the Delimiter property, which in this case is vbTab.
The channel data is imported in a loop structure. The number of loops is specified by the size of the array which contains the channel names. Within the loop, the DataPlugin generates a new DirectAccess channel with the title that was previously read in and the associated unit. Because channels always belong to one channel group, the DataPlugin assigns the channels to the new channel group.