Accessing text files via E3.

1) Introduction

Sometimes, you will need to follow the details of the values of an application’s data: to check whether the data already has the expected value or when a script’s excerpt is not acceptable, to know how far the script has gone or where the error is, to verify the return value of a certain function, etc. By generating our own logs, we can easily monitor these items. To create these logs, the best tools are the functions for creating and accessing text files, which are supported by VBScript, available at E3.

To illustrate how this item works, we will show you how to generate the log of an OPC communication, where E3 will work as both the server and the client at the same time.

2) Development

Creating the Project

When you start E3 Studio, choose the option Create new project, and then Standard Application. Name your project TrainingTXT and choose a folder to save the application. Create a new domain with the same name as the project. Answer No to the next three questions on screen.

At Data folder, create an internal tag and a demo tag, which will work as the data to be transferred via OPC. Increase the demo tag’s Scan to  10000. Star the domain to initialize the OPC Server.

To set up the application as OPC Client, insert an OPC driver into the project. At the driver’s properties, access OPC Driver tab and search, via Select button, for E3 OPC Server (Elipse.OPCSvr.1) in Local Servers.  To enable the import of tags created in the project, activate the communication.


Figure 1: Select OPC Server

Return to the Organizer, right-click OPCDriver, and choose the option Import Tags. E3 will ask you if you want to create a group to continue the import process, and then if you would like to detect the available OPC tags; click Yes on both questions.

The OPC Tags Import window will be opened. On the right side, inside Data folder, select DemoTag1 and drag it to OPCGroup1 to the left. Do the same with InternalTag1. The result is illustrated in the figure below.


Figure 2: Tags import

Set up the InitialScreen similarly to what is on Figure 3. Link the two displays to the left to the OPC tags’ Value property, and the setpoint to the right to the InternalTag’s Value property. Enable MultiBox’s MultiLine property (TRUE).


Figure 3: Initial Screen

To check whether the application is working, execute the project before the scripts for writing in the file are implemented. On InitialScreen, the values of the internal tag and the OPC tags must be the same. Change these values in a setpoint, and then repeat them in another setpoint.

Scripts

Once it has been confirmed that the data are correctly being sent and received via OPC protocol, the scripts for reading and writing in text files will be implemented.

The first item to be logged to files is the OPC server identification, as soon as it is initialized. This will happen after the domain has been executed. The server’s ID and Name properties, as well as the name of the OPCDriver object in E3, will be at the beginning of the file.

To do so, add the following script to the OPCDriver’s OnStartRunning event:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim fso, f
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(“C:\LogOPC.txt”, ForWriting, True)

f.WriteLine Name
f.WriteLine CStr( “ServerID: ” & ServerId )
f.WriteLine CStr( “ServerName: ” & ServerName )
f.WriteLine

f.Close

To have a new line added to the log file at every variation in value of the OPC tags, create a new event called Change in each OPC tag and link it to Value property. Figure 4 illustrates how to implement these events.


Figure 4: Creating new events

After these new events have been created, insert the code below in each one of them. Since this is a developed script, you can only copy the code from one event to another, with no need to change it.

set Valor = Item(1) ‘Points to tag’s Value property
DateTime = Valor.TimeStamp
Grupo = Parent.Name

‘ Adds new lines in the log file
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f

Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(“C:\LogOPC.txt”, ForAppending, False)

‘ Event Time Group Variable name Value
f.WriteLine DateTime & ” ” & Group & ” ” & Name & ” ” & Value
f.Close

To allow visualizing the generated file, a script will be created for reading it. To do so, insert the code below into InitialScreen‘s button’s Click event:

Const ForReading = 1, ForWriting = 2

Dim fso, f
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(“C:\LogOPC.txt”, ForReading)

Screen.Item(“TextBox1”).Value = f.ReadAll
f.Close

Execute the application and check the result of these implementations.

3) Final Remarks

Usually, the use of log files helps locating errors in developed applications, especially when it comes to objects that are executed in the server, such as tags, historics, etc. For such purposes, an interesting tool available in VBScript language is the access to these files via FileSystemObjects, which have a powerful set of methods for reading and writing in files. The complete documentation of these objects, as well as other examples of their usage, is in the VBScript tutorial that comes along with E3’s installation.

 

Attachments:

Sample.zip

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *