Alert.png

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.


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

Alert.png

Language - Visual Basic for Applications: Difference between revisions

From Phidgets Legacy Support
No edit summary
Line 4: Line 4:
==Introduction==
==Introduction==


{{LanguageSupport|Visual Basic for Applications|the complete Phidget API, including events|Accelerometer (not Spatial), Encoder, InterfaceKit, RFID, TemperatureSensor and Servo.|Microsoft Office on Windows.|}}
{{LanguageSupport|Visual Basic for Applications|the complete Phidget API, including events|Accelerometer (not Spatial), Encoder, InterfaceKit, RFID, TemperatureSensor and Servo.|Microsoft Office on Windows|}}


==Quick Downloads==
==Quick Downloads==

Revision as of 20:40, 25 May 2012

Visual Basic for Applications (VBA) is an implementation of Microsoft's event-driven programming language Visual Basic 6.0 and its associated integrated development environment (IDE), which are built into most Microsoft Office applications.

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 Visual Basic for Applications specifically.

Visual Basic for Applications is capable of using the complete Phidget API, including events. We also provide example code in Visual Basic for Applications for Accelerometer (not Spatial), Encoder, InterfaceKit, RFID, TemperatureSensor and Servo.

Visual Basic for Applications can be developed with Microsoft Office on Windows.

You can compare Visual Basic for Applications with our other supported languages.

Quick Downloads

Just need the Visual Basic for Applications documentation, drivers, libraries, and examples? Here they are:

Documentation

Example Code

Libraries and Drivers

Getting Started with Visual Basic for Applications (VBA)

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

Support for VBA is only on Windows 2000 / XP / Vista / 7.

Windows (2000/XP/Vista/7)

For a description of the Phidget library files, please see the main Windows page.

The Phidget examples were written using Microsoft Excel 2000 and this tutorial assumes its use. Other environments such as Microsoft Office should work provided they support VBA, and each would be set up in a similar manner.

Use Our Examples

First, please download and unpack the VBA Examples. Here you will find an example called HelloWorld which is a simple, high-level example which will work with any Phidget device. You will also find specific, in-depth examples for some of our other devices. 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 easiest example to start with is the HelloWorld example. Find it in the unpacked examples folder:

And then double click on it to open it in Excel. It contains Macros, so you may get a message that you can't run the program without changing your security settings. The settings can be found in Office XP under Tools → Options → Security (tab). Then you can set the security level to Medium so you can choose to run the Phidget Examples:

This will bring up the example itself. Hello and goodbye messages are printed to the screen as Phidgets are plugged in (attached) and unplugged (detached). Here, we plugged a Phidget Interface Kit in:

Write Your Own Code

To begin, launch Excel with a new workbook for our project. Launch the VBA editor (Tools | Macro | Visual Basic Editor) and open “ThisWorkbook” in the navigator.

Before you can use the Phidget in your code, you must include the library in your project. This can be accomplished from the references screen (Tools | References...) by checking the box beside “Phidget Library 2.1”, or by browsing to the location the framework was installed and choosing the Phidget21COM.dll.

Afterwards, the Phidget object will need to be declared and then initialized inside the new workbook (“ThisWorkbook”). For example, we can declare and create a PhidgetInterfaceKit at the top of the code with:

  Public WithEvents phid As PhidgetInterfaceKit
  Private Sub Workbook_Open()
      Set phid = New PhidgetInterfaceKit
  End Sub

The object name for any type of Phidget is listed in the API manual. Each Phidget (Spatial, Temperature Sensor, Motor Controller, Interface Kit) has a different software object named similarly to its own name. 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. Every type of Phidget also inherits functionality from the Phidget base class.

You can now play around with the examples and basic code, and when you are ready to begin writing your project you can go through our instructional section on what code is in the examples.

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


Code Snippets

Specific calls in VBA 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 .COM/VBA 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

Before you can use the Phidget, you must include the library in your project. This can be accomplished from the references screen (Tools | References...) by checking the box beside “Phidget Library 2.1”, or by browsing to the location the framework was installed and choosing the Phidget21COM.dll.

Afterwards, the Phidget object will need to be declared and then initialized inside the workbook item. For example, we can declare and create a PhidgetInterfaceKit at the top of the code with:

  Public WithEvents phid As PhidgetInterfaceKit
  Private Sub Workbook_Open()
      Set phid = New PhidgetInterfaceKit
  End Sub

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.

At the end of your program, don’t forget to call close to free any locks on the Phidget.

  Private Sub Workbook_BeforeClose(Cancel As Boolean)
     phid.Close
  End Sub

Event Driven Programming

We recommend the use of event driven programming when working with Phidgets. In Visual Basic, we hook an event handler with the following code:

    Private Sub phid_OnSensorChange(ByVal Index As Long, ByVal SensorValue As Long)
        Range("A2").Select
        ActiveCell.Offset(Index, 0).Value = SensorValue
    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. You can let the editor generate the procedure declaration for you through the drop down menu at the top of the code window.

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 SensorValue(Index as Long) or OutputState(Index as Long) for PhidgetInterfaceKits.

  phid.OutputState(4) = True

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 declare 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

Here you can put various frequent problems and our recommended solutions.