![]() |
|||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
|
Automated Tasks, the use of scheduled execution of Scripting within your environment 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 Next, we now need to log this data to a text file:
Const FOR_WRITING = 2 You can simply join the two tables together to form a single script file. What if the file is locked? 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
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
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. |
|||||||||||||||||||||||||||||||||||||