Upgrading Code from Phidget21 to Phidget22

From Phidgets Support
Revision as of 10:23, 24 November 2016 by Mparadis (talk | contribs) (→‎Support)

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 voltage inputs, and control the digital outputs. In Phidget22, there are separate objects for digital inputs, digital outputs, voltage 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 HumanInput
1016 InterfaceKit HumanInput
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.

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.

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.