Do Things with the Channel
8 . Do Things with the Channel
After a channel has been opened and attached, software can control the Phidget through the functions available to its channel class.
This is the stage of your program where you can send commands to your channel, and handle the information it sends back.
Calling Functions and Polling for Data
The most basic way your program can interact with the Phidget channel is by interacting with the channel's functions and properties from the main body of the program.
#Set a DC Motor's velocity:
ch.setTargetVelocity(0.5)
#Get a Voltage Input's voltage:
val = ch.getVoltage()
Important Note: There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an exception.
//Set a DC Motor's velocity:
ch.setTargetVelocity(0.5);
//Get a Voltage Input's voltage:
double val = ch.getVoltage();
Important Note: There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an exception.
//Set a DC Motor's velocity:
ch.TargetVelocity = 0.5;
//Get a Voltage Input's voltage:
double val = ch.Voltage;
Important Note: There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an exception.
//Set a DC Motor's velocity:
PhidgetDCMotor_setTargetVelocity(ch, 0.5);
//Get a Voltage Input's voltage:
double voltage;
PhidgetVoltageInput_getVoltage(ch, &voltage);
Important Note: There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an error code, and a specific nonsensical result. See more information on this on our page for Unknown Values.
// Set a DC Motor's velocity:
ch.targetVelocity = 0.5
// Get a Voltage Input's voltage:
let voltage = ch.voltage
Important Note: There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an exception.
The values returned by getting the values of these properties will be the same as the value from the latest event associated with the property.
Event Handlers
The other, generally more recommended way of interacting with a Phidget channel is through event handlers. You can set up event handlers to react to status updates from channel, such as when a state change occurs, or when sensor data is received. Event handlers should be set up before opening the channel to avoid missing events, but they will begin executing during this phase of the program.
For example, with a Voltage Input channel, you could set up an event handler to process Voltage Change events.
#Declare the event handler
def onVoltageChangeHandler(self, voltage):
print("Voltage: " + str(voltage))
...
ch = VoltageInput()
...
#Assign the handler that will be called when the event occurs
ch.setOnVoltageChangeHandler(onVoltageChangeHandler)
//Declare the event listener
public static VoltageInputVoltageChangeListener onVoltageChange = new VoltageInputVoltageChangeListener() {
@Override
public void onVoltageChange(VoltageInputVoltageChangeEvent e) {
System.out.println("Voltage: " + e.getVoltage());
}
};
...
VoltageInput ch;
...
//Assign the event listener that will be called when the event occurs
ch.addVoltageChangeListener(onVoltageChange);
//Declare the event handler
void voltageChange(object sender, Phidget22.Events.VoltageChangeEventArgs e) {
Console.WriteLine("Voltage: " + e.Voltage.ToString());
}
...
VoltageInput ch;
...
//Assign the handler that will be called when the event occurs
ch.VoltageChange += voltageChange;
//Declare the event handler
static void CCONV onVoltageChangeHandler(PhidgetVoltageInputHandle ph, void *ctx, double voltage) {
printf("Voltage: %lf\n", voltage);
}
...
PhidgetVoltageInputHandle ch;
...
//Assign the handler that will be called when the event occurs
PhidgetVoltageInput_setOnVoltageChangeHandler(ch, onVoltageChangeHandler, NULL);
const ch = new phidget22.VoltageInput()
// Assign the handler that will be called when the event occurs
ch.onVoltageChange = function(voltage) {
console.log('Voltage: ' + voltage)
}
Enumerations
Some Phidget devices have functions that deal with specific predefined values called enumerations. Enumerations commonly provide readable names to a set of numbered options.
The form of these enumerations depends on your programming language, as shown below:
To include a specific enumeration in your Python script, first import it using:
from Phidget22.EnumerationType import *
Once imported, Phidgets enumerations in Python will take the form of EnumerationType.ENUMERATION_NAME.
For example, specifying a SensorType to use the 1142 for a voltage input would look like:
VoltageSensorType.SENSOR_TYPE_1142
and specifying a K-Type thermocouple for a temperature sensor would be:
ThermocoupleType.THERMOCOUPLE_TYPE_K
The Phidget error code for timing out could be specified as:
ErrorCode.EPHIDGET_TIMEOUT
For example, specifying a SensorType to use the 1142 for a voltage input would look like:
com.phidget22.VoltageSensorType.PN_1142
and specifying a K-Type thermocouple for a temperature sensor would be:
com.phidget22.ThermocoupleType.K
The Phidget error code for timing out could be specified as:
com.phidget22.ErrorCode.TIMEOUT
For example, specifying a SensorType to use the 1142 for a voltage input would look like:
Phidget22.VoltageSensorType.PN_1142
and specifying a K-Type thermocouple for a temperature sensor would be:
Phidget22.ThermocoupleType.K
The Phidget error code for timing out could be specified as:
Phidget22.ErrorCode.Timeout
For example, specifying a SensorType to use the 1142 for a voltage input would look like:
SENSOR_TYPE_1142
and specifying a K-Type thermocouple for a temperature sensor would be:
THERMOCOUPLE_TYPE_K
The Phidget error code for timing out could be specified as:
EPHIDGET_TIMEOUT
For example, specifying a SensorType to use the 1142 for a voltage input would look like:
phidget22.VoltageSensorType.PN_1142
and specifying a K-Type thermocouple for a temperature sensor would be:
phidget22.ThermocoupleType.K
The Phidget error code for timing out could be specified as:
phidget22.ErrorCode.TIMEOUT
You can find the Enumeration Type under the Enumerations section of the Phidget22 API for your device, and the Enumeration Name in the drop-down list within.