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

From Phidgets Legacy Support
No edit summary
 
(33 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Preamble about the language and its general strengths and weaknesses.
[[Category:Language]]
{{OSLang|[[File:Icon-Delphi.png|64px|alt=|link=]]|'''[http://www.embarcadero.com/products/delphi Embarcadero Delphi]''' is an integrated development environment for console, desktop graphical, web application, and mobile applications.}}
__TOC__


==Assessment for use with Phidgets==
==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.


==Support==
{{LanguageSupport|Delphi|the complete Phidget API|AdvancedServo, Servo, Phidget Interface Kit, Phidget IR, Encoder, and RFID.|Windows|}}
Delphi has a complete API for all Phidgets devices, and code samples for some devices.


For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
==Quick Downloads==
 
{{QuickDownloads|Delphi|
==Restrictions==
{{APIQuickDownloads|{{SERVER}}/documentation/COM_API_Manual.pdf COM}}|
In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.
{{ExampleQuickDownloads|{{SERVER}}/downloads/phidget21/examples/com/Delphi.zip Delphi|}}|
 
{{WindowsQuickDownloads}}
==Development Environments and Compilers==
}}
==Getting Started with Delphi==


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.  
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
{{ExampleCodeReasons}}


==Quick Downloads==
Delphi is a .COM-based language. Currently Windows is the only supported operating system for Delphi:
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
*[[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]
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/Delphi_2.1.8.20110615.zip Delphi 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.
==Windows(XP/Vista/7/8)==


==Getting Started==
===Borland Delphi===


The Phidget examples were written using Borland Delphi 2005 and 7, and this tutorial assumes its use.  
The Phidget examples were written using Borland Delphi 2005 and 7, although they also work with other versions of Delphi as described on this Delphi Language page. We include the method to import ActiveX objects onto these platforms, but our current in-depth support for using our examples and writing your own code is only for [[#Embarcadero Delphi XE2|Embarcadero Delphi XE2]].
The ActiveX objects installed with the Phidget libraries are not automatically imported into Delphi, so to begin you will need to manually import them:


#Go to Component >> Import ActiveX Control...
====Import ActiveX Objects====
#Select the Phidget Library 2.1 and click install
#In the next popup select the “install into new package” tab
The ActiveX objects installed with the Phidget libraries are not automatically imported into Delphi, so to begin you will need to manually import them.
#Give the package a suitable name and description such as “Phidgets”, and then click OK
#Compile and Install the newly created package if not done so automatically.


In Delphi 2005/2006:
In Delphi 2005/2006:


#Go to File >> New >> Package - Delphi for Win32. Rename and save the package using File >> Save Project As...
#Go to File→New→Package - Delphi for Win32. Rename and save the package using File→Save Project As...
#Import the ActiveX component, go to Component >> Import Component.
#Import the ActiveX component, go to Component→Import Component.
#Choose Inport ActiveX Control and click Next
#Choose Inport ActiveX Control and click Next
#Select Phidget Library 2.1 and click Next
#Select Phidget Library 2.1 and click Next
Line 51: Line 43:
#When you open a new or existing application, you should now see all the Phidget device control objects listed under the ActiveX category.
#When you open a new or existing application, you should now see all the Phidget device control objects listed under the ActiveX category.


In newer versions of Delphi:
In slightly newer versions of Delphi:


#Go to Component >> Import Component...
#Go to Component→Import Component...
#Choose “Import ActiveX Control” and click Next
#Choose “Import ActiveX Control” and click Next
#Pick Phidget Library 2.1 from the list and click Next
#Pick Phidget Library 2.1 from the list and click Next
Line 62: Line 54:
Once installed, all future projects will be able to use the Phidget ActiveX objects.
Once installed, all future projects will be able to use the Phidget ActiveX objects.


===Coding For Your Phidget===
===Embarcadero Delphi XE2===
 
Embarcadero Delphi is the more recent platform for developing Delphi on.  To use it, we follow the same setup procedure as Borland, which is to first import the Phidget library as an ActiveX object.  We then offer in-depth instructions on how to use the Phidget examples, both as described below.
 
====Import ActiveX Objects====
 
The ActiveX objects installed with the Phidget libraries are not automatically imported into Delphi, so to begin you will need to manually import them.  In Embarcadero XE2, do the following:
 
1. Go to Component→Import Component...
 
[[Image:Delphi open component.png|link=|alt=]]
 
2. Choose “Import ActiveX Control” and click Next:
 
[[Image:Delphi import activex.png|link=|alt=]]
 
3. Pick Phidget Library 2.1 from the list and click Next:
 
[[Image:Delphi register activex.png|link=|alt=]]
 
4. Choose Install into a new package and click next:
 
[[Image:Delphi as new package.png|link=|alt=]]
 
5. Give the package a name and description such as “Phidgets”, and then click Finish.
 
6. To finish, the objects need to be re-linked.  When prompted to do this, click Yes:
 
[[Image:Delphi finish activex.png|link=|alt=]]
 
At this point, opening a new project will have the use of the Phidget library.  If you already had a project open, you will need to close and re-open it for the linking to Phidgets to occur.
 
====Use Our Examples====
 
After you have installed the [[#Install ActiveX Objects|Active X objects]], you can open our examples and run code to test your device.  {{FindYourDevice}}
 
First download the [{{SERVER}}/downloads/phidget21/examples/com/Delphi.zip Delphi examples] and unpack them.  Each project has its own folder.  You can open an existing project and navigate to the project file itself to open it:
 
[[Image:Delphi open project.png|link=|alt=]]
 
Then, the project and all of its files will appear in your project navigator on the right.  Plug in your Phidget to your computer, and make sure no other programs are accessing the Phidget.  Right-click on the project root and select "Run".  For example, for the Interface Kit Phidget, your project will probably look something like this:


Before you can use the Phidget, you must declare and initialize its ActiveX object.  
[[Image:Delphi run interfacekit.png|link=|alt=]]
The simplest method is to place the control from the ActiveX component tab on to your form.  
 
For this tutorial, create a PhidgetInterfaceKit control (PhidgetInterfaceKit1) and then add a text edit field to the form for the purpose of capturing simple output.  
The GUI form of the example will open, and you can test your device.
The object name for any type of Phidget is listed in the API manual.  
 
====Write Your Own Code====
 
In Embarcadero, the lower right panel displays the available ActiveX objects for coding.  You can create a new project and then simply drag the ActiveX object that matches your Phidget device into your project.
 
We have a [[#Follow The Examples|teaching section]] below which includes code snippets to augment the examples and help you compose your project.
 
==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 Delphi code will be our .COM API information, with syntax for all of our functions:
 
{{UsingAPhidgetInCodeGeneral|both of which are available in Delphi|[{{SERVER}}/documentation/COM_API_Manual.pdf .COM API]}}
 
===Code Snippets===
 
Specific calls in Delphi 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 Delphi 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 declare and initialize its ActiveX object. The simplest method is to place the control from the ActiveX component tab on to your form. For this tutorial, we create a Phidget Interface Kit control (PhidgetInterfaceKit1) and then add a text edit field to the form for the purpose of capturing simple output. 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.
===Connecting to the Phidget===


Next, we need to tell the program to try and connect to the Phidget through a call to open().  
Next, we need to tell the program to try and connect to the Phidget through a call to open().  
The open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected.
The open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected:
This means that calling open does not guarantee you can use the Phidget immediately.
We can handle it by either 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 style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=delphi>


<div class="source"><syntaxhighlight lang=delphi>
   procedure TForm1.FormCreate(Sender: TObject);
   procedure TForm1.FormCreate(Sender: TObject);
   begin
   begin
     PhidgetInterfaceKit1.Open(-1);
     PhidgetInterfaceKit1.Open(-1);
  PhidgetInterfaceKit1.WaitForAttachment(3000);
   end;
   end;
</syntaxhighlight></div>


</source>
The parameters (here just a -1) can also be used to open the first Phidget of a type it can find, open based on its serial number, or even open across the network. The API manual lists all of the available modes that open provides.
</font>
</div>


The parameters can also be used to open the first Phidget of a type it can find, open based on its serial number, or even open across the network.
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. To open multiple Phidgets, they each need their own ActiveX object, and you can distinguish between them by serial number.
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.
You can call Close any time outside of the Phidget’s own event handlers to end the connection.


===Event Driven Programming===
====Step Two: Wait for Attachment (plugging in) of the Phidget====


We recommend the use of event driven programming when working with Phidgets.  
Calling open does not guarantee you can use the Phidget immediately. To use the Phidget, it must be plugged in (attached). We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or we can handle this by calling WaitForAttachment. You can put the wait function right after your open function.  So we can modify our opening block to read:
This allows the program to execute other tasks until the Phidget fires a new event.


In Delphi, when the event library was installed it added Phidget ActiveX components for each type of Phidget.
<div class="source"><syntaxhighlight lang=delphi>
These ActiveX components will show up in the toolbox under the ActiveX tab.
   procedure TForm1.FormCreate(Sender: TObject);
The ActiveX object for your Phidget can be added to the form and then hooked to the event handlers you wish to use like any other control.
For a Phidget21COMIPhidgetInterfaceKitEvents control, the event handler looks like the following: 
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=delphi>
 
   procedure TForm1.PhidgetInterfaceKit1SensorChange(ASender: TObject; Index,
      SensorValue: Integer);
   begin
   begin
      Edit1.Text := IntToStr(Index) + ': ' + IntToStr(SensorValue);
    PhidgetInterfaceKit1.Open(-1);
    PhidgetInterfaceKit1.WaitForAttachment(3000);
   end;
   end;
</syntaxhighlight></div>


</source>
This function works for any Phidget. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
</font>
</div>


With this method, the code inside PhidgetInterfaceKit1SensorChange will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
====Step Three: Do Things with the Phidget====
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 COM API manual for a full list of events and their general usage.


===Working directly with the Phidget===
We recommend the use of [[General Phidget Programming#Event Driven Code|event driven programming]] when working with Phidgets. This allows the program to execute other tasks until the Phidget fires a new event.


Some values can be directly read and set on the Phidget, and inside polling loops used as an alternative to event driven programming.
In Delphi, when the event library was installed as described in the [[#Use Our Examples|Use Our Examples]] section, it added Phidget ActiveX components for each type of Phidget. These ActiveX components will show up in the toolbox under the ActiveX tab, usually in the lower right. The ActiveX object for your Phidget can be added to the form and then hooked to the event handlers you wish to use like any other control.  
Simply use the CallString such as SensorValue[Index] or OutputState[Index] for PhidgetInterfaceKits.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
For example, for a Phidget21COMIPhidgetInterfaceKitEvents control, the event handler looks like the following:
<font size="3">
<source lang=delphi>


   PhidgetInterfaceKit1.OutputState[0] := true;
<div class="source"><syntaxhighlight lang=delphi>
   procedure TForm1.PhidgetInterfaceKit1SensorChange(ASender: TObject; Index, SensorValue: Integer);
  begin
      Edit1.Text := IntToStr(Index) + ': ' + IntToStr(SensorValue);
  end;
</syntaxhighlight></div>


</source>
With this method, the code inside PhidgetInterfaceKit1SensorChange 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, like this sensor change one, are specific to the type of Phidget.  Please refer to the COM API manual for a full list of events and their general usage.
</font>
</div>


===Working with multiple Phidgets===
Some values can be directly read and set on the Phidget. These functions can be used inside a polling loop as an alternative to event driven programming.    Simply use the CallString such as SensorValue[Index] or OutputState[Index] for PhidgetInterfaceKits:


Multiple Phidgets of the same type can easily be run inside the same program.
<div class="source"><syntaxhighlight lang=delphi>
In our case, it requires another PhidgetInterfaceKit ActiveX object to be added to the project.
  PhidgetInterfaceKit1.OutputState[0] := true;
The new instance can then be set up, opened and used in the same fashion as the previous one.  
</syntaxhighlight></div>
If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.


===Other Phidgets===
====Step Four: Close and Delete====


The design given in this document can also be followed for almost all Phidgets.  
Remember that the Open call reserves the Phidget for sole use by your program. Close removes this lock. You can call Close any time outside of the Phidget's own event handlers to end the connection.
For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would place a PhidgetRFID ActiveX object instead of a PhidgetInterfaceKit.  
The methods and events available would change but they can be accessed in a similar manner.  


===Enabling Logging===
====Enabling Logging====


Often it’s a good idea to enable logging during development for debugging purposes.  
Often it’s a good idea to enable logging during development for debugging purposes. This log, depending on the level set, will record certain events and errors from Phidgets. In Delphi, this is accomplished by calling EnableLogging(logLevel, filename) on the Phidget object.  
This log, depending on the level set, will record certain events and errors from Phidgets.  
In Delphi, this is accomplished by calling EnableLogging(logLevel, filename) on the Phidget object.  
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=delphi>


<div class="source"><syntaxhighlight lang=delphi>
   PhidgetInterfaceKit1.EnableLogging(6, 'testlog.txt');
   PhidgetInterfaceKit1.EnableLogging(6, 'testlog.txt');
</syntaxhighlight></div>


</source>
Similarly, you can call DisableLogging() at the end of your program to shut it off. Should you need to contact Phidgets for support, including this log is very helpful for revealing the cause of the problem.  
</font>
</div>
 
Similarly, you can call DisableLogging() at the end of your program to shut it off.  
Should you need to contact Phidgets for support, including this log is very helpful for revealing the cause of the problem.  
Please see the Programming Manual for a general discussion on Phidget logging and the levels provided.
Please see the Programming Manual for a general discussion on Phidget logging and the levels provided.


==Building your Project==
{{MoreHowTos}}
Describe the different ways a project could be built using this language.


==Common Problems and Solutions/Workarounds==
==Common Problems and Solutions/Workarounds==
Here you can put various frequent problems and our recommended solutions.
 
===Embarcadero XE2: The Phidget Manager creates a runtime fault===
 
In Delphi XE2, you cannot use the Phidget Manager as an object directly.  If you need to open multiple Phidgets of the same device type, use the serial number to distinguish between them.
 
===Embarcadero XE2 under Windows in Oracle VirtualBox: System Crash===
 
Embarcadero Delphi does ''not'' work under Windows running as a guest on Oracle's VirtualBox.  Adding an ActiveX object on such a system will crash the system and appear to destroy it, although a hard power cycle will recover the system.  VMWare does not suffer from this problem, and of course native Windows 7 works fine.

Latest revision as of 19:05, 6 June 2017

Embarcadero Delphi is an integrated development environment for console, desktop graphical, web application, and mobile 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 Delphi specifically.

Delphi is capable of using the complete Phidget API. We also provide example code in Delphi for AdvancedServo, Servo, Phidget Interface Kit, Phidget IR, Encoder, and RFID.

Delphi can be developed with Windows.

You can compare Delphi with our other supported languages.

Quick Downloads

Just need the Delphi documentation, drivers, libraries, and examples? Here they are:

Documentation

Example Code

Libraries and Drivers

Getting Started with Delphi

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

Delphi is a .COM-based language. Currently Windows is the only supported operating system for Delphi:

Windows(XP/Vista/7/8)

Borland Delphi

The Phidget examples were written using Borland Delphi 2005 and 7, although they also work with other versions of Delphi as described on this Delphi Language page. We include the method to import ActiveX objects onto these platforms, but our current in-depth support for using our examples and writing your own code is only for Embarcadero Delphi XE2.

Import ActiveX Objects

The ActiveX objects installed with the Phidget libraries are not automatically imported into Delphi, so to begin you will need to manually import them.

In Delphi 2005/2006:

  1. Go to File→New→Package - Delphi for Win32. Rename and save the package using File→Save Project As...
  2. Import the ActiveX component, go to Component→Import Component.
  3. Choose Inport ActiveX Control and click Next
  4. Select Phidget Library 2.1 and click Next
  5. Click Next
  6. Select ‘Add unit to [Package Name] project’ and click Finish.
  7. Install the ActiveX package you have created by right-mouse-clicking the package name in the Project Manager pane and selecting ‘Install’.
  8. Delphi should now display a confirmation that the package was installed. Click ok and save the package.
  9. When you open a new or existing application, you should now see all the Phidget device control objects listed under the ActiveX category.

In slightly newer versions of Delphi:

  1. Go to Component→Import Component...
  2. Choose “Import ActiveX Control” and click Next
  3. Pick Phidget Library 2.1 from the list and click Next
  4. Set the palette Page to ActiveX and check the Generate Component Wrappers option
  5. Choose Install into a new package and click next
  6. Give the package a name and description such as “Phidgets”, and then click Finish

Once installed, all future projects will be able to use the Phidget ActiveX objects.

Embarcadero Delphi XE2

Embarcadero Delphi is the more recent platform for developing Delphi on. To use it, we follow the same setup procedure as Borland, which is to first import the Phidget library as an ActiveX object. We then offer in-depth instructions on how to use the Phidget examples, both as described below.

Import ActiveX Objects

The ActiveX objects installed with the Phidget libraries are not automatically imported into Delphi, so to begin you will need to manually import them. In Embarcadero XE2, do the following:

1. Go to Component→Import Component...

2. Choose “Import ActiveX Control” and click Next:

3. Pick Phidget Library 2.1 from the list and click Next:

4. Choose Install into a new package and click next:

5. Give the package a name and description such as “Phidgets”, and then click Finish.

6. To finish, the objects need to be re-linked. When prompted to do this, click Yes:

At this point, opening a new project will have the use of the Phidget library. If you already had a project open, you will need to close and re-open it for the linking to Phidgets to occur.

Use Our Examples

After you have installed the Active X objects, you can open our examples and run code to test your device. 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.

First download the Delphi examples and unpack them. Each project has its own folder. You can open an existing project and navigate to the project file itself to open it:

Then, the project and all of its files will appear in your project navigator on the right. Plug in your Phidget to your computer, and make sure no other programs are accessing the Phidget. Right-click on the project root and select "Run". For example, for the Interface Kit Phidget, your project will probably look something like this:

The GUI form of the example will open, and you can test your device.

Write Your Own Code

In Embarcadero, the lower right panel displays the available ActiveX objects for coding. You can create a new project and then simply drag the ActiveX object that matches your Phidget device into your project.

We have a teaching section below which includes code snippets to augment the examples and help you compose your project.

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

Code Snippets

Specific calls in Delphi 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 Delphi 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 declare and initialize its ActiveX object. The simplest method is to place the control from the ActiveX component tab on to your form. For this tutorial, we create a Phidget Interface Kit control (PhidgetInterfaceKit1) and then add a text edit field to the form for the purpose of capturing simple output. 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.

Next, we need to tell the program to try and connect to the Phidget through a call to open(). The open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected:

  procedure TForm1.FormCreate(Sender: TObject);
  begin
     PhidgetInterfaceKit1.Open(-1);
  end;

The parameters (here just a -1) can also be used to open the first Phidget of a type it can find, open based on its 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. To open multiple Phidgets, they each need their own ActiveX object, and you can distinguish between them by serial number.

Step Two: Wait for Attachment (plugging in) of the Phidget

Calling open does not guarantee you can use the Phidget immediately. To use the Phidget, it must be plugged in (attached). We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or we can handle this by calling WaitForAttachment. You can put the wait function right after your open function. So we can modify our opening block to read:

  procedure TForm1.FormCreate(Sender: TObject);
  begin
     PhidgetInterfaceKit1.Open(-1);
     PhidgetInterfaceKit1.WaitForAttachment(3000);
  end;

This function works for any Phidget. WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.

Step Three: Do Things with the Phidget

We recommend the use of event driven programming when working with Phidgets. This allows the program to execute other tasks until the Phidget fires a new event.

In Delphi, when the event library was installed as described in the Use Our Examples section, it added Phidget ActiveX components for each type of Phidget. These ActiveX components will show up in the toolbox under the ActiveX tab, usually in the lower right. The ActiveX object for your Phidget can be added to the form and then hooked to the event handlers you wish to use like any other control.

For example, for a Phidget21COMIPhidgetInterfaceKitEvents control, the event handler looks like the following:

  procedure TForm1.PhidgetInterfaceKit1SensorChange(ASender: TObject; Index, SensorValue: Integer);
  begin
       Edit1.Text := IntToStr(Index) + ': ' + IntToStr(SensorValue);
  end;

With this method, the code inside PhidgetInterfaceKit1SensorChange 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, like this sensor change one, are specific to the type of Phidget. Please refer to the COM API manual for a full list of events and their general usage.

Some values can be directly read and set on the Phidget. These functions can be used inside a polling loop as an alternative to event driven programming. Simply use the CallString such as SensorValue[Index] or OutputState[Index] for PhidgetInterfaceKits:

  PhidgetInterfaceKit1.OutputState[0] := true;

Step Four: Close and Delete

Remember that the Open call reserves the Phidget for sole use by your program. Close removes this lock. You can call Close any time outside of the Phidget's own event handlers to end the connection.

Enabling Logging

Often it’s a good idea to enable logging during development for debugging purposes. This log, depending on the level set, will record certain events and errors from Phidgets. In Delphi, this is accomplished by calling EnableLogging(logLevel, filename) on the Phidget object.

  PhidgetInterfaceKit1.EnableLogging(6, 'testlog.txt');

Similarly, you can call DisableLogging() at the end of your program to shut it off. Should you need to contact Phidgets for support, including this log is very helpful for revealing the cause of the problem. Please see the Programming Manual for a general discussion on Phidget logging and the levels provided.

More How-To's

The General Phidget Programming page gives more information about:

Common Problems and Solutions/Workarounds

Embarcadero XE2: The Phidget Manager creates a runtime fault

In Delphi XE2, you cannot use the Phidget Manager as an object directly. If you need to open multiple Phidgets of the same device type, use the serial number to distinguish between them.

Embarcadero XE2 under Windows in Oracle VirtualBox: System Crash

Embarcadero Delphi does not work under Windows running as a guest on Oracle's VirtualBox. Adding an ActiveX object on such a system will crash the system and appear to destroy it, although a hard power cycle will recover the system. VMWare does not suffer from this problem, and of course native Windows 7 works fine.