|
|
(202 intermediate revisions by 9 users not shown) |
Line 1: |
Line 1: |
| Preamble about the language and its general strengths and weaknesses.
| | [[Category:Language]] |
| | __NOTOC__ |
|
| |
|
| ==Assessment for use with Phidgets==
| | We provide support for Python in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets. |
| Our honest opinion on how well this language is suited to controlling Phidgets. If it is a poor choice, suggest and link similar (better) languages.
| |
|
| |
|
| ==Support==
| | If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with command line or terminal as the simplest path to getting your code running. |
| Python has a complete API and sample code for all Phidgets devices.
| |
|
| |
|
| For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
| | Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on [[Phidget Programming Basics]]. The guide will showcase the fundamentals of programming with Phidgets, with examples in Python. |
|
| |
|
| ==Restrictions== | | == Setup Guide == |
| In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.
| |
|
| |
|
| ==Development Environments and Compilers==
| | <div class="phd-deck-sequence"> |
| | | {{PT3_PYTHON_CHOOSE}}{{PT3_PYTHON_WIN_PYCHARM}}{{PT3_PYTHON_WIN_PYCHARM_1}}{{PT3_PYTHON_WIN_PYCHARM_2}}{{PT3_PYTHON_WIN_PYCHARM_3}}{{PT3_PYTHON_WIN_CMD}}{{PT3_PYTHON_MAC_TRM}}{{PT3_PYTHON_LNX_TRM}}{{PT3_PYTHON_SBC_TRM}}{{PT3_PYTHON_WIN_LIC}}{{PT3_PYTHON_WIN_VS}}{{PT3_PYTHON_WIN_VS_1}}{{PT3_PYTHON_WIN_VS_2}}{{PT3_PYTHON_WIN_IDLE}} |
| Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.
| |
| | |
| ==Drivers, Libraries and Resources==
| |
| Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
| |
| Visit the drivers section at www.phidgets.com and get the latest:
| |
| * [http://www.phidgets.com/drivers.php Phidget Framework]
| |
| * [http://www.phidgets.com/downloads/libraries/PhidgetsPython_2.1.8.20110804.zip Phidgets Python Module]
| |
| You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:
| |
| * [http://www.phidgets.com/documentation/PythonDoc.zip API Manual]
| |
| * [http://www.phidgets.com/documentation/web/PythonDoc/Phidgets.html API Reference]
| |
| * [http://www.phidgets.com/downloads/examples/Python_2.1.8.20110804.zip Python Sample Code]
| |
| * You can find a high level discussion about programming with Phidgets in general on the [[General API]] page.
| |
| * The [[Device Functionality]] page explains the general operational information for your device.
| |
| | |
| You may want to have these pages open while working through these instructions.
| |
| | |
| ==Getting Started==
| |
| | |
| The Phidget examples were written in Python 3.0 and this tutorial assumes its use.
| |
| However, they should still be compatible with Python 2.6.
| |
| To run the examples using Python 2.5, you will need to modify the example code in the exception handling to read “except RuntimeError, e:”, instead of “except RuntimeError as e:”.
| |
| Please ensure you have extracted the “Phidgets” directory from the Python Module into your project directory or into the lib\site-packages\ directory in your python install.
| |
| | |
| ===Coding For Your Phidget===
| |
| | |
| Before you can use the Phidget, you must include a reference in the code to the library. In Python:
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=python> | |
| | |
| from Phidgets.PhidgetException import *
| |
| from Phidgets.Events.Events import *
| |
| from Phidgets.Devices.InterfaceKit import *
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| Afterwards, the Phidget object will need to be declared and then initialized.
| |
| For example, we can declare a PhidgetInterfaceKit with:
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=python>
| |
| | |
| try:
| |
| interfaceKit = InterfaceKit()
| |
| except RuntimeError as e:
| |
| print("Runtime Error: %s" % e.message)
| |
| | |
| </source>
| |
| </font>
| |
| </div> | | </div> |
|
| |
|
| The initialization of the Phidget as well as calls using the Phidget object should be surrounded by a try catch block to handle any errors thrown by the library.
| |
| Calls to the Phidget object will throw a PhidgetException on an error.
| |
|
| |
|
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| | == Quick Downloads == |
| <font size="3">
| |
| <source lang=python>
| |
|
| |
|
| try:
| | If you already know what you're doing and just need the files, you can find them all below. |
| #Your program Code here
| |
| except PhidgetException as e:
| |
| print (“Phidget Exception %i: %s” % (e.code, e.detail))
| |
| exit(1)
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| The object name for any type of Phidget is listed in the API manual.
| |
| Every type of Phidget also inherits functionality from the Phidget base class.
| |
| | |
| ===Connecting to the Phidget===
| |
| | |
| The program can try to connect to the Phidget through an open call.
| |
| Open will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected.
| |
| This means that simply calling open does not guarantee you can use the Phidget immediately.
| |
| We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking isAttached().
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=python>
| |
| | |
| interfaceKit.openPhidget()
| |
| interfaceKit.waitForAttach(10000)
| |
| print (“%d attached!” % (interfaceKit.getSerialNum()))
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| The parameters can be used to open the first Phidget of a type it can find, open based on its serial number, or even open across the network.
| |
| The API manual lists all of the available modes that open provides.
| |
| One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed.
| |
| This prevents any other instances from retrieving data from the Phidget, including other programs.
| |
| The one connection per device limit does not apply when exclusively using the Phidget Webservice.
| |
| At the end of your program, don’t forget to call close to free any locks on the Phidget.
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=python>
| |
| | |
| interfaceKit.closePhidget()
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| ===Event Driven Programming===
| |
| | |
| We recommend the use of event driven programming when working with Phidgets.
| |
| In Python, we hook an event handler by defining the callback function and then calling a set handler function on it.
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=python>
| |
| | |
| def interfaceKitSensorChanged(e):
| |
| print (“Sensor %i: %i” % (e.index, e.value))
| |
| return 0
| |
| interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged)
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| With this, the code inside interfaceKitSensorChanged will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
| |
| The values from the report can be accessed from the PhidgetDataEvent object properties.
| |
| | |
| Certain events such as Attach and Detach belong to the base Phidget object and thus are common to
| |
| all types of Phidgets. Please refer to the API manual for a full list of events and their usage.
| |
| | |
| ===Working directly with the Phidget===
| |
| | |
| Some values can be directly read and set on the Phidget and used as an alternative to event driven programming.
| |
| Simply use the instance properties or call member functions such as getSensorValue(index) or setOutputState(index, state) for PhidgetInterfaceKits.
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=python>
| |
| | |
| interfaceKit.setOutputState(0, 1)
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
|
| |
|
| ===Working with multiple Phidgets=== | | === Python Module === |
| | * [https://cdn.phidgets.com/downloads/phidget22/libraries/any/Phidget22Python.zip Phidget Python module] |
|
| |
|
| Multiple Phidgets of the same type can easily be run inside the same program.
| | === Documentation === |
| In our case, it requires another instance of a PhidgetInterfaceKit to be defined and initialized.
| |
| The new instance can then be set up, opened and used in the same process as the previous one.
| |
| If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.
| |
|
| |
|
| ===Other Phidgets===
| | *{{Phidget22API}} (select Python from the drop-down menu) |
|
| |
|
| The design given in this document can also be followed for almost all Phidgets.
| | === Example Code === |
| For example, if you were using a PhidgetRFID instead of a PhidgetInterfacekit, you would declare an RFID instead of an InterfaceKit.
| |
| The functions and events available would change but they can be accessed in a similar manner.
| |
|
| |
|
| ==Building your Project==
| | *{{SampleCode|Python|Python Examples}} |
| Describe the different ways a project could be built using this language.
| |
|
| |
|
| ==Common Problems and Solutions/Workarounds== | | ===Libraries=== |
| Here you can put various frequent problems and our recommended solutions.
| | {{AllQuickDownloads}} |