Upgrading Code from Phidget21 to Phidget22

From Phidgets Support
Jump to: navigation, search

In order to use your older Phidget applications with new VINT devices, or new versions of the library, you'll have to update to Phidget22. Many structural changes have been made to the library to accommodate the increasing complexity and variety of Phidgets devices. This guide will highlight the most important changes, so you can update your older applications. Please note that for relatively simple programs and applications, you may just want to rewrite them entirely, using the old program as a guideline. For very complex applications, it may be quicker to follow this guide.

Step 1: Install Phidget22 and Update References

The first step in updating your program will be to install phidget22 and update your program's include list or references list, replacing all instances of Phidget21 with Phidget22. Visit the language page for your programming language for more information on including libraries. Once you've removed Phidget21 and added Phidget22, your program will have a number of syntax errors. This will provide an indicator of where in your program you need to focus as you continue to the next few steps.

Step 2: Split up Objects

One of the major changes between Phidget21 and Phidget22 is the way device objects are organized. In Phidget21, each device would have one object which contained properties and methods to interact with each of that device's features. In Phidget22, objects have been broken up so that each object belongs to a single feature, input, or output. Some objects that were already self-contained may have been renamed.

For example, in Phidget21 if you were using a 1018 Phidget InterfaceKit, you'd open the InterfaceKit object, which contained methods and properties to read digital inputs, read analog inputs, and control the digital outputs. In Phidget22, there are separate objects for digital inputs, digital outputs, analog inputs (0-5V input), and voltage ratio inputs (0-5V input in ratiometric mode). In C#, the difference may look something like this:


Phidget21:
 int serial = 324781;
 InterfaceKit ifk = new InterfaceKit();
 ifk.open(serial);
 if (!ifk.Attached)
      ifk.waitForAttachment(5000);

 ifk.outputs[0] = true;
 bool digital_in = ifk.inputs[2];
 double voltage_in = ifk.sensors[1];
 ifk.close();
Phidget22:
 int serial = 324781;
 DigitalOutput do0 = new DigitalOutput();
 DigitalInput di2 = new DigitalInput();
 VoltageInput vi1 = new VoltageInput();
 
 do0.Channel = 0;
 di2.Channel = 2;
 vi1.Channel = 1;

 do0.DeviceSerialNumber = serial;
 di2.DeviceSerialNumber = serial;
 vi1.DeviceSerialNumber = serial;

 do0.Open(5000);
 di2.Open(5000);
 vi1.Open(5000);

 do0.State = true;
 bool digital_in = di2.State;
 double voltate_in = vi1.Voltage;

 do0.Close();
 di2.Close();
 vi1.Close();


As you can see, instead of calling 'open' with many parameters such as serial number, IP address, and port; these are instead set as properties, as is the new "Channel" property. The parameter sent to 'open' in this case is the timeout, which replaces the need for waitForAttachment. Speaking of the Channel property, instead of being handed an array of inputs or outputs, you must declare and open each one individually. This was a necessary change to support devices that have per-channel control of certain properties. Here's an example of how you can deal with this change:


Phidget21:
 InterfaceKit ifk = new InterfaceKit();
 ifk.open(serial);
 if (!ifk.Attached)
      ifk.waitForAttachment(5000);

 ifk.outputs[0] = true;
 ifk.outputs[1] = false;
 ifk.outputs[2] = false;
 ifk.outputs[3] = true;
 
 ifk.close();
Phidget22:
 DigitalOutput[] ifk_output = new DigitalOutput[8];
 
 for(int i=0; i<8; i++)
 {
   ifk_output[i].Channel = i;
   ifk_output[i].Open(5000);
 }

 ifk_output[0].State = true;
 ifk_output[1].State = false;
 ifk_output[2].State = false;
 ifk_output[3].State = true;

 for(int i=0; i<8; i++)
 {
   ifk_output[i].Close();
 }


Refer to the list below to determine which objects have been split or renamed for your device:


Device Phidget21 Objects Phidget22 Objects
1002 Analog VoltageOutput
1010, 1011, 1018, 1019 InterfaceKit DigitalInput
DigitalOutput
VoltageInput
VoltageRatioInput
1012 InterfaceKit DigitalInput
DigitalOutput
1014 InterfaceKit DigitalOutput
1015 InterfaceKit CapacitiveTouch
1016 InterfaceKit CapacitiveTouch
1017 InterfaceKit DigitalOutput
1024 RFID RFID
DigitalOutput
1032 LED DigitalOutput
1040 GPS GPS
1041, 1043 Spatial Accelerometer
1042, 1044 Spatial Accelerometer
Gyroscope
Magnetometer
Spatial
1045 TemperatureSensor TemperatureSensor
1046 Bridge VoltageRatioInput
1047 Encoder Encoder
DigitalInput
1048 TemperatureSensor TemperatureSensor
VoltageInput
Device Phidget21 Objects Phidget22 Objects
1051 TemperatureSensor TemperatureSensor
VoltageInput
1054 FrequencyCounter FrequencyCounter
1055 IR IR
1057 Encoder Encoder
1061 Servo RCServo
CurrentInput
1062 Stepper Stepper
1063 Stepper Stepper
DigitalInput
CurrentInput
1064 MotorControl DCMotor
1065 MotorControl DCMotor
Encoder
DigitalInput
VoltageInput
VoltageRatioInput
1066 Servo RCServo
CurrentInput
1067 Stepper Stepper
1203 TextLCD LCD
InterfaceKit DigitalInput
DigitalOutput
VoltageInput
VoltageRatioInput
1204 TextLCD LCD

Step 3: Update Property and Method Names

Many names of properties, methods, and parameters have changed in the library update for the sake of clarity and consistency. For example, 'open' in Phidget22 is capitalized. Digital outputs are changed via the 'State' property instead of in the 'outputs' array. The LCD object sets custom characters using 'SetCharacterBitmap' instead of the 'customCharacters' array.

If you're using a development environment that lists properties and methods as you're typing, you'll probably be able to figure out the correct replacement.

Objectlist.jpg

If you're using a simpler development environment, you'll want to have the Phidget22 API for your device open as you make your replacements.

SensorValue

The SensorValue abstraction used in Phidget21 (a number ranging from 0 to 1000 that represents a 0 to 5 volt value) doesn't exist in Phidget22. Instead, VoltageInput channels will report the actual voltage read from 0 to 5 volts, so you may need to change some formulae in your code.

However, there is a completely different property called SensorValue in Phidget22, which is automatically converted into the appropriate value for the sensor if you set the SensorType property. For example, if you're using an 1127 Phidget Light sensor, you can set the SensorType to PN_1127. This will cause SensorValue to be automatically converted into lux according to the formula for the 1127. You can use the SensorUnit property to confirm which unit SensorValue is currently using. Have a look at the SensorType enumeration in the VoltageInput and VoltageRatioInput sections of the Phidget22 API for a complete list of supported sensors.

Step 4: Clean Up Remaining Syntax Errors and Test

Keep trying to compile your program as you make these changes. Incorrectly named objects or properties and mismatched types will both be caught by the compiler. Once your program compiles with no errors, test it thoroughly just in case one of the library changes has affected your application in subtle ways.

Other Differences

Persistence of Outputs and Settings

In Phidget21, you could create an object, open it, set properties, and close it. When re-opening, the properties that you set would still be there. For example, you could open an InterfaceKit object, use setOutputState to set one of the outputs to TRUE, and then close the InterfaceKit object. As long as the Phidget wasn't disconnected and didn't lose power, you could count on that output to remain TRUE, even after being reopened.

In Phidget22, outputs and settings are not persistent; when a channel is opened or closed, it is completely reset. In an equivalent example from the one above, if you open a DigitalOutput object and use setState to set it to TRUE, it will revert back to FALSE when it is closed. The reason this change was made is because we wanted Phidget channels to be in a predictable state when first opened. It's not generally a good idea to count on the state of a Phidget that you don't have explicitly opened, because unexpected detaches can happen at any time, and these should be handled by your code. It's a good idea to keep your handles open as long as you need an output set.

Support

If you come across problems with Phidget22, you can contact us for help. Alternatively, you can create a topic on our forums in the appropriate programming language section. Be sure to post the part of your code that's causing the problem and describe what happens when you try to run it.