Channel Persistence Guide
What is Channel Persistence
By default, all properties of your Phidget are reset to default values if the channel is closed and opened again, or if your application stops running and is restarted.
Enabling Channel Persistence allows the state of your channel to persist, even after your application has been closed or has stopped running. If the Phidget loses power (e.g. if it's unplugged from its USB or VINT connection) then it will not persist, event if persistence was enabled.
Supported Channel Classes
Channel Persistence is supported on most output-style devices:
- DigitalOutput
- LCD
- PowerGuard
- RCServo
- Stepper
- VoltageOutput
Enabling Channel Persistence
Video Guide
Adding Persistence to your Program
Persistence can be enabled simply by setting the ChannelPersistence
property of the channel. We recommend enabling it before opening the channel. You can still enable it after the channel is opened, but this would cause the outputs to briefly return to the default (off) state until your application reaches the point in the code where persistence is enabled.
When setting persistence before opening the channel, the library will attempt to read the state of the device, and any property that cannot be read will have an unknown value. This will happen the first time the application runs after the Phidget has been unplugged. In order to deal with the unknown values, certain properties must be set to a known state before you can interact with the channel. If you don't, you'll get an Unknown Value exception.
To determine which properties (if any) must be set, visit your device's API and check the Channel Persistence checkbox:
Then, scroll down to see which properties are labelled as "Must be set":
Example
Here's a simple VINT Hub digital output example with channel persistence. When this code is run the first time, the output will begin in the "off" state, and turn on after 500ms. The next time and any subsequent time it's run, the output will begin in the "on" state.
using System;
using Phidget22;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DigitalOutput digitalOutput0 = new DigitalOutput();
digitalOutput0.IsHubPortDevice = true;
digitalOutput0.HubPort = 0;
digitalOutput0.ChannelPersistence = true;
digitalOutput0.Open(5000);
Thread.Sleep(500);
digitalOutput0.DutyCycle = 1;
digitalOutput0.Close();
}
}
}
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalOutput import *
import time
def main():
digitalOutput0 = DigitalOutput()
digitalOutput0.setIsHubPortDevice(True)
digitalOutput0.setHubPort(0)
digitalOutput0.setChannelPersistence(True)
digitalOutput0.openWaitForAttachment(5000)
time.sleep(1)
digitalOutput0.setDutyCycle(1)
try:
input("Press Enter to Stop\n")
except (Exception, KeyboardInterrupt):
pass
digitalOutput0.close()
main()
import * as phidget22 from 'phidget22';
const conn = new phidget22.NetworkConnection(5661, 'localhost');
await conn.connect();
const digitalOutput0 = new phidget22.DigitalOutput();
digitalOutput0.setIsHubPortDevice(true);
digitalOutput0.setHubPort(0);
digitalOutput0.channelPersistence = 1;
await digitalOutput0.open(5000);
await digitalOutput0.setDutyCycle(1);
setTimeout(async () => {
await digitalOutput0.close();
conn.close();
conn.delete();
process.exit();
}, 5000);
import com.phidget22.*;
import java.util.Scanner; //Required for Text Input
import java.io.IOException;
public class Java_Example {
public static void main(String[] args) throws Exception {
DigitalOutput digitalOutput0 = new DigitalOutput();
digitalOutput0.setIsHubPortDevice(true);
digitalOutput0.setHubPort(0);
digitalOutput0.setChannelPersistence(true);
digitalOutput0.open(5000);
Thread.sleep(500);
digitalOutput0.setDutyCycle(1);
//Wait until Enter has been pressed before exiting
System.in.read();
digitalOutput0.close();
}
}
#include <phidget22.h>
#include <stdio.h>
#include <unistd.h>
int main() {
PhidgetDigitalOutputHandle digitalOutput0;
PhidgetDigitalOutput_create(&digitalOutput0);
Phidget_setIsHubPortDevice((PhidgetHandle)digitalOutput0, 1);
Phidget_setHubPort((PhidgetHandle)digitalOutput0, 0);
Phidget_setChannelPersistence((PhidgetHandle)digitalOutput0, 1);
Phidget_openWaitForAttachment((PhidgetHandle)digitalOutput0, 5000);
sleep(1);
PhidgetDigitalOutput_setDutyCycle(digitalOutput0, 1);
//Wait until Enter has been pressed before exiting
getchar();
Phidget_close((PhidgetHandle)digitalOutput0);
PhidgetDigitalOutput_delete(&digitalOutput0);
}