Difference between revisions of "A Basic Phidget Program"

From Phidgets Support
Jump to: navigation, search
(Created page with "{{Recommended_Flow_Links|{{Flow Page Number|{{PAGENAME}} }} }} Putting all this information together may seem a bit daunting at first, but the end result can actually be quite...")
 
 
Line 156: Line 156:
 
     PhidgetDigitalInput_delete(&ch);
 
     PhidgetDigitalInput_delete(&ch);
 
}
 
}
 +
</syntaxhighlight>
 +
|-|
 +
JavaScript (Node.js)=<syntaxhighlight lang=javascript>
 +
var phidget22 = require('phidget22');
 +
 +
function attach(ch) {
 +
    console.log('Phidget Attached!')
 +
}
 +
 +
function stateChange(state) {
 +
    console.log('State: ' + state)
 +
}
 +
 +
function runExample() {
 +
    var ch = new phidget22.DigitalInput()
 +
 +
    // Set any addressing parameters here
 +
 +
    ch.onAttach = attach
 +
    ch.onStateChange = stateChange
 +
    ch.open(5000)
 +
 +
    // Do stuff with your Phidgets here
 +
 +
    setTimeout(function() {
 +
ch.close()
 +
process.exit(1)
 +
    },10000);
 +
}
 +
 +
var conn = new phidget22.Connection(5661, 'localhost')
 +
conn.connect().then(runExample)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</tabber>
 
</tabber>

Latest revision as of 20:24, 9 April 2019

 Phidget Programming Basics: A Basic Phidget ProgramTOC 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 WhiteTab8.png HoverTab8.jpg WhiteTab9.png HoverTab9.jpg WhiteTab10.png HoverTab10.jpg GreenTab11.png 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


11 . A Basic Phidget Program

Putting all this information together may seem a bit daunting at first, but the end result can actually be quite simple.

For example, the following program will read the state of a button for a time, as seen in the flowchart to the right:

from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
import time

def onAttachHandler(self):
    print("Phidget Attached!")

def onStateChangeHandler(self, state):
    print("State %f" % state)

def main():
    ch = DigitalInput()

    #Set Any Addressing Parameters Here

    ch.setOnAttachHandler(onAttachHandler)
    ch.setOnStateChangeHandler(onStateChangeHandler)

    ch.openWaitForAttachment(5000)

    # Do stuff with your Phidgets here.
    time.sleep(10)

    ch.close()

main()

import com.phidget22.*;

public class DigitalInputExample {
   
   private static DigitalInput ch = null; 

   public static AttachListener onAttach = new AttachListener() {
      @Override
      public void onAttach(AttachEvent e) {
         System.out.print("Phidget Attached!\n");
      }
   };
   
   public static DigitalInputStateChangeListener onStateChange =
      new DigitalInputStateChangeListener() {
      @Override
      public void onStateChange(DigitalInputStateChangeEvent e) {
         System.out.println("State: " + e.getState());
      }
   };
   
   public static void main(String[] args) throws Exception {
      try {
         ch = new DigitalInput();

         //Set Any Addressing Parameters Here

         ch.addAttachListener(onAttach);
         ch.addStateChangeListener(onStateChange);
         
         ch.open(5000);
         
         Thread.sleep(10000);

         ch.close();
         return;
         
      } catch (PhidgetException ex) {
         System.out.println(ex.getDescription());
      }
   }
}

using System;
using Phidget22;
using Phidget22.Events;

namespace ConsoleApplication
{
    class Program
    {
        private static DigitalInput ch = null;

        private static void onAttach(object sender,
                                     AttachEventArgs e)
        {
            Console.WriteLine("Phidget Attached!");
        }

        private static void onStateChange(object sender,
                              DigitalInputStateChangeEventArgs e)
        {
            Console.WriteLine("State: " + e.State);
        }

        static void Main(string[] args)
        {
            ch = new DigitalInput();

            //Set Any Addressing Parameters Here

            ch.Attach += onAttach;
            ch.StateChange += onStateChange;

            ch.Open(5000);
            
            System.Threading.Thread.Sleep(10000);
            
            ch.Close();
        }
    }
}

#include <phidget22.h>
#include <stdio.h>

static void CCONV onAttachHandler(PhidgetHandle ph, void *ctx) {
    printf("Phidget Attached!\n");
}

static void CCONV onStateChangeHandler(PhidgetDigitalInputHandle ph,
                                       void *ctx, int state) {
    printf("State: %d\n", state);
}

int main() {
    PhidgetDigitalInputHandle ch;
    PhidgetDigitalInput_create(&ch);

    //Set Any Addressing Parameters Here

    Phidget_setOnAttachHandler((PhidgetHandle)ch,
                                onAttachHandler, NULL);
    PhidgetDigitalInput_setOnStateChangeHandler(ch,
                                onStateChangeHandler, NULL);

    Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);

    //Do stuff with your Phidgets here.
    getchar(); //Wait until ENTER is pressed

    Phidget_close((PhidgetHandle)ch);
    PhidgetDigitalInput_delete(&ch);
}

var phidget22 = require('phidget22');

function attach(ch) {
    console.log('Phidget Attached!')
}

function stateChange(state) {
    console.log('State: ' + state)
}

function runExample() {
    var ch = new phidget22.DigitalInput()

    // Set any addressing parameters here

    ch.onAttach = attach
    ch.onStateChange = stateChange
    ch.open(5000)

    // Do stuff with your Phidgets here

    setTimeout(function() {
	ch.close()
	process.exit(1)
    },10000);
}

var conn = new phidget22.Connection(5661, 'localhost')
conn.connect().then(runExample)


Simple Phidget Program Flowchart.png

Click Flowchart to Enlarge