Additional Example 4: Merging Date and Time Channels
- Updated2024-09-12
- 4 minute(s) read
DataPlugin > Examples > Additional Example 4: Merging Date and Time Channels
Additional Example 4: Merging Date and Time Channels
The following example shows a DataPlugin for reading in the text file DataPlugin_AdditionalEx4.aex4. The DataPlugin script file is located at DataPlugin_AdditionalEx4.vbs
Data Format Description
The text file contains only channel data: one channel with date and time data and two numeric channels. The tilde character separates the channel data. A point is the decimal symbol for the specified numbers. The numbers do not have a thousand separator. Leading and trailing blanks are irrelevant. The end of the row is indicated by Carriage Return and Line Feed.
Special Features of the DataPlugin
-
Line feed: CrLf
-
Ignoring spaces
-
Separators: Tilde
-
Decimal symbol: Point
-
Reading in channel data. The date and time information is stored together in one channel.
-
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.Delimiters = "~" oFile.Formatter.DecimalPoint = "." oFile.Formatter.ThousandSeparator = "" ' Create channel group object Dim oChannelGroup : Set oChannelGroup = Root.ChannelGroups.Add(oFile.Info.FileName) ' Declaration for channel and add properties Dim oChn : Set oChn = oChannelGroup.Channels.Add("Time", eTime) ' Import the time channel with offset 2000 for year ' needs to be value by value because offset must be calculated Dim iLoop : iLoop = 1 Dim oTime : Set oTime = CreateTime(2014,6,17,10,20,30,40,45,50) Dim sTemp Do While not oFile.Position = oFile.Size sTemp = Trim(oFile.GetNextStringValue(eString)) sTemp = sTemp & " " & Trim(oFile.GetNextStringValue(eString)) Call CreateDate(sTemp, oTime) oChn.Values(iLoop) = oTime.VariantDate oFile.SkipLine iLoop = iLoop + 1 Loop ' Jump back to begin oFile.Position = 1 ' Get object for block stored data Dim oBlock: Set oBlock = oFile.GetStringBlock() ' This channel must be ignored for the channel group Set oChn = oBlock.Channels.Add("Dummy1", eString) Set oChn = oBlock.Channels.Add("Dummy2", eString) ' Import all other channels with fast access Set oChn = oBlock.Channels.Add("Channel_1", eR64) Call oChannelGroup.Channels.AddDirectAccessChannel(oChn) Set oChn = oBlock.Channels.Add("Channel_2", eR64) Call oChannelGroup.Channels.AddDirectAccessChannel(oChn) End Sub '----------------------------------- ' Creates the date channel with the offset for year 2000 Sub CreateDate(sTxt, oReturnedDate) Dim iY, iM, iD, iH, iMin, iSec iD = CInt(Left(sTxt, 2)) iM = GetMonthNo(Mid(sTxt, 4, 3)) iY = CInt(Mid(sTxt, 8, 2)) iH = CInt(Mid(sTxt, 11, 2)) iMin = CInt(Mid(sTxt, 14, 2)) iSec = CInt(Mid(sTxt, 17, 2)) Set oReturnedDate = CreateTime(2000 + iY, iM, iD, iH, iMin, iSec, 0, 0, 0) End Sub '----------------------------------- ' Creates month as number Function GetMonthNo(sInput) Select Case UCase(sInput) Case "JAN" GetMonthNo = 1 Case "FEB" GetMonthNo = 2 Case "MAR" GetMonthNo = 3 Case "APR" GetMonthNo = 4 Case "MAY" GetMonthNo = 5 Case "JUN" GetMonthNo = 6 Case "JUL" GetMonthNo = 7 Case "AUG" GetMonthNo = 8 Case "SEP" GetMonthNo = 9 Case "OCT" GetMonthNo = 10 Case "NOV" GetMonthNo = 11 Case "DEC" GetMonthNo = 12 Case Else GetMonthNo = 0 End Select End Function
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 spaces which are not within the text. The tilde character separates single values. A point is the decimal symbol.
The DataPlugin then creates a new channel group, whose name is the same as that of the file to be read in and also a new channel named Time.
The date and time data is imported in a loop structure. The While loop runs over all the lines of the text file and uses the GetNextStringValue function to read in the respective date value and time value. The DataPlugin connects the read in values, executes an offset correction, and saves the new value in the Time channel.
In the next step, the DataPlugin positions the file pointer at the beginning and transfers the entire file contents to the 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 limits of the value are specified by the Delimiter property, which in this case is the tilde. The DataPlugin uses wildcards for the two channels within the StringBlock because the date and time information is already read in. The DataPlugin creates a new DirectAccess channel for the remaining channels and assigns these channels to the new channel group.
The CreateDate function defines the offset correction and the GetMonthNo function converts the name of the month into a numeric value.