W&T connects
Interfaces for TCP/IP, Ethernet, RS-232, RS-485, USB, 20mA, glass and plastic fiber optic cable, http, SNMP, OPC, Modbus TCP, I/O digital, I/O analog, ISA, PCI

Application for the Web-IO Digital:

Automated switching with VBScript and batch jobs


In many smaller switching tasks it is not worth the effort to create special application software or purchase expensive automation tools. Here it’s enough to initiate the desired switching operation by simply clicking on an incon or entering a command. By using the Web-IO Digital and short VBScripts you can implement such solutions with just a few lines of commands.

Automatic switching with VBScript

VBScript is an easy-to-program interpreter language supported by Windows 2000, XP and Vista. As the name (Visual Basic Script) implies, the syntax of VBScript is derived from Visual Basic. No special development environment is required to create VBScripts - all you need is a text editor. We recommend the free PSPAD editor (www.pspad.com), which has syntax coloring for VBScript. When saving VBScripts the file extension ".vbs" is used.

The examples below show how VBScript and the Web-IO interact.

You don’t have a Web-IO® yet but would like to try the example out sometime?

No problem: We will be glad to send you the Web-IO Digital 2xInput, 2xOutput at no charge for 30 days. Simply fill out a sample ordering form, and we will ship the Web-IO for testing on an open invoice. If you return the unit within 30 days, we will simply mark the invoice as paid.

To sample orders

Preparations

You have already provided your Web-IO Digital

1. Set an output using VBScript

When the following script is invoked, Output 0 on the Web-IO having IP address 10.40.22.101 is set.


							' VB Script Document
							option explicit

							Dim objHttp
							Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest.5.1")
							If objHttp Is Nothing Then Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest")
							objHttp.Option(4) = 256 + 512 + 4096 + 8192
							objHttp.SetTimeouts 0, 5000, 10000, 10000
							objHttp.Open "GET", "http://10.40.22.101/outputaccess0?PW=&State=ON&",
							FALSE
							objHttp.setRequestHeader "User-Agent",
							WScript.ScriptName
							objHttp.Send ""
							If Not (objHttp.statusText = "OK")
							Then
								WScript.Echo "Error: " & objHttp.statusText
								WScript.Quit 1
							Else
								WScript.Echo objHttp.ResponseText
							End If
						

The core of this script is the WinHttp object, which is used to send commands to the Web-IO. The actual command is passed using objHttp.Open.

http://10.40.22.101/outputaccess0?PW=&State=ON& specifies that Output 0 on Web-IO 10.40.22.101 is to assume the ON state

objHttp.ResponseText reads in the response from the Web-IO. For command outputaccess the Web-IO returns a string consisting of the word output, a semicolon and the output status.

Example output;1

A detailed description of the possible Web-IO commands can be found in the manual for the Web-IO starting on page 102.

2. Invoking VBScripts from batch jobs

Batch jobs under Windows were originally intended for automatically running Windows commands. A classic example is the Autoexec.bat in older DOS environments. A list of commands is entered in the batch file which are processed sequentially when the list is opened.

In Windows systems this means you can automate scripts and the opening of programs. If for example you want to switch outputs on different Web-IOs with a call, this can also be done using a batch job.

The corresponding names of the VBScripts need to be written below each other in the batch file. In the case of scripts like the one above, you need to create a separate script for each switching operation.

This is why in batch jobs it makes more sense to create a universally usable script which when it is invoked is told using additional parameters what the switching task consists of. In the following VBScript you can pass these parameters to it when invoked:

IP IP address of the Web-IO
PORT TCP port of the Web-IO This parameter is optional; if it is not transmitted, the script uses Port 80
PW Administrator or operator password for the Web-IO This parameter is optional; if it is not transmitted, the script runs without a password
MASK indicates in hex format which outputs are supposed to be set This parameter is optional; if it is not transmitted, the script runs using all outputs
STATE indicates in hex format to which state the outputs are supposed to be set

The script invocation looks as follows:

setoutput.vbs IP=<IP address> [PORT=<portno.>] [PASSWORD=<password>] [MASK=<hex value>] STATE=<hex value>

								' VB Script Document
								option explicit

								Dim objArgs, strArg, strArgall
								Dim IP, PORT, PASSWORD, MASK, STATE, URLStr
								Dim objHttp
								Set objArgs = WScript.Arguments

								'# Make sure that script starts as console application (best way "cscript //h:cscript")
								Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell")
								If Right(WScript.FullName, Len(WScript.Fullname) - Len(WScript.Path) -1) _ = "WScript.exe"
								Then
								For each strArg in objArgs
									strArgall = strArgall & " " & strArg
								next
								WshShell.Run "cmd /k cscript " & Chr(34) & WScript.ScriptFullName _ & Chr(34) & strArgall
								WScript.Quit
								End If

								'# Check if there are Parameters
								If WScript.Arguments.count < 1 Then
								WScript.Echo "***********************************"
								WScript.Echo "* Not enough arguments *"
								WScript.Echo "***********************************"
								WScript.Echo ""
								WScript.Echo "Syntyx: setoutput.vbs
								IP=<IP address> [PORT=<portno.>] _

								[PASSWORD=<password>] [MASK=<hex value>] STATE=<hex value>"
								WScript.Quit
								End If

								'# Read the Parameters
								for each strArg in objArgs
								If Left(strArg,3) = "IP="
								then
									IP = Mid(strArg, 4, Len(strArg) - 3)
								End If

								If Left(strArg,5) = "PORT="
								then
									PORT = Mid(strArg, 6, Len(strArg) - 5)
								End If

								If Left(strArg,9) = "PASSWORD="
								then
									PASSWORD = Mid(strArg, 10, Len(strArg) - 9)
								End If

								If Left(strArg,5) = "MASK="
								then
									MASK = Mid(strArg, 6, Len(strArg) - 5)
								End If

								If Left(strArg,6) = "STATE="
								then
									STATE = Mid(strArg, 7, Len(strArg) - 6)
								End If
								Next

								'# Mount the command String
								If IP <> ""
								then
								URLStr = "http://" & IP
								else
								WScript.Echo "***********************************"
								WScript.Echo "* Not enough arguments : IP *"
								WScript.Echo "***********************************"
								End If

								If PORT <> ""
								then
								URLStr = URLStr & ":" & PORT
								End If

								URLStr = URLStr & "/outputaccess?PW=" & PASSWORD & "&"

								If MASK <> ""
								then
								URLStr = URLStr & "Mask=" & MASK & "&"
								End If

								If STATE <> "" then
								URLStr = URLStr & "State=" & STATE & "&"
								else
								WScript.Echo "***********************************"
								WScript.Echo "*  Not
								enough arguments : STATE   *"
								WScript.Echo "***********************************"
								End If

								'# Send the Command String via HTTP object
								Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest.5.1")
								If objHttp Is Nothing Then Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest")
								objHttp.Option(4) = 256 + 512 + 4096 + 8192
								objHttp.SetTimeouts 0, 5000, 10000, 10000
								objHttp.Open "GET", URLStr,
								FALSE
								objHttp.setRequestHeader "User-Agent",
								WScript.ScriptName
								objHttp.Send ""
								If Not (objHttp.statusText = "OK")
								Then
								WScript.Echo "Error: "& objHttp.statusText
								WScript.Quit 1
								else
								WScript.Echo objHttp.ResponseText
							End If
						

After the selected action has been performed, the script returns the status of the outputs.

e.g. output:0100

The status message consists of the word output, a semicolon and the output status in hex format.

TIP: The Windows Script Host (WSH) is responsible for running VBScripts; WSH is present in two variants on every Windows PC. The variants Wscript and Cscript differ mainly in how the data are output. Unless otherwise configured, the scripts are processed using Wscript, and text messages are output in a Windows dialog box. The disadvantage to this is that the user has to acknowledge each message in order for the script to continue. This type of processing is undesirable for use in batch jobs.

By entering the command wscrit //H:cscript, CScript is defined as the standard Script Host. CScript returns all messages in a DOSBOX and does not wait for confirmation.

The script shown above checks which Script Host is active, cancels if WScript is active and restarts using CScript.

Still, to ensure that the script can run faster, it is recommended that you switch to CScript from the outset.

Additional programming examples for socket programming can be found on the tool pages for the Web-IO. A detailed description for the socket interface of the Web-IO Digital models can be found in the reference manual.

Download program example

You don’t have a Web-IO® yet but would like to try the example out sometime?

No problem: We will be glad to send you the Web-IO Digital 2xInput, 2xOutput at no charge for 30 days. Simply fill out a sample ordering form, and we will ship the Web-IO for testing on an open invoice. If you return the unit within 30 days, we will simply mark the invoice as paid.

To sample orders