Alert.png

Notice: This page contains information for the legacy Phidget21 Library. Phidget21 does not support VINT Phidgets, and will not support any new Phidgets. Phidget21 will be maintained until 2020. We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Language - Visual Basic Script

From Phidgets Legacy Support
Revision as of 21:28, 27 March 2012 by Djrudiak (Talk | contribs)

Jump to: navigation, search

Icon-Visual Basic Script.png Preamble about the language's origin and its main characteristics.

Support

Visual Basic Script has a complete API for all Phidgets devices, and code samples for Encoder, InterfaceKit and RFID.

For a complete list of our supported languages and their support status, click here.

  • Our honest opinion on how well this language is suited to controlling Phidgets. If it is a poor choice, suggest and link similar (better) languages.
  • In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.

Development Environments and Compilers

Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.

Quick Downloads

Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. Visit the drivers section at www.phidgets.com and get the latest:

You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:

You may want to have these pages open while working through these instructions.

Getting Started

You will need the ClassID List from the VBScript examples above to program with Phidgets, and the Phidget Framework to use them. The Phidget examples were written using Visual Basic Script for Internet Explorer and this tutorial assumes its use. 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>

Coding For Your Phidget

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.

Connecting to the Phidget

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.

Event Driven Programming

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.

Working directly with the Phidget

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

Working with multiple Phidgets

Multiple Phidgets of the same type can easily be run inside the same program. In our case, it requires another PhidgetInterfaceKit instance to be defined and initialized. The new instance can then be set up, opened and used in the same process as the previous one.

If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.

Other Phidgets

The design given in this document can also be followed for almost all Phidgets. For example, if you were using a PhidgetRFID instead of an PhidgetInterfaceKit, you would use the class ID for a PhidgetRFID instead of a PhidgetInterfaceKit. The methods and events available would change but they can be accessed in a similar manner.

Building your Project

Describe the different ways a project could be built using this language.

Common Problems and Solutions/Workarounds

Issue: Cannot control Phidgets with certain web browsers

Affected Operating Systems: Windows

Certain web browsers such as Firefox and Chrome cannot control Phidgets in Visual Basic Script.

Solution: Only Internet Explorer can control Phidgets with Visual Basic Script.