DIAdem Help

Example 4: Reading in a Text File and Handling Errors

  • Updated2024-09-12
  • 6 minute(s) read

Example 4: Reading in a Text File and Handling Errors

The following example shows you how to create a DataPlugin to read the ASCII file DataPluginExample4.ex4. This text file contains metadata and channel data. Refer to DataPluginExample4.vbs to open the DataPlugin script file.
 

Data Format Description

The text file to be read contains an event log. The log includes a number of time stamps and associated alarm codes. There is a list of the alarm codes and their descriptions, separated by equal signs, at the beginning of the file. An empty line separates this list from the actual log.

The time stamp and the associated alarm codes are separated by semicolons. A time stamp consists of the day, month, year, hour, minute, and second.

Leading and trailing blanks are irrelevant. The end of the line is indicated by Carriage Return and Line Feed.

The following graphic illustrates this format:


 

Configuring a DataPlugin

  1. Select Settings»Extensions»DataPlugins in DIAdem NAVIGATOR.

  2. Click Create DataPlugin to configure a new DataPlugin.

  3. A dialog box opens where you can define the parameters for the new DataPlugin.

    Enter the Name of the DataPlugin, for example, DataPluginExample4.

    Below Filename Extensions, enter a list of file types that the DataPlugin can access, for example, *.ex4.

    Enter the filename, including the path and the filename extension, to specify the script that DIAdem uses to connect the DataPlugin, for example, DataPluginExample4.vbs. Click ... to specify the script in a file selection dialog box. If you enter the name of a script that does not exist, DIAdem generates the script.


    Use Icon to specify the name, including the path and the filename extension, of the symbol file to which DIAdem links the DataPlugin, for example, DataPluginExample4.ico. Click ... to specify the symbol file in a file selection dialog box.

  4. Click OK.

  5. The new DataPlugin is included in the list of DataPlugins. Close the dialog box.

Note  You also can select Settings»Extensions»DataPlugins in DIAdem NAVIGATOR or in DIAdem SCRIPT to import, to export, to modify, or to delete an existing DataPlugin.

You can now start programming the DataPlugin.

Creating the DataPlugin

  1. Open the DataPluginExample4.vbs script in the script editor.
    The script contains an example. USI uses the prepared procedure ReadStore to read the contents of the specified file File. The File parameter contains the opened file, which you selected in the DataFinder.

  2. Delete the contents of the ReadStore procedure until only the following lines remain:

Option Explicit
Sub ReadStore(File)
End Sub

Reading the Alarm Keywords

  1. Outside the ReadStore procedure, define a class that binds the alarm codes to their descriptions. This class contains two dynamic arrays for the alarm codes and their descriptions, and an enumeration variable. The Add method of the object assigns the read text to the respective fields.

Class CAlarmCodeCorrespondence Sub Add(Code, Description)
    Count = Count +1
    ReDim Preserve Codes(Count)
    ReDim Preserve Descriptions(Count)

    Codes(Count) = Code
    Descriptions(Count)= Description
End Sub

Private Sub Class_Initialize
  Count = 0
  End Sub

  Private Codes()
  Private Descriptions()
  Private Count
End Class
  1. Inside the procedure, use the File object to specify the general file format. Use the VBS constant vbNewLine as the linefeed character. Ignore the blank spaces outside the text. Specify the equals sign as the delimiter between texts.

File.Formatter.LineFeeds      = vbNewLine
File.Formatter.TrimCharacters = " " 
File.Formatter.Delimiters     = "="
  1. After the File object is initialized, read the single alarm codes and their descriptions. Use the Add method of the CAlarmCodeCorrespondence object to assign these texts to the respective fields. End reading at the first empty line.

Dim AlarmCode        : AlarmCode       = File.GetNextStringValue(eString)
Dim AlarmDescription : AlarmDescription= File.GetNextStringValue(eString)
Dim AlarmCodeCorrespondence : Set AlarmCodeCorrespondence = New CAlarmCodeCorrespondence

While not(IsEmpty(AlarmCode))
  Call AlarmCodeCorrespondence.Add(AlarmCode, AlarmDescription)
  File.SkipLine()
  AlarmCode       = File.GetNextStringValue(eString)
  AlarmDescription= File.GetNextStringValue(eString)
Wend

Reading the Time Channel

  1. Take the actual event log from the file. To do this, skip the empty line. To read out the channel data, change the delimiter and specify the time format:

File.SkipLine()
File.Formatter.Delimiters = ";"
File.Formatter.TimeFormat = "DD.MM.YYYY hh:mm:ss"
  1. Read the event log with a StringBlock, to which you assign a time channel and a text channel. The time channel receives the time stamps and the text channel receives the alarm codes:

Dim Block : Set Block = File.GetStringBlock()
Dim TimeStampsChn : Set TimeStampsChn = Block.Channels.Add("TimeStamps",eTime)
Dim AlarmCodesChn : Set AlarmCodesChn = Block.Channels.Add("AlarmCodes",eString)
  1. To make the channels available to USI, complete the following steps.
    In a DataPlugin, channels are assigned to a channel group and channel groups belong to a data set. Therefore, create a new channel group first, and then generate the channels.
    You can read the channel that contains the time stamp, directly from the file. However, the descriptions should appear in the text channel instead of the alarm codes. In the channel group, create a text channel for the descriptions.

Dim ChannelGroup : Set ChannelGroup = Root.ChannelGroups.Add("MyChannelGroup")
ChannelGroup.Channels.AddDirectAccessChannel(TimeStampsChn)
Dim AlarmDescriptionChn : Set AlarmDescriptionChn = Channelgroup.Channels.Add("Alarms", eString)

Translating the Alarm Codes into Descriptions

  1. In step 3, you defined the class CAlarmCodeCorrespondence, to relate the alarm codes to the descriptions. Now extend the class to include a function that returns the description for a specific code. If a code does not have a description, the function generates a runtime error:

Function GetDescription(FromCode)
  Dim i
  For i = 1 to Count
    If Codes(i) = FromCode Then
      GetDescription = Descriptions(i)
      Exit Function
    End If
  Next
  Call Err.Raise(42, "DataPluginExample4", "There is no description for the alarm code "&FromCode)
End Function
  1. In the last step, use the GetDescription function you have just defined, to find the description associated with each alarm code, and transfer the description to the text channel. Expand the ReadStore procedure, as shown in the following figure:

Dim i
For i = 1 to AlarmCodesChn.Size
  AlarmDescriptionChn.Values(i) = AlarmCodeCorrespondence.GetDescription(AlarmCodesChn.Values(i))
Next

Creating Error Messages

You cannot load the example data set with your script yet. If the description of an alarm code is not found, the GetDescription function generates a runtime error with Err.Raise and aborts the script.

When Err.Raise is called, detailed information about the error is displayed for the end user, including the number of the line the error occurs in. This information is important for you as the programmer, but it is confusing for the user. For example, if you want to inform the user about a damaged file, use the DataPlugin function RaiseError instead of the VBS function Err.Raise.

  1. Insert the following line before the For loop, so the runtime error does not abort the ReadStore procedure:

On Error Resume Next
  1. After GetDescription is called, check whether an error has occurred. Refer to the Err object for information about the error. Display a message informing the user that an alarm code does not have a description.

If Err = 42 Then
  Dim ErrorString : Errorstring = "The file is corrupt. "  & Err.Description
  Call RaiseError(ErrorString)
End If

If you now load the example data set, this error message appears:"The file is corrupt. There is no description for the alarm code E_UNKNOWN". The data source does not open.

Working with Invalid Values

  1. If the error is not major enough to abort loading, you can delete the runtime error and assign the keyword NULL to the text channel, instead of the alarm description. Replace the If structure with the following statement:

If Err = 42 Then
 Call Err.Clear
 AlarmDescriptionChn.Values(i)= NULL
End If
  1. Save the DataPlugin.

Making Additions to the DataFinder

  1. Select Settings»DataFinders»Properties»DataPlugins in DIAdem NAVIGATOR and enable the DataPluginExample4 DataPlugin.

  2. Click OK to close the dialog boxes.

In the DataFinder you now can navigate in the DataPluginExample4.ex4 file and drag and drop the channels into the DIAdem Data Portal.
 

Overview of the Methods and Properties Used

RaiseError

CreateTime

File.Formatter

Formatter.LineFeeds

Formatter.Delimiters

Formatter.TimeFormat

File.GetStringBlock

StringBlock.Channels

DirectAccessChannels.Add

Root.Channelgroups

Properties.Add

Channelgroups.Add

Channelgroup.Channels

Channels.Add

Channel.Values

DirectAccessChannel.Values

DirectAccessChannel.Size

Log in to get a better experience