Notice: This page contains information for the legacy Phidget21 Library. Phidget21 is out of support. Bugfixes may be considered on a case by case basis. Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21. We recommend that new projects be developed against the Phidget22 Library.
|
Language - Visual Basic Script: Difference between revisions
Line 23: | Line 23: | ||
===Use Our Examples=== | ===Use Our Examples=== | ||
We provide Hello World examples for both Internet Explorer (IE) and for Windows Script Host (WSH). | |||
===Write Your Own Code=== | ===Write Your Own Code=== | ||
Line 70: | Line 72: | ||
The object name for any type of Phidget is listed in the API manual. | The object name for any type of Phidget is listed in the API manual. | ||
Every type of Phidget also inherits functionality from the Phidget base class. | Every type of Phidget also inherits functionality from the Phidget base class. | ||
==Follow The Examples== | ==Follow The Examples== |
Revision as of 22:27, 23 May 2012
VBScript (Visual Basic Scripting Edition) is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. |
Introduction
If this is your first time working with a Phidget, we suggest starting with the Getting Started page for your specific device. This can be found in the user guide for your device. That page will walk you through installing drivers and libraries for your operating system, and will then bring you back here to use VBScript specifically.
VBScript is capable of using the complete Phidget API, including events. We also provide example code in VBScript for Encoder, InterfaceKit and RFID.
VBScript can be developed with various script editors, although the programs themselves only run in Internet Explore (IE).
You can compare VBScript with our other supported languages.
Quick Downloads
Just need the VBScript documentation, drivers, libraries, and examples? Here they are:
Documentation
Example Code
Libraries and Drivers
- 32-bit Windows Drivers Installer
- 64-bit Windows Drivers Installer
- Windows Driver and Library Files (Zipped)
Getting started with VBScript
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
- Make sure your libraries are properly linked
- Go from source code to a test application as quickly as possible
- Ensure your Phidget is hooked up properly
Visual Basic script is only supported under Windows 2000 / XP / Vista / 7, and then for use only under Internet Explorer.
Windows (2000/XP/Vista/7)
Use Our Examples
We provide Hello World examples for both Internet Explorer (IE) and for Windows Script Host (WSH).
Write Your Own Code
To begin, load the HTML editor of your choice and create a new document. Add a table to body of the code for the purpose of displaying simple output.
<html>
<head>
<body>
<form action=”html_interfacekit.htm”>
<table id=”mytable”>
<thead>
<tr><td>Analog Input</td></tr>
</thead>
<tr id=”tr0”><td>0</td> </tr>
<tr id=”tr1”><td>0</td> </tr>
<tr id=”tr2”><td>0</td> </tr>
<tr id=”tr3”><td>0</td> </tr>
<tr id=”tr4”><td>0</td> </tr>
<tr id=”tr5”><td>0</td> </tr>
<tr id=”tr6”><td>0</td> </tr>
<tr id=”tr7”><td>0</td> </tr>
</table>
</form>
</body>
</html>
Before you can use the Phidget, you must include its Class ID on object creation in your code. This will let the browser know which library to try and load for the Phidget definitions. Each type of Phidget has its ClassID listed inside the ClassID_List.txt document from the VBScript examples. For example, we can declare and create a PhidgetInterfaceKit for vbscript with:
<object classid="clsid:50484945-4745-5453-3000-000000000003" id="phid"></object>
<script type="text/vbscript">
<!--Your Code goes here-->
</script>
The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.
Follow The Examples
Code Snippets
Step One: Initialize and Open
The program can try to connect to the Phidget through an open call. Open will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. This means that simply calling open does not guarantee you can use the Phidget immediately. We can account for a connection by using event driven programming and tracking the AttachEvents and DetachEvents, or by calling WaitForAttachment. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
phid.Open()
phid.WaitForAttachment (3000)
The different parameters and open calls can be used to open the first Phidget of a type it can find, open based on a serial number, or even open across the network. The API manual lists all of the available modes that open provides. One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget Webservice.
Step Two: Wait for Attachment (plugging in) of the Phidget
Step Three: Do Things with the Phidget
We recommend the use of event driven programming when working with Phidgets. In VBScript, we hook an event handler with the following code:
Sub phid_OnSensorChange(ByVal Index, ByVal SensorValue)
mytable.rows("str"+CStr(Index)).cells(0).innerText = Cstr(InterfaceKit1.sensorValue(Index))
End Sub
With this method, the code inside onSensorChange will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets. Please refer to the API manual for a full list of events and their usage.
Some values can be directly read and set on the Phidget, and inside polling loops used as an alternative to event driven programming. Simply use the instance properties such as phid.SensorChangeTrigger(Index) for PhidgetInterfaceKits.
phid.SensorChangeTrigger(0) = 1
Step Four: Close and Delete
More How-To's
The General Phidget Programming page gives more information about:
- Using Multiple Phidgets (or a Phidget other than the Interface Kit)
- Catching exceptions and errors and using logging
- Event catching versus direct polling
- And more....
Common Problems and Solutions/Workarounds
None at this time.