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

Difference between revisions of "Language - Visual Basic Script"

From Phidgets Legacy Support
Jump to: navigation, search
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Category:Language]]
 
[[Category:Language]]
 
{{OSLang|[[File:icon-Visual Basic Script.png|64x64px|alt=|link=]]|'''VBScript''' ('''V'''isual '''B'''asic '''Script'''ing Edition) is an [http://en.wikipedia.org/wiki/Active_Scripting Active Scripting] language developed by [http://msdn.microsoft.com/en-us/library/t0aew7h6 Microsoft] that is modeled on [[Language_-_Visual_Basic_6.0|Visual Basic]].}}
 
{{OSLang|[[File:icon-Visual Basic Script.png|64x64px|alt=|link=]]|'''VBScript''' ('''V'''isual '''B'''asic '''Script'''ing Edition) is an [http://en.wikipedia.org/wiki/Active_Scripting Active Scripting] language developed by [http://msdn.microsoft.com/en-us/library/t0aew7h6 Microsoft] that is modeled on [[Language_-_Visual_Basic_6.0|Visual Basic]].}}
==Support==
+
__TOC__
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, [[Levels of Support|click here]].
+
==Introduction==
  
* 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.
+
{{LanguageSupport|VBScript|the complete Phidget API, including events|the Phidget Encoder, Interface Kit and RFID boards.|various script editors, although the programs themselves only run in Internet Explore (IE)|}}
* 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==
+
==Quick Downloads==
 +
{{QuickDownloads|VBScript|
 +
{{APIQuickDownloads|{{SERVER}}/documentation/COM_API_Manual.pdf .COM}}|
 +
{{ExampleQuickDownloads|{{SERVER}}/downloads/phidget21/examples/com/VBScript.zip|}}|
 +
{{WindowsQuickDownloads}}}}
  
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.
+
==Getting started with VBScript==
  
==Quick Downloads==
+
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
 +
{{ExampleCodeReasons}}
 +
 
 +
Visual Basic script is only supported under [[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]], and then for use '''only under Internet Explorer'''.
 +
 
 +
==Windows (XP/Vista/7/8)==
 +
 
 +
The examples are quite easy to run under Windows, all you need is the Phidget libraries installed, and Internet Explorer.
 +
 
 +
===Internet Explorer===
 +
 
 +
When you run Phidget examples, depending on your security settings, Internet Explorer may ask you whether you actually want to run the example or not:
 +
 
 +
[[Image:Vbscript runprogram.png|border|link=|alt=]]
 +
 
 +
At this point you need to select "Run Add On".  This prompt can blend in at the top of the screen, so make sure to look for and approve it once the example is open.
 +
 
 +
===Use Our Examples===
 +
 
 +
We provide Hello World examples for both Internet Explorer (IE) and for Windows Script Host (WSH).  These are simple, high-level examples that work with any Phidget.  We also have a few in-depth examples for our Interface Kit, our Encoder, and our RFID boards.  {{FindYourDevice}}
  
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
+
The HelloWorld example simply prints a message when a Phidget is plugged in (attached) or unplugged (detached).  For example, if we run it and then plug in an Interface Kit, we see:
Visit the drivers section at www.phidgets.com and get the latest:
+
* [http://www.phidgets.com/drivers.php Phidget Framework]
+
You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:
+
* [http://www.phidgets.com/documentation/COM_API_Manual.pdf API Manual]
+
* [http://www.phidgets.com/downloads/examples/VBScript_2.1.8.20110615.zip VBS Sample Code]
+
* You can find a high level discussion about programming with Phidgets in general on the [[General API]] page.
+
* The [[Device Functionality]] page explains the general operational information for your device.
+
  
You may want to have these pages open while working through these instructions.
+
[[Image:Vbscript helloworld.png|border|link=|alt=]]
  
==Getting Started==
+
===Write Your Own Code===
  
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.  
 
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.
 
Add a table to body of the code for the purpose of displaying simple output.
  
 
<div class="source"><syntaxhighlight lang=html4strict>
 
<div class="source"><syntaxhighlight lang=html4strict>
 
 
   <html>
 
   <html>
 
   <head>  
 
   <head>  
Line 55: Line 66:
 
   </body>
 
   </body>
 
   </html>
 
   </html>
 
 
</syntaxhighlight></div>
 
</syntaxhighlight></div>
 
===Coding For Your Phidget===
 
  
 
Before you can use the Phidget, you must include its Class ID on object creation in your code.  
 
Before you can use the Phidget, you must include its Class ID on object creation in your code.  
Line 66: Line 74:
  
 
<div class="source"><syntaxhighlight lang=html4strict>
 
<div class="source"><syntaxhighlight lang=html4strict>
 
 
   <object classid="clsid:50484945-4745-5453-3000-000000000003" id="phid"></object>
 
   <object classid="clsid:50484945-4745-5453-3000-000000000003" id="phid"></object>
 
   <script type="text/vbscript">
 
   <script type="text/vbscript">
 
     <!--Your Code goes here-->
 
     <!--Your Code goes here-->
 
   </script>
 
   </script>
 
 
</syntaxhighlight></div>
 
</syntaxhighlight></div>
  
Line 77: Line 83:
 
Every type of Phidget also inherits functionality from the Phidget base class.
 
Every type of Phidget also inherits functionality from the Phidget base class.
  
===Connecting to the Phidget===
+
==Follow The Examples==
  
The program can try to connect to the Phidget through an open call.  
+
By following the instructions above, you probably now have a working example and want to understand it better so you can change it to do what you want.  This teaching section has resources for you to learn from the examples and write your own.
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.
+
Your main reference for writing VBScript code will be our .COM API information, with syntax for all of our functions:
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.
+
{{UsingAPhidgetInCodeGeneral|both of which are available in VBScript|[{{SERVER}}/documentation/COM_API_Manual.pdf .COM API]}}
 +
 
 +
===Code Snippets===
 +
 
 +
Specific calls in VBScript will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  
 +
 
 +
It may help to have the [[General Phidget Programming]] page and this section open at the same time, because they parallel each other and you can refer to the VBScript syntax.  However, ''many'' additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming.
 +
 
 +
====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:
  
 
<div class="source"><syntaxhighlight lang=vb>
 
<div class="source"><syntaxhighlight lang=vb>
 
 
   phid.Open()
 
   phid.Open()
  phid.WaitForAttachment (3000)
 
 
 
</syntaxhighlight></div>
 
</syntaxhighlight></div>
  
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 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 .COM API manual lists all of the available modes that open provides.  
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===
+
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]].
  
We recommend the use of event driven programming when working with Phidgets.  
+
====Step Two: Wait for Attachment (plugging in) of the Phidget====
In VBScript, we hook an event handler with the following code:
+
 
 +
To use the Phidget, it must be plugged in (attached).  Because we do not know whether it is when our code starts, 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:
 +
 
 +
<div class="source"><syntaxhighlight lang=vb>
 +
  phid.WaitForAttachment (3000)
 +
</syntaxhighlight></div>
 +
 
 +
====Step Three: Do Things with the Phidget====
 +
 
 +
We recommend the use of event driven programming when working with Phidgets. In VBScript, we can hook an event handler with the following code:
  
 
<div class="source"><syntaxhighlight lang=vb>
 
<div class="source"><syntaxhighlight lang=vb>
 
 
 
   Sub phid_OnSensorChange(ByVal Index, ByVal SensorValue)
 
   Sub phid_OnSensorChange(ByVal Index, ByVal SensorValue)
 
     mytable.rows("str"+CStr(Index)).cells(0).innerText = Cstr(InterfaceKit1.sensorValue(Index))
 
     mytable.rows("str"+CStr(Index)).cells(0).innerText = Cstr(InterfaceKit1.sensorValue(Index))
 
   End Sub
 
   End Sub
 
 
</syntaxhighlight></div>
 
</syntaxhighlight></div>
  
 
With this method, the code inside onSensorChange will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
 
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.  
+
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets. Other events, such as this one to read a sensor change, are specific to the type of Phidget (for example, a Temperature Sensor would have a temperature change event). Please refer to the API manual for a full list of events and their usage.
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. For example, simply use the instance properties such as phid.SensorChangeTrigger(Index) for Phidget Interface Kits:
 
+
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.
+
  
 
<div class="source"><syntaxhighlight lang=vb>
 
<div class="source"><syntaxhighlight lang=vb>
 
 
   phid.SensorChangeTrigger(0) = 1
 
   phid.SensorChangeTrigger(0) = 1
 
 
</syntaxhighlight></div>
 
</syntaxhighlight></div>
  
===Working with multiple Phidgets===
+
====Step Four: Close and Delete====
  
Multiple Phidgets of the same type can easily be run inside the same program.
+
If the web page will close via a button, say, and you would like to release use of the Phidget, you can close it via the Close function:
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.
+
<div class="source"><syntaxhighlight lang=vb>
 +
Private Sub closeBtn_OnClick
 +
  document.getElementById("closeMsg").innerHTML="Closing..."
 +
  phid.Close
 +
End Sub
 +
</syntaxhighlight></div>
  
===Other Phidgets===
+
Then when the button that this function is hooked into is clicked, the Phidget will be closed in software (though not reset or turned off).
  
The design given in this document can also be followed for almost all Phidgets.
+
{{MoreHowTos}}
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==
 
==Common Problems and Solutions/Workarounds==
  
===Issue: Cannot control Phidgets with certain web browsers===
+
None at this time.
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.
+

Latest revision as of 15:55, 7 June 2017

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 the Phidget Encoder, Interface Kit and RFID boards.

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

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 (XP/Vista/7/8)

The examples are quite easy to run under Windows, all you need is the Phidget libraries installed, and Internet Explorer.

Internet Explorer

When you run Phidget examples, depending on your security settings, Internet Explorer may ask you whether you actually want to run the example or not:

At this point you need to select "Run Add On". This prompt can blend in at the top of the screen, so make sure to look for and approve it once the example is open.

Use Our Examples

We provide Hello World examples for both Internet Explorer (IE) and for Windows Script Host (WSH). These are simple, high-level examples that work with any Phidget. We also have a few in-depth examples for our Interface Kit, our Encoder, and our RFID boards. The source file will be named the same as the software object for your device. If you are not sure what the software object for your device is, find your Phidget on our webpage, and then check the API documentation for it.

The HelloWorld example simply prints a message when a Phidget is plugged in (attached) or unplugged (detached). For example, if we run it and then plug in an Interface Kit, we see:

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

By following the instructions above, you probably now have a working example and want to understand it better so you can change it to do what you want. This teaching section has resources for you to learn from the examples and write your own.

Your main reference for writing VBScript code will be our .COM API information, with syntax for all of our functions:

  • .COM API (This is the complete set of functions you have available for all Phidgets)
  • Device Specific APIs - The one for your Phidget can be found in its user guide.

To learn the details behind opening, configuring, using, and closing your Phidget, try the General Phidget Programming page. That page also describes using the Phidget in an event-driven manner and in a traditional manner, both of which are available in VBScript.

Code Snippets

Specific calls in VBScript will differ in syntax from those on the General Phidget Programming page, but the concepts stay the same.  

It may help to have the General Phidget Programming page and this section open at the same time, because they parallel each other and you can refer to the VBScript syntax.  However, many additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming.

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:

  phid.Open()

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 .COM 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

To use the Phidget, it must be plugged in (attached). Because we do not know whether it is when our code starts, 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.WaitForAttachment (3000)

Step Three: Do Things with the Phidget

We recommend the use of event driven programming when working with Phidgets. In VBScript, we can 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. Other events, such as this one to read a sensor change, are specific to the type of Phidget (for example, a Temperature Sensor would have a temperature change event). 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. For example, simply use the instance properties such as phid.SensorChangeTrigger(Index) for Phidget Interface Kits:

  phid.SensorChangeTrigger(0) = 1

Step Four: Close and Delete

If the web page will close via a button, say, and you would like to release use of the Phidget, you can close it via the Close function:

Private Sub closeBtn_OnClick
   document.getElementById("closeMsg").innerHTML="Closing..."
   phid.Close
End Sub

Then when the button that this function is hooked into is clicked, the Phidget will be closed in software (though not reset or turned off).

More How-To's

The General Phidget Programming page gives more information about:

Common Problems and Solutions/Workarounds

None at this time.