Automation and Scripting
Home Product
Details Free 
Demo Pricing &
Ordering Related
Products Support About Us

Back to all documents

Automated Tasks, the use of scheduled execution of Scripting within your environment

Introduction

    Sometimes you simply do not need an application to stay open and continually run, or stay open and sleep for the majority of time before doing anything. Sometimes you just need a small application that will run on a schedule.

    The OPC Web Client, because it is so versatile, does give you this possibility! With a little bit of Windows Shell scripting and the us of a scheduler, you can automate tasks quickly and easily, and this document will show you how...

Intended Audience

    This paper is intended for Administrators and Developers.

Scheduling

    There are many scheduling applications out there. In a world of Windows computers, there is a scheduler already available that is often overlooked... "AT".

    AT is a command-line utility that allows you to specify an application to run, and a time in which to run it. AT can be used to schedule a script to run also as it is not distinguished any differently to an application.

    AT can be used to schedule "something" to run once! or to run on a regular/cyclical basis.

    It is beyond the scope of this paper to detail every feature of the AT command. Our advice is to read the Help on this command or seek the documentation from Microsoft. For help, simply open a Command Prompt and type:

      AT /?

    ... followed by ENTER.

    Once you have defined your application and schedules, you are set!

Using Scripts for Automation

    Windows Shell Scripting (filenames are suffixed with .VBS extension) allows you to create scripts using VB. There is no need for a development environment, no compilation and complication! Simply create a new text file and edit it using NotePad or any other ASCII text editor. Simply save the file with a ".VBS" extension and you have just created a Windows Shell Script that you can run like a Batch-file/executable!

A Typical Case Scenarios for Automated Scripts

    There are many use-cases where automated scripts can be of use. However, one use-case stands out as the one that is most commonly needed

I need to be able to read a tag (or some tags) at midnight and to dump those values into a specific (or unique) text file.

After I have captured and logged this data, another application will read the values from that/those file(s).

    This would've been previously been accomplished via a dedicated application running all the time. The application would be asleep the majority of the time only to wake-up at night and log the data to a file before going back to sleep. This is not needed anymore!

A Step-By-Step Example Of a Scripted Data Logging Application

    Defining "the problem"

      In this example we are going to log a single value of a Tag to a text file named "mydata.txt". This application will need to run at midnight, every night.

    Defining the Script

      There are many scripts available in our source-code section.

      Let's see how a script might look in our example. We are going to use the "Greenhouse" OPC Server that is installed with the OPC Web Client . We are going to log a single line of data as follows:

      TAG:<tagname>;VALUE:<value>;QUALITY:<quality>;TIMESTAMP:<timestamp>

      Let's begin by first, reading our Tag's value:

Option Explicit

Dim EasyDA
Set EasyDA = CreateObject("OPCLabs.EasyDA3.0")

Dim strOPCServer
strOPCServer
= "OPCLabs.KitServer.2"

Dim strTag
strTag
= "Simulation.Random"

Dim VTQ
Set VTQ = EasyDA.ReadItem("", strOPCServer, strTag)

      Next, we now need to log this data to a text file:

Const FOR_WRITING = 2
Dim strFileName
strFileName
= "C:\OPC\MyData.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, FOR_WRITING, True)

Dim strWriteLine
strWriteLine
= "TAG:" & strTag & ";VALUE:" & VTQ.Value & ";TIMESTAMP:" & VTQ.TimeStamp

objFile.WriteLine(strWriteLine)
objFile.Close

      You can simply join the two tables together to form a single script file.
      This script will make a connection to the OPC Server, read a Tag value, write it to a file and then close the file and exit the script.

      What if the file is locked?
      What if the OPC Server is not available?
      What about error trapping?

      These are questions that I'm not going to go into here. There are some very obvious things that you can do, but you are at the mercy of VBScript's infamous:

      On Error Resume Next

      You do have to assume errors will arise, and you do have to plan for them. A pseudo paradigm for handling errors in this scripted-example may be:

On Error Resume Next

Make the connection to the OPC Server

If no error occurred then:

    Try to read the Tag

    If no error occurred then:

      Try to open/create the text file

      If no error occurred then:

        Write the Tag's value to the file

        Close the file

      Our script is complete!

      SAVE THE SCRIPT - lets call our script "MidnightRun.vbs"

    Creating the scheduled event

      As mentioned previously, we are going to use the "At" command which is available in all fo the NT-Based OS's. Simply open an MS-DOS/Command-Prompt window and type the following:

        AT /?

      ... followed by ENTER.

      The following information should be shown:

The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.

AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
   [ /EVERY:date[,...] | /NEXT:date[,...]] "command"

\\computername     Specifies a remote computer. Commands are scheduled on
                   local computer if this parameter is omitted.
id                 Is an identification number assigned to a scheduled
                   command.
/delete            Cancels a scheduled command. If id is omitted, all the
                   scheduled commands on the computer are canceled.
/yes               Used with cancel all jobs command when no further
                   confirmation is desired.
time               Specifies the time when command is to run.
/interactive       Allows the job to interact with the desktop of the user
                   who is logged on at the time the job runs.
/every:date[,...]  Runs the command on each specified day(s) of the week o
                   month. If date is omitted, the current day of the month
                   is assumed.
/next:date[,...]   Runs the specified command on the next occurrence of th
                   day (for example, next Thursday). If date is omitted,
                   current day of the month is assumed.
"command"          Is the Windows NT command, or batch program to be run.

      We are going to specify that our script is going to run at midnight, every night!

      Enter the following AT command:

AT 12:00:00am /every:Monday,Tuesday,Wednesday,Thursday,Friday MidnightRun.vbs

      ... then press ENTER.

      Hopefully, "nothing will happen" meaning you will not see an error or the Help screen.

      To see what scheduled events are defined, simply type in the following:

At

      ... then press ENTER.

      Information similar to the following should show:

Status ID  Day                    Time         Command Line
-------------------------------------------------------------------------------
       1  Each M T W Th F        12:00 AM     MidnightRun.vbs

Summary

    The purpose of this paper was to show you how to automate tasks using a little Windows Shell Scripting along with the scheduling abilities of the Windows AT command.

    Hopefully I have explained and shown how easy it is to use the OPC Web Client in a simple and versatile manner such as this.

Copyright Software Toolbox, Inc., 1996-2004, All Rights Reserved Worldwide.
148A East Charles Street, Matthews, North Carolina, USA 28105
Phone: 704-849-2773 or 1-888-665-3678 (US), Fax: 704-849-6388
sales@softwaretoolbox.com | support@softwaretoolbox.com