Do Things with the Channel: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
Line 1: Line 1:
{{Recommended_Flow_Links|{{Flow Page Number|{{PAGENAME}} }} }}
[[Category:Programming]]{{Recommended_Flow_Links|{{Flow Page Number|{{PAGENAME}} }} }}
After a channel has been ''opened'' and ''attached'', software can control the Phidget through the functions available to its channel class.
After a channel has been ''opened'' and ''attached'', software can control the Phidget through the functions available to its channel class.



Revision as of 16:58, 15 March 2021

 Phidget Programming Basics: Do Things with the ChannelTOC Icon.png Table of Contents

Nav Back Arrow.png Nav Back Hover.png WhiteTab1.png HoverTab1.jpg WhiteTab2.png HoverTab2.jpg WhiteTab3.png HoverTab3.jpg WhiteTab4.png HoverTab4.jpg WhiteTab5.png HoverTab5.jpg WhiteTab6.png HoverTab6.jpg WhiteTab7.png HoverTab7.jpg GreenTab8.png WhiteTab9.png HoverTab9.jpg WhiteTab10.png HoverTab10.jpg WhiteTab11.png HoverTab11.jpg WhiteTab12.png HoverTab12.jpg WhiteTab13.png HoverTab13.jpg WhiteTab14.png HoverTab14.jpg WhiteTab15.png HoverTab15.jpg WhiteTab16.png HoverTab16.jpg Nav Next Arrow.png Nav Next Hover.png


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

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);
// Declare the event handler
function onVoltageChangeHandler (voltage) {
    console.log('Voltage: ' + voltage)
}
...
var ch = new phidget22.VoltageInput()
...
// Assign the handler that will be called when the event occurs
ch.onVoltageChange = onVoltageChangeHandler

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

Enumerations with Phidgets in Java will take the form of com.phidget22.EnumerationType.ENUMERATION_NAME.

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

Enumerations with Phidgets in C# will take the form of Phidget22.EnumerationType.Enumeration_Name.

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

Enumerations with Phidgets in C will take the form of ENUMERATION_NAME.

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

Enumerations with Phidgets in JavaScript will take the form of phidget22.EnumerationType.ENUMERATION_NAME.

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.