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 - REALBasic: Difference between revisions

From Phidgets Legacy Support
No edit summary
Line 40: Line 40:
For example, you can declare at initialization (inside Windows1.Open) some C functions for a PhidgetInterfaceKit with the following syntax:
For example, you can declare at initialization (inside Windows1.Open) some C functions for a PhidgetInterfaceKit with the following syntax:
    
    
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=vb>
<font size="3">
<source lang=vb>


   Declare Function CPhidget_open lib "phidget21" (phid as Ptr, serialNumber as Integer) as integer
   Declare Function CPhidget_open lib "phidget21" (phid as Ptr, serialNumber as Integer) as integer
Line 49: Line 47:
   Declare Function CPhidgetInterfaceKit_set_OnSensorChange_Handler lib "phidget21"(CPhidgetInterfaceKitHandle as Ptr, fptr as Ptr, UserPtr as Ptr) as Integer
   Declare Function CPhidgetInterfaceKit_set_OnSensorChange_Handler lib "phidget21"(CPhidgetInterfaceKitHandle as Ptr, fptr as Ptr, UserPtr as Ptr) as Integer


</source>
</syntaxhighlight></div>
</font>
</div>


The declaration closely follows the C API with a few minor changes.  
The declaration closely follows the C API with a few minor changes.  
Line 60: Line 56:
This can be done by declaring a property on Windows1 named “ifKitHandle” as a Ptr, calling create on an empty block of memory, and then setting the handle to the result.
This can be done by declaring a property on Windows1 named “ifKitHandle” as a Ptr, calling create on an empty block of memory, and then setting the handle to the result.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=vb>
<font size="3">
<source lang=vb>


   Dim tmp As MemoryBlock
   Dim tmp As MemoryBlock
Line 70: Line 64:
   ifKitHandle = tmp.Ptr(0)
   ifKitHandle = tmp.Ptr(0)


</source>
</syntaxhighlight></div>
</font>
</div>


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.  
Line 85: Line 77:
WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=vb>
<font size="3">
<source lang=vb>


   result = CPhidget_open(ifKitHandle, -1)
   result = CPhidget_open(ifKitHandle, -1)
Line 96: Line 86:
   end if
   end if


</source>
</syntaxhighlight></div>
</font>
</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.  
Line 112: Line 100:
Event handler methods with matching definitions must be created inside their own module (Project | Add | Module).  
Event handler methods with matching definitions must be created inside their own module (Project | Add | Module).  


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=vb>
<font size="3">
<source lang=vb>


   Function ifKit_OnSensorChange(ByVal phid As Ptr, ByVal userPtr As Ptr, ByVal index  
   Function ifKit_OnSensorChange(ByVal phid As Ptr, ByVal userPtr As Ptr, ByVal index  
Line 122: Line 108:
   End Function
   End Function


</source>
</syntaxhighlight></div>
</font>
</div>


In this example, a new module “Module1” was created and given the ifKit_OnSensorChange method.  
In this example, a new module “Module1” was created and given the ifKit_OnSensorChange method.  
Line 130: Line 114:
Inside Window1.Open we can then link to the method we wrote with a call to CPhidgetInterfaceKit_set_OnSensorChange_Handler.
Inside Window1.Open we can then link to the method we wrote with a call to CPhidgetInterfaceKit_set_OnSensorChange_Handler.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=vb>
<font size="3">
<source lang=vb>


   Call CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifKitHandle, AddressOf Module1.ifKit_OnsensorChange, nil)
   Call CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifKitHandle, AddressOf Module1.ifKit_OnsensorChange, nil)


</source>
</syntaxhighlight></div>
</font>
</div>


With this, the code inside ifKit_onSensorChange will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
With this, the code inside ifKit_onSensorChange will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
Line 149: Line 129:
Simply declare the C function before using calls such as CPhidgetInterfaceKit_getSensorValue for PhidgetInterfaceKits.
Simply declare the C function before using calls such as CPhidgetInterfaceKit_getSensorValue for PhidgetInterfaceKits.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div class="source"><syntaxhighlight lang=vb>
<font size="3">
<source lang=vb>


   Declare Function CPhidgetInterfaceKit_getSensorValue lib "phidget21" (phid as Ptr, index as Integer, sensorValue as Ptr) as Integerresult = CPhidgetInterfaceKit_getSensorValue(ifKitHandle, 0, tmp)
   Declare Function CPhidgetInterfaceKit_getSensorValue lib "phidget21" (phid as Ptr, index as Integer, sensorValue as Ptr) as Integerresult = CPhidgetInterfaceKit_getSensorValue(ifKitHandle, 0, tmp)
Line 157: Line 135:
   Window1.EditField1.Text = Str(sensorValue)
   Window1.EditField1.Text = Str(sensorValue)


</source>
</syntaxhighlight></div>
</font>
</div>


===Working with multiple Phidgets===
===Working with multiple Phidgets===
Line 172: Line 148:
The design given in this document can also be followed for almost all 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 call CPhidgetRFID_create instead of CPhidgetInterfaceKit_create.  
For example, if you were using a PhidgetRFID instead of an PhidgetInterfaceKit, you would call CPhidgetRFID_create instead of CPhidgetInterfaceKit_create.  
The methods and events available would change but they can be used in a similar manner.  
The methods and events available would change but they can be used in a similar manner.


==Building your Project==
==Building your Project==

Revision as of 21:31, 27 March 2012

Icon-REALBasic.png Preamble about the language's origin and its main characteristics.

Support

REALBasic has a complete API for all Phidgets devices, but no sample code at this time.

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:

  • API Manual
  • API Reference
  • We currently do not have any sample code for REALBasic.
  • 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.

Getting Started

Applications using Phidget can be developed in REALbasic through the C API. This tutorial assumes the use of REALbasic 2009, other recent versions should work as well and would be set up in a similar manner. Phidgets will not run under REALbasic 2006. Although it’s possible to import Phidgets as ActiveX controls, this is not recommended due to the way REALbasic handles them. To begin, launch REALbasic and create a new “Desktop Application”. Then, place an EditField “EditField1” in the form designer for the purpose of capturing some simple output.

Coding For Your Phidget

Before you can use the Phidget, you must declare each of the functions from the C library that the program will use. For example, you can declare at initialization (inside Windows1.Open) some C functions for a PhidgetInterfaceKit with the following syntax:

  Declare Function CPhidget_open lib "phidget21" (phid as Ptr, serialNumber as Integer) as integer
  Declare Function CPhidget_waitForAttachment lib "phidget21" (phid as Ptr, milliseconds asInteger) as Integer
  Declare Function CPhidgetInterfaceKit_create lib "phidget21" (CPhidgetInterfaceKitHandle as Ptr) as Integer
  Declare Function CPhidgetInterfaceKit_set_OnSensorChange_Handler lib "phidget21"(CPhidgetInterfaceKitHandle as Ptr, fptr as Ptr, UserPtr as Ptr) as Integer

The declaration closely follows the C API with a few minor changes. Generally, when any type of PhidgetHandle is used, it’s safe to change the type to Ptr. You will also need to copy the phidget21.dll file from the Phidget Framework directory into your project output directory for these declarations to work.

Afterwards, the Phidget object itself will need to be declared and then initialized. This can be done by declaring a property on Windows1 named “ifKitHandle” as a Ptr, calling create on an empty block of memory, and then setting the handle to the result.

  Dim tmp As MemoryBlock
  Dim result As Integer
  tmp = NewMemoryBlock(1000)
  Call CPhidgetInterfaceKit_create(tmp)
  ifKitHandle = tmp.Ptr(0)

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.

  result = CPhidget_open(ifKitHandle, -1)
  if CPhidget_waitForAttachment(ifKitHandle, 3000)<>0 Then
     MsgBox("Can’t find a PhidgetInterfaceKit")
  else
      'More code goes here
  end if

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 Real Basic, a few steps will need to be followed to properly hook C events. Event handler methods with matching definitions must be created inside their own module (Project | Add | Module).

  Function ifKit_OnSensorChange(ByVal phid As Ptr, ByVal userPtr As Ptr, ByVal index 
  As Integer, ByVal sensorValue As Integer) As Integer
     #pragma X86CallingConvention StdCall
  Window1.EditField1.text = Str(index) + ":" + Str(sensorValue)
  End Function

In this example, a new module “Module1” was created and given the ifKit_OnSensorChange method. Note that the handler must have “#pragma X86CallingConvention StdCall” as the first line. Inside Window1.Open we can then link to the method we wrote with a call to CPhidgetInterfaceKit_set_OnSensorChange_Handler.

  Call CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifKitHandle, AddressOf Module1.ifKit_OnsensorChange, nil)

With this, the code inside ifKit_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 declare the C function before using calls such as CPhidgetInterfaceKit_getSensorValue for PhidgetInterfaceKits.

  Declare Function CPhidgetInterfaceKit_getSensorValue lib "phidget21" (phid as Ptr, index as Integer, sensorValue as Ptr) as Integerresult = CPhidgetInterfaceKit_getSensorValue(ifKitHandle, 0, tmp)
  Dim sensorValue As Integer = tmp.Long(0)
  Window1.EditField1.Text = Str(sensorValue)

Working with multiple Phidgets

Multiple Phidgets of the same type can easily be run inside the same program. In our case, it requires another instance of a PhidgetInterfaceKit to be defined and initialized. The new instance can 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 call CPhidgetRFID_create instead of CPhidgetInterfaceKit_create. The methods and events available would change but they can be used 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.