: Parsing Text Files in Application Server

 

Wonderware’s Industrial Application Server provides several sample scripts that perform common operations. One of the samples provided is an example of reading from a text file. (Search: Read a Text File from Disk) which is useful in pulling information in from external systems.

 

Comma Separated Value files (.CSV) are a very common method for transferring information. This sample object builds on that script example and shows one method to parse a delimited text file into the individual components.

 

NOTE:  This article and all content are provided on an "as is" basis without any warranties of any kind, whether express or implied, including, but not limited to the implied warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall Q-mation be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortuous action, arising out of or in connection with the use or performance of information contained in this article.

 

$qFileReader:

 

It is good practice to create Derived Templates from the version that is supplied during installation. This allows you to maintain independent configuration for this specific application. The $qFileReader was derived from the $UserDefined template and added the file access functionality.

 

NOTE: The attributes that are locked will not be able to be changed when the instance is created.

 

Object Information Settings:

 

  • The $qFileReader is a working example that will read from a "single character" (,) delimited file.

  • The Object Information is basic information about the object and can be updated at the instance level.

 Scripts Tab:

 

 

There are a couple of scripts that were added to access the file. These scripts are detailed below:

 

Name:

initialize

Desc:

This script can be used to initialize any values, like building the reference to the file name.

Script Info:

Script:

' NOTE: Insert scripting to dynamically build filename here

 

 

' BEGIN -- Build sample file for testing

'dim sw as System.IO.StreamWriter;

'dim filename as String;

'dim myRandom as System.Random;

'dim RandInt as Integer;

 

'myRandom = New System.Random();

'RandInt = myRandom.Next( 100, 999 );

 

'filename = me.cfgFilename;

'sw = System.IO.File.CreateText(filename);

'sw.WriteLine(me.Tagname + me.cfgDelimiter + StringFromIntg( RandInt, 10) );

 

'sw.Close();

' END

 

' Set Initialized status

me.objInitialized = 1;

 

 

 

Name:

readFile

Desc:

This script will read from the text file and fill the split[] array with field values. The loop will continue until the end of the file.

Script Info:

Script:

dim sr as System.IO.StreamReader;

dim rowValues as String;

' NOTE: Set array index to max number of columns

dim split[60] as String;

dim delimiter as string;

dim s As String;

dim filename as String;

 

filename = me.cfgFilename;

delimiter = me.myDelimiter;

 

sr = System.IO.File.OpenText(filename);

 

while sr.Peek() > -1

 

rowValues = sr.ReadLine();

LogMessage(rowValues);

 

' NOTE: Set second .Split() argument to max number of columns

split[] = rowValues.Split(delimiter.toCharArray(), 60);

for each s in split[]

 

LogMessage(s);

 

next;

 

endwhile;

 

sr.Close();

 

me.cmdReadLine = 0;

 

 

 

Name:

readTrigger

Desc:

This script can be used to programmatically trigger the file to be read.

Script Info:

Script:

me.cmdReadLine = 1;

 

 


UDAs Tab:

 

 

Here are the details around the UDAs that are added:

 

Name

Data Type

Category

Array

Initial Value

cfgDelimiter

String

User Writeable

No

,

cfgFilename

String

User Writeable

No

* C:\IASTest.csv

cmdReadLine

Boolean

User Writeable

No

Off/False

objInitialized

Boolean

User Writeable

No

Off/False

 

* The File Name should either be set at the instance level or built and assigned in the initialize script.


Extensions Tab:

 

 

The Extensions tab can be used if you would like to Alarm or Historize any of in added UDAs. Implementing this is optional.

To learn more, feel free to contact our technical support team!

 

 

©2003 Q-mation, Inc. All rights reserved. All trademarks are the property of their respective owners.