<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.phidgets.com/docs/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chad</id>
	<title>Phidgets Support - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.phidgets.com/docs/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chad"/>
	<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/Special:Contributions/Chad"/>
	<updated>2026-04-27T12:00:02Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.8</generator>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28307</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28307"/>
		<updated>2017-07-13T22:12:27Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Opening a Manager Channel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, and continuously monitors channels as they attach and detach.&lt;br /&gt;
&lt;br /&gt;
Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or becomes available over the network), a manager attach event is fired for each channel available from the Phidget.  When the Phidget is removed from the system, a manger detach event is fired for each channel that is no longer available. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel has &#039;&#039;appeared&#039;&#039;, and the device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel, a manager event is not fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.&lt;br /&gt;
&lt;br /&gt;
As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; events will be fired when new Phidgets become available or existing Phidget are removed.&lt;br /&gt;
&lt;br /&gt;
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.&lt;br /&gt;
&lt;br /&gt;
What follows is an example of how to use a manager in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P attached&amp;quot;, mgrch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P detached&amp;quot;, mgrch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv) {&lt;br /&gt;
    PhidgetManagerHandle mgr;&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_create(&amp;amp;mgr);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);&lt;br /&gt;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_open(mgr);&lt;br /&gt;
&lt;br /&gt;
    /* Do something long running, like run a GUI threads */&lt;br /&gt;
 &lt;br /&gt;
    /* Now close and delete the manager */&lt;br /&gt;
    PhidgetManager_close(mgr);&lt;br /&gt;
    PhidgetManager_delete(&amp;amp;mgr);&lt;br /&gt;
    exit(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A further example would be &amp;quot;HelloWorld&amp;quot; for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.&lt;br /&gt;
&lt;br /&gt;
==Opening a Manager Channel==&lt;br /&gt;
&lt;br /&gt;
A manager channel passed to a Phidget Manager attach handler is assigned all of the matching parameters for the Phidget device and the device channel the manager channel refers to; however, the manager channel is not attached to the device channel, and is not receiving data and status information from the device.&lt;br /&gt;
&lt;br /&gt;
User code may {{Code|Open()}} a manager channel, and just like a channel created by the user code, the Phidget library will begin trying to match the manager channel with a device channel.  User code may also set attach and detach handlers on the manager channel, and set change event handlers.  The manager channel can be treated exactly the same as a user channel.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28290</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28290"/>
		<updated>2017-07-13T17:18:41Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, and continuously monitors channels as they attach and detach.&lt;br /&gt;
&lt;br /&gt;
Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or becomes available over the network), a manager attach event is fired for each channel available from the Phidget.  When the Phidget is removed from the system, a manger detach event is fired for each channel that is no longer available. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel has &#039;&#039;appeared&#039;&#039;, and the device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel, a manager event is not fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.&lt;br /&gt;
&lt;br /&gt;
As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; events will be fired when new Phidgets become available or existing Phidget are removed.&lt;br /&gt;
&lt;br /&gt;
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.&lt;br /&gt;
&lt;br /&gt;
What follows is an example of how to use a manager in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P attached&amp;quot;, mgrch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P detached&amp;quot;, mgrch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv) {&lt;br /&gt;
    PhidgetManagerHandle mgr;&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_create(&amp;amp;mgr);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);&lt;br /&gt;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_open(mgr);&lt;br /&gt;
&lt;br /&gt;
    /* Do something long running, like run a GUI threads */&lt;br /&gt;
 &lt;br /&gt;
    /* Now close and delete the manager */&lt;br /&gt;
    PhidgetManager_close(mgr);&lt;br /&gt;
    PhidgetManager_delete(&amp;amp;mgr);&lt;br /&gt;
    exit(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A further example would be &amp;quot;HelloWorld&amp;quot; for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.&lt;br /&gt;
&lt;br /&gt;
==Opening a Manager Channel==&lt;br /&gt;
&lt;br /&gt;
A manager channel passed to a Phidget Manager attach handler is assigned all of the matching parameters for the Phidget device and the device channel the manager channel refers to; however, the manager channel is not attached to the device channel, and is not receiving data and status information from the device.&lt;br /&gt;
&lt;br /&gt;
User code may {{Code|Open()}} a manager channel, and just like a channel created by the user code, the Phidget library will begin trying to match the manager channel with a device channel.  User code may also set attach and detach handlers on the manager channel, and set change event handlers.  The manager channel can be treated exactly the same as a user channel, except that the manager channel must never be deleted, and after calling {{Code|Close()}}, should not be referred to again.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28289</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28289"/>
		<updated>2017-07-13T17:15:29Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using the Phidget Manager */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.&lt;br /&gt;
&lt;br /&gt;
As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; events will be fired when new Phidgets become available or existing Phidget are removed.&lt;br /&gt;
&lt;br /&gt;
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.&lt;br /&gt;
&lt;br /&gt;
What follows is an example of how to use a manager in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P attached&amp;quot;, mgrch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle mgrch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P detached&amp;quot;, mgrch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv) {&lt;br /&gt;
    PhidgetManagerHandle mgr;&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_create(&amp;amp;mgr);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);&lt;br /&gt;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_open(mgr);&lt;br /&gt;
&lt;br /&gt;
    /* Do something long running, like run a GUI threads */&lt;br /&gt;
 &lt;br /&gt;
    /* Now close and delete the manager */&lt;br /&gt;
    PhidgetManager_close(mgr);&lt;br /&gt;
    PhidgetManager_delete(&amp;amp;mgr);&lt;br /&gt;
    exit(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A further example would be &amp;quot;HelloWorld&amp;quot; for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.&lt;br /&gt;
&lt;br /&gt;
==Opening a Manager Channel==&lt;br /&gt;
&lt;br /&gt;
A manager channel passed to a Phidget Manager attach handler is assigned all of the matching parameters for the Phidget device and the device channel the manager channel refers to; however, the manager channel is not attached to the device channel, and is not receiving data and status information from the device.&lt;br /&gt;
&lt;br /&gt;
User code may {{Code|Open()}} a manager channel, and just like a channel created by the user code, the Phidget library will begin trying to match the manager channel with a device channel.  User code may also set attach and detach handlers on the manager channel, and set change event handlers.  The manager channel can be treated exactly the same as a user channel, except that the manager channel must never be deleted, and after calling {{Code|Close()}}, should not be referred to again.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28288</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28288"/>
		<updated>2017-07-13T17:13:32Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Opening a Manager Channel= */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.&lt;br /&gt;
&lt;br /&gt;
As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; events will be fired when new Phidgets become available or existing Phidget are removed.&lt;br /&gt;
&lt;br /&gt;
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.&lt;br /&gt;
&lt;br /&gt;
What follows is an example of how to use a manager in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P attached&amp;quot;, devch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P detached&amp;quot;, devch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv) {&lt;br /&gt;
    PhidgetManagerHandle mgr;&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_create(&amp;amp;mgr);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);&lt;br /&gt;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_open(mgr);&lt;br /&gt;
&lt;br /&gt;
    /* Do something long running, like run a GUI threads */&lt;br /&gt;
 &lt;br /&gt;
    /* Now close and delete the manager */&lt;br /&gt;
    PhidgetManager_close(mgr);&lt;br /&gt;
    PhidgetManager_delete(&amp;amp;mgr);&lt;br /&gt;
    exit(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A further example would be &amp;quot;HelloWorld&amp;quot; for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.&lt;br /&gt;
&lt;br /&gt;
==Opening a Manager Channel==&lt;br /&gt;
&lt;br /&gt;
A manager channel passed to a Phidget Manager attach handler is assigned all of the matching parameters for the Phidget device and the device channel the manager channel refers to; however, the manager channel is not attached to the device channel, and is not receiving data and status information from the device.&lt;br /&gt;
&lt;br /&gt;
User code may {{Code|Open()}} a manager channel, and just like a channel created by the user code, the Phidget library will begin trying to match the manager channel with a device channel.  User code may also set attach and detach handlers on the manager channel, and set change event handlers.  The manager channel can be treated exactly the same as a user channel, except that the manager channel must never be deleted, and after calling {{Code|Close()}}, should not be referred to again.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28287</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28287"/>
		<updated>2017-07-13T17:04:43Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.&lt;br /&gt;
&lt;br /&gt;
As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; events will be fired when new Phidgets become available or existing Phidget are removed.&lt;br /&gt;
&lt;br /&gt;
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.&lt;br /&gt;
&lt;br /&gt;
What follows is an example of how to use a manager in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P attached&amp;quot;, devch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P detached&amp;quot;, devch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv) {&lt;br /&gt;
    PhidgetManagerHandle mgr;&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_create(&amp;amp;mgr);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);&lt;br /&gt;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_open(mgr);&lt;br /&gt;
&lt;br /&gt;
    /* Do something long running, like run a GUI threads */&lt;br /&gt;
 &lt;br /&gt;
    /* Now close and delete the manager */&lt;br /&gt;
    PhidgetManager_close(mgr);&lt;br /&gt;
    PhidgetManager_delete(&amp;amp;mgr);&lt;br /&gt;
    exit(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A further example would be &amp;quot;HelloWorld&amp;quot; for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.&lt;br /&gt;
&lt;br /&gt;
==Opening a Manager Channel===&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28286</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28286"/>
		<updated>2017-07-13T17:04:00Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using the Phidget Manager */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
Much like a Phidget channel, a Phidget Manager must be created and opened.  Refer to the {{Phidget22API}} for details.&lt;br /&gt;
&lt;br /&gt;
As soon as a manager is opened the Phidget library will fire an event for each device channel that is currently know to the system.  After those initial events, &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; events will be fired when new Phidgets become available or existing Phidget are removed.&lt;br /&gt;
&lt;br /&gt;
In the C programming language, it is not safe to refer to a {{Code|PhidgetHandle}} after an event handler returns, unless {{Code|Phidget_retain()}} is first called ({{Code|Phidget_open()}} and {{Code|Phidget_close()}} retain and release handlers automatically).  The {{Code|PhidgetHandle}} must be released with {{Code|Phidget_release()}} if it has been retained. The object oriented languages supported in the {{Phidget22API}} automatically retain and release handles.&lt;br /&gt;
&lt;br /&gt;
What follows is an example of how to use a manager in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrAttachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P attached&amp;quot;, devch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void CCONV&lt;br /&gt;
mgrDetachHandler(PhidgetManagerHandle mgr, void *ctx, PhidgetHandle devch) {&lt;br /&gt;
&lt;br /&gt;
    PhidgetLog_log(&amp;quot;%P detached&amp;quot;, devch);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int&lt;br /&gt;
main(int argc, char **argv) {&lt;br /&gt;
    PhidgetManagerHandle mgr;&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_create(&amp;amp;mgr);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_setOnAttachHandler(mgr, mgrAttachHandler, NULL);&lt;br /&gt;
    PhidgetManager_setOnDetachHandler(mgr, mgrDetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
    PhidgetManager_open(mgr);&lt;br /&gt;
&lt;br /&gt;
    /* Do something long running, like run a GUI threads */&lt;br /&gt;
 &lt;br /&gt;
    /* Now close and delete the manager */&lt;br /&gt;
    PhidgetManager_close(mgr);&lt;br /&gt;
    PhidgetManager_delete(&amp;amp;mgr);&lt;br /&gt;
    exit(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A further example would be &amp;quot;HelloWorld&amp;quot; for most [[Software Overview#Language Support|supported languages]] uses the Phidget Manager.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28285</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28285"/>
		<updated>2017-07-13T16:42:03Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Purpose */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The primary function of a Phidget Manager is to fire an attach event for each new device channel when a Phidget becomes available to a system, and to fire a detach event for each device channel that is no longer available when the Phidget is removed from the system.  For example, when a 1044_0 - PhidgetSpatial is connected, a Phidget Manager would fire three attach events, one for each device channel, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
As with any other Phidget software object, the Phidget Manager must be [[General Phidget Programming#Opening the Phidget|opened]], then [[General Phidget Programming#Attaching the Phidget|attached]] within your code. Here&#039;s an example of how the manager is used in C:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
&lt;br /&gt;
     // Define the manager handle and create&lt;br /&gt;
     PhidgetManagerHandle device = 0;&lt;br /&gt;
     PhidgetManager_create(&amp;amp;device);&lt;br /&gt;
&lt;br /&gt;
     // Set up event handlers&lt;br /&gt;
     PhidgetManager_setOnAttachHandler(device, AttachHandler, NULL);&lt;br /&gt;
     PhidgetManager_setOnDetachHandler(device, DetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
     // Open the Phidget Manager&lt;br /&gt;
     PhidgetManager_open(device);&lt;br /&gt;
&lt;br /&gt;
     // Wait for attach/detach events&lt;br /&gt;
     printf(&amp;quot;Press Enter to end anytime...\n&amp;quot;);&lt;br /&gt;
     getchar();&lt;br /&gt;
&lt;br /&gt;
     // Close and clean up manager object&lt;br /&gt;
     PhidgetManager_close(device);&lt;br /&gt;
     PhidgetManager_delete(&amp;amp;device);&lt;br /&gt;
     return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CCONV AttachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {&lt;br /&gt;
&lt;br /&gt;
     const char *chanClass;&lt;br /&gt;
     // Define handles and spatial variables&lt;br /&gt;
     PhidgetAccelerometerHandle accHandle;&lt;br /&gt;
     PhidgetGyroscopeHandle gyrHandle;&lt;br /&gt;
     PhidgetMagnetometerHandle magHandle;&lt;br /&gt;
     double acceleration[3];&lt;br /&gt;
     double angularRate[3];&lt;br /&gt;
     double fieldStrength[3];&lt;br /&gt;
&lt;br /&gt;
     // Get the class of this generic Phidget object that has just attached&lt;br /&gt;
     Phidget_getChannelClassName(device, &amp;amp;chanClass);&lt;br /&gt;
     &lt;br /&gt;
     // If the channel is an accelerometer, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetAccelerometer&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Accelerometer object&lt;br /&gt;
          accHandle = (PhidgetAccelerometerHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Accelerometer-specific methods like getAcceleration:&lt;br /&gt;
          PhidgetAccelerometer_getAcceleration(accHandle, &amp;amp;acceleration);&lt;br /&gt;
          // Print out the acceleration values and close the object&lt;br /&gt;
          printf(&amp;quot;Accelerometer attached! X:%f Y:%f Z:%f \n&amp;quot;, acceleration[0], acceleration[1], acceleration[2]);&lt;br /&gt;
	  Phidget_close(accHandle);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     // If the channel is a gyroscope, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetGyroscope&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Gyroscope object&lt;br /&gt;
          gyrHandle = (PhidgetGyroscopeHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Gyroscope-specific methods like getAngularRate:&lt;br /&gt;
          PhidgetGyroscope_getAngularRate(gyrHandle, &amp;amp;angularRate);&lt;br /&gt;
          // Print out the angular rate values and close the object&lt;br /&gt;
          printf(&amp;quot;Gyroscope attached! X:%f Y:%f Z:%f \n&amp;quot;, angularRate[0], angularRate[1], angularRate[2]);&lt;br /&gt;
	  Phidget_close(gyrHandle);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     // If the channel is a magnetometer, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetMagnetometer&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Magnetometer object&lt;br /&gt;
          magHandle = (PhidgetMagnetometerHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Magnetometer-specific methods like getFieldStrength:&lt;br /&gt;
          PhidgetMagnetometer_getFieldStrength(magHandle, &amp;amp;fieldStrength);&lt;br /&gt;
          // Print out the field strength values and close the object&lt;br /&gt;
          printf(&amp;quot;Magnetometer attached! X:%f Y:%f Z:%f \n&amp;quot;, fieldStrength[0], fieldStrength[1], fieldStrength[2]);&lt;br /&gt;
	  Phidget_close(magHandle);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CCONV DetachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {&lt;br /&gt;
     &lt;br /&gt;
     const char *chanClass;&lt;br /&gt;
&lt;br /&gt;
     // Get an print the class of this generic Phidget object that has just detached&lt;br /&gt;
     Phidget_getChannelClassName(device, &amp;amp;chanClass);&lt;br /&gt;
     printf(&amp;quot;%s detached!\n&amp;quot;,chanClass);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For more sample code that uses the Phidget Manager, have a look at our &amp;quot;HelloWorld&amp;quot; programming examples, which are available for most of [[Software Overview#Language Support|the languages we support]].&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28284</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28284"/>
		<updated>2017-07-13T16:38:02Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Object Structure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The main function of the Phidget Manager is to launch events when a Phidget attaches or detaches, so you can execute code to prepare those Phidgets for use. For Phidgets that have multiple objects and/or channels (which happens to be most of them), a separate attach and detach event will trigger for each one. For example, when the 1044_0 - PhidgetSpatial is connected, the Phidget Manager would launch three attach events, one for each distinct object, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once an attach event triggers, the attach event handler will be called and your code will run. In order to interact with the object, you need to cast it as the correct type and open it. You can determine the correct type by using &amp;lt;code&amp;gt;getChannelClassName()&amp;lt;/code&amp;gt; (or the &amp;lt;code&amp;gt;channelClassName &amp;lt;/code&amp;gt; property if you&#039;re using and object-oriented language). Once cast, you can open it and begin to use methods and properties that belong to that specific object. In the next section we&#039;ll look at an example of a program implementing the process described by this diagram.&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
As with any other Phidget software object, the Phidget Manager must be [[General Phidget Programming#Opening the Phidget|opened]], then [[General Phidget Programming#Attaching the Phidget|attached]] within your code. Here&#039;s an example of how the manager is used in C:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
&lt;br /&gt;
     // Define the manager handle and create&lt;br /&gt;
     PhidgetManagerHandle device = 0;&lt;br /&gt;
     PhidgetManager_create(&amp;amp;device);&lt;br /&gt;
&lt;br /&gt;
     // Set up event handlers&lt;br /&gt;
     PhidgetManager_setOnAttachHandler(device, AttachHandler, NULL);&lt;br /&gt;
     PhidgetManager_setOnDetachHandler(device, DetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
     // Open the Phidget Manager&lt;br /&gt;
     PhidgetManager_open(device);&lt;br /&gt;
&lt;br /&gt;
     // Wait for attach/detach events&lt;br /&gt;
     printf(&amp;quot;Press Enter to end anytime...\n&amp;quot;);&lt;br /&gt;
     getchar();&lt;br /&gt;
&lt;br /&gt;
     // Close and clean up manager object&lt;br /&gt;
     PhidgetManager_close(device);&lt;br /&gt;
     PhidgetManager_delete(&amp;amp;device);&lt;br /&gt;
     return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CCONV AttachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {&lt;br /&gt;
&lt;br /&gt;
     const char *chanClass;&lt;br /&gt;
     // Define handles and spatial variables&lt;br /&gt;
     PhidgetAccelerometerHandle accHandle;&lt;br /&gt;
     PhidgetGyroscopeHandle gyrHandle;&lt;br /&gt;
     PhidgetMagnetometerHandle magHandle;&lt;br /&gt;
     double acceleration[3];&lt;br /&gt;
     double angularRate[3];&lt;br /&gt;
     double fieldStrength[3];&lt;br /&gt;
&lt;br /&gt;
     // Get the class of this generic Phidget object that has just attached&lt;br /&gt;
     Phidget_getChannelClassName(device, &amp;amp;chanClass);&lt;br /&gt;
     &lt;br /&gt;
     // If the channel is an accelerometer, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetAccelerometer&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Accelerometer object&lt;br /&gt;
          accHandle = (PhidgetAccelerometerHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Accelerometer-specific methods like getAcceleration:&lt;br /&gt;
          PhidgetAccelerometer_getAcceleration(accHandle, &amp;amp;acceleration);&lt;br /&gt;
          // Print out the acceleration values and close the object&lt;br /&gt;
          printf(&amp;quot;Accelerometer attached! X:%f Y:%f Z:%f \n&amp;quot;, acceleration[0], acceleration[1], acceleration[2]);&lt;br /&gt;
	  Phidget_close(accHandle);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     // If the channel is a gyroscope, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetGyroscope&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Gyroscope object&lt;br /&gt;
          gyrHandle = (PhidgetGyroscopeHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Gyroscope-specific methods like getAngularRate:&lt;br /&gt;
          PhidgetGyroscope_getAngularRate(gyrHandle, &amp;amp;angularRate);&lt;br /&gt;
          // Print out the angular rate values and close the object&lt;br /&gt;
          printf(&amp;quot;Gyroscope attached! X:%f Y:%f Z:%f \n&amp;quot;, angularRate[0], angularRate[1], angularRate[2]);&lt;br /&gt;
	  Phidget_close(gyrHandle);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     // If the channel is a magnetometer, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetMagnetometer&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Magnetometer object&lt;br /&gt;
          magHandle = (PhidgetMagnetometerHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Magnetometer-specific methods like getFieldStrength:&lt;br /&gt;
          PhidgetMagnetometer_getFieldStrength(magHandle, &amp;amp;fieldStrength);&lt;br /&gt;
          // Print out the field strength values and close the object&lt;br /&gt;
          printf(&amp;quot;Magnetometer attached! X:%f Y:%f Z:%f \n&amp;quot;, fieldStrength[0], fieldStrength[1], fieldStrength[2]);&lt;br /&gt;
	  Phidget_close(magHandle);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CCONV DetachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {&lt;br /&gt;
     &lt;br /&gt;
     const char *chanClass;&lt;br /&gt;
&lt;br /&gt;
     // Get an print the class of this generic Phidget object that has just detached&lt;br /&gt;
     Phidget_getChannelClassName(device, &amp;amp;chanClass);&lt;br /&gt;
     printf(&amp;quot;%s detached!\n&amp;quot;,chanClass);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For more sample code that uses the Phidget Manager, have a look at our &amp;quot;HelloWorld&amp;quot; programming examples, which are available for most of [[Software Overview#Language Support|the languages we support]].&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28283</id>
		<title>Phidget Manager</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Manager&amp;diff=28283"/>
		<updated>2017-07-13T16:37:40Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
The Phidget Manager is an interface into the device channels available to the Phidget library.  The API is strictly asynchronous, a continuously monitors channels as they attach and detach.  Each Phidget exports one or more device channels, and when a Phidget is plugged into a system (or become available over the network), an attach event is fired for each channel available on that Phidget. &lt;br /&gt;
&lt;br /&gt;
It is important to understand the concepts of &#039;&#039;&#039;attach&#039;&#039;&#039; and &#039;&#039;&#039;detach&#039;&#039;&#039; as the they relate to the manager.  A manager attach does not imply that a user channel has attached to a device channel, but that the device channel have become available.  The device channel is now ready to be attached to a user channel.  When a user channel closes and detaches from a device channel a manager event will not be fired.  A manager &#039;&#039;&#039;detach&#039;&#039;&#039; event is fired when the Phidget device is removed from the system.&lt;br /&gt;
&lt;br /&gt;
==Object Structure==&lt;br /&gt;
&lt;br /&gt;
You are probably already familiar with Phidgets software objects, where a single set of functions for a class of hardware are contained in one object. For example, the VoltageInput object:&lt;br /&gt;
&lt;br /&gt;
[[Image:voltageInput_object.jpg|300px|link=|alt=]]&lt;br /&gt;
&lt;br /&gt;
The methods for the Phidget Manager are contained in the same way, but it does not belong to any particular Phidget device. It exists separately, so you can have an active Phidget Manager object even when there are no Phidgets attached. &lt;br /&gt;
&lt;br /&gt;
[[Image:manager.jpg|300px|link=|alt=]]&lt;br /&gt;
&lt;br /&gt;
You can find the complete API for the Phidget Manager in the {{Phidget22API}}. Select your language from the first drop-down box and select &#039;&#039;&#039;Manager API&#039;&#039;&#039; from the second box.&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
&lt;br /&gt;
The main function of the Phidget Manager is to launch events when a Phidget attaches or detaches, so you can execute code to prepare those Phidgets for use. For Phidgets that have multiple objects and/or channels (which happens to be most of them), a separate attach and detach event will trigger for each one. For example, when the 1044_0 - PhidgetSpatial is connected, the Phidget Manager would launch three attach events, one for each distinct object, as illustrated below:&lt;br /&gt;
&lt;br /&gt;
[[Image:manager22.jpg|link=|1000px|alt=]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once an attach event triggers, the attach event handler will be called and your code will run. In order to interact with the object, you need to cast it as the correct type and open it. You can determine the correct type by using &amp;lt;code&amp;gt;getChannelClassName()&amp;lt;/code&amp;gt; (or the &amp;lt;code&amp;gt;channelClassName &amp;lt;/code&amp;gt; property if you&#039;re using and object-oriented language). Once cast, you can open it and begin to use methods and properties that belong to that specific object. In the next section we&#039;ll look at an example of a program implementing the process described by this diagram.&lt;br /&gt;
&lt;br /&gt;
==Using the Phidget Manager==&lt;br /&gt;
&lt;br /&gt;
As with any other Phidget software object, the Phidget Manager must be [[General Phidget Programming#Opening the Phidget|opened]], then [[General Phidget Programming#Attaching the Phidget|attached]] within your code. Here&#039;s an example of how the manager is used in C:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
&lt;br /&gt;
     // Define the manager handle and create&lt;br /&gt;
     PhidgetManagerHandle device = 0;&lt;br /&gt;
     PhidgetManager_create(&amp;amp;device);&lt;br /&gt;
&lt;br /&gt;
     // Set up event handlers&lt;br /&gt;
     PhidgetManager_setOnAttachHandler(device, AttachHandler, NULL);&lt;br /&gt;
     PhidgetManager_setOnDetachHandler(device, DetachHandler, NULL);&lt;br /&gt;
&lt;br /&gt;
     // Open the Phidget Manager&lt;br /&gt;
     PhidgetManager_open(device);&lt;br /&gt;
&lt;br /&gt;
     // Wait for attach/detach events&lt;br /&gt;
     printf(&amp;quot;Press Enter to end anytime...\n&amp;quot;);&lt;br /&gt;
     getchar();&lt;br /&gt;
&lt;br /&gt;
     // Close and clean up manager object&lt;br /&gt;
     PhidgetManager_close(device);&lt;br /&gt;
     PhidgetManager_delete(&amp;amp;device);&lt;br /&gt;
     return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CCONV AttachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {&lt;br /&gt;
&lt;br /&gt;
     const char *chanClass;&lt;br /&gt;
     // Define handles and spatial variables&lt;br /&gt;
     PhidgetAccelerometerHandle accHandle;&lt;br /&gt;
     PhidgetGyroscopeHandle gyrHandle;&lt;br /&gt;
     PhidgetMagnetometerHandle magHandle;&lt;br /&gt;
     double acceleration[3];&lt;br /&gt;
     double angularRate[3];&lt;br /&gt;
     double fieldStrength[3];&lt;br /&gt;
&lt;br /&gt;
     // Get the class of this generic Phidget object that has just attached&lt;br /&gt;
     Phidget_getChannelClassName(device, &amp;amp;chanClass);&lt;br /&gt;
     &lt;br /&gt;
     // If the channel is an accelerometer, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetAccelerometer&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Accelerometer object&lt;br /&gt;
          accHandle = (PhidgetAccelerometerHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Accelerometer-specific methods like getAcceleration:&lt;br /&gt;
          PhidgetAccelerometer_getAcceleration(accHandle, &amp;amp;acceleration);&lt;br /&gt;
          // Print out the acceleration values and close the object&lt;br /&gt;
          printf(&amp;quot;Accelerometer attached! X:%f Y:%f Z:%f \n&amp;quot;, acceleration[0], acceleration[1], acceleration[2]);&lt;br /&gt;
	  Phidget_close(accHandle);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     // If the channel is a gyroscope, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetGyroscope&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Gyroscope object&lt;br /&gt;
          gyrHandle = (PhidgetGyroscopeHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Gyroscope-specific methods like getAngularRate:&lt;br /&gt;
          PhidgetGyroscope_getAngularRate(gyrHandle, &amp;amp;angularRate);&lt;br /&gt;
          // Print out the angular rate values and close the object&lt;br /&gt;
          printf(&amp;quot;Gyroscope attached! X:%f Y:%f Z:%f \n&amp;quot;, angularRate[0], angularRate[1], angularRate[2]);&lt;br /&gt;
	  Phidget_close(gyrHandle);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
     // If the channel is a magnetometer, handle it accordingly:&lt;br /&gt;
     if(strcmp(chanClass,&amp;quot;PhidgetMagnetometer&amp;quot;) == 0) {&lt;br /&gt;
&lt;br /&gt;
          // Cast the generic Phidget object into a Phidget Magnetometer object&lt;br /&gt;
          magHandle = (PhidgetMagnetometerHandle) device;&lt;br /&gt;
          // Now that the channel is cast, we can call Magnetometer-specific methods like getFieldStrength:&lt;br /&gt;
          PhidgetMagnetometer_getFieldStrength(magHandle, &amp;amp;fieldStrength);&lt;br /&gt;
          // Print out the field strength values and close the object&lt;br /&gt;
          printf(&amp;quot;Magnetometer attached! X:%f Y:%f Z:%f \n&amp;quot;, fieldStrength[0], fieldStrength[1], fieldStrength[2]);&lt;br /&gt;
	  Phidget_close(magHandle);&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void CCONV DetachHandler(PhidgetManagerHandle manager, void *userptr, PhidgetHandle device) {&lt;br /&gt;
     &lt;br /&gt;
     const char *chanClass;&lt;br /&gt;
&lt;br /&gt;
     // Get an print the class of this generic Phidget object that has just detached&lt;br /&gt;
     Phidget_getChannelClassName(device, &amp;amp;chanClass);&lt;br /&gt;
     printf(&amp;quot;%s detached!\n&amp;quot;,chanClass);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For more sample code that uses the Phidget Manager, have a look at our &amp;quot;HelloWorld&amp;quot; programming examples, which are available for most of [[Software Overview#Language Support|the languages we support]].&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28282</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28282"/>
		<updated>2017-07-13T15:16:16Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are created and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes made to a dictionary are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
On Linux, refer to the README for information on how to enable the dictionary feature in the [[Phidget Network Server]], and for the syntax of dictionary configuration files.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label, and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
A common mistake is to assume an event will be delivered to the channel that triggered the event.  This is not the case.  For example, when a channel adds a key, that channel&#039;s add event handler is not called.&lt;br /&gt;
&lt;br /&gt;
It is an error to {{Code|Add()}} a key that already exists in the dictionary.  {{Code|Set()}} should be used to update existing keys, creating the key if it does not already exist.&lt;br /&gt;
&lt;br /&gt;
There is no way to directly list all of the keys in a dictionary.  The {{Code|Scan()}} method allows code to access the existing keys, but only returns the number of keys that can be held in a limited sized buffer.  This interface was chosen as the dictionary could contain more keys than could reasonably returned in a single network request.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28281</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28281"/>
		<updated>2017-07-12T20:48:11Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes made to a dictionary are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
On Linux, refer to the README for information on how to enable the dictionary feature in the [[Phidget Network Server]], and for the syntax of dictionary configuration files.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label, and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
A common mistake is to assume an event will be delivered to the channel that triggered the event.  This is not the case.  For example, when a channel adds a key, that channel&#039;s add event handler is not called.&lt;br /&gt;
&lt;br /&gt;
It is an error to {{Code|Add()}} a key that already exists in the dictionary.  {{Code|Set()}} should be used to update existing keys, creating the key if it does not already exist.&lt;br /&gt;
&lt;br /&gt;
There is no way to directly list all of the keys in a dictionary.  The {{Code|Scan()}} method allows code to access the existing keys, but only returns the number of keys that can be held in a limited sized buffer.  This interface was chosen as the dictionary could contain more keys than could reasonably returned in a single network request.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28280</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28280"/>
		<updated>2017-07-12T20:34:50Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using The Dictionary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes made to a dictionary are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
On Linux, refer to the README for information on how to enable the dictionary feature in the [[Phidget Network Server]], and for the syntax of dictionary configuration files.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label, and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28279</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28279"/>
		<updated>2017-07-12T20:34:05Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using The Dictionary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes made to a dictionary are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
On Linux, refer to the README for information on how to enable the dictionary feature in the [[Phidget Network Server]], and for the syntax of dictionary configuration files.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label, and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28278</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28278"/>
		<updated>2017-07-12T20:33:06Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Creating a Dictionary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes made to a dictionary are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
On Linux, refer to the README for information on how to enable the dictionary feature in the [[Phidget Network Server]], and for the syntax of dictionary configuration files.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28277</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28277"/>
		<updated>2017-07-12T20:28:49Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes made to a dictionary are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28276</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28276"/>
		<updated>2017-07-12T20:26:30Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using The Dictionary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes to dictionaries are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Creating a Dictionary==&lt;br /&gt;
Dictionaries are managed by the [[Phidget Network Server]].  To create a dictionary, go to the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039;&#039; tab.  There are options to create and manage dictionaries.  The server must be enabled for clients to access dictionaries.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To access a dictionary, create a channel, set the serial number or the label and open the channel.&lt;br /&gt;
To &amp;quot;listen&amp;quot; for changes to the Dictionary,  {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event handlers can be set.  Refer to the {{Phidget22API}} to see the methods supported by Dictionary. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
char val[32];&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_add(dict, &amp;quot;my_key&amp;quot;, &amp;quot;my_value&amp;quot;); // add a key that does not already exist &lt;br /&gt;
PhidgetDictionary_get(dict, &amp;quot;my_key&amp;quot;, val, sizeof (val)); // get the value back&lt;br /&gt;
printf(&amp;quot;Value: %s\n&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
PhidgetDictionary_remove(dict, &amp;quot;my_key&amp;quot;); // remove the key&lt;br /&gt;
&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28275</id>
		<title>Phidget Dictionary</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Dictionary&amp;diff=28275"/>
		<updated>2017-07-12T20:12:57Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
==General Overview==&lt;br /&gt;
&lt;br /&gt;
Dictionary is Phidget channel class that exports a key-value pair database.  Dictionaries are create and made accessible by the [[Phidget Network Server]].  A Dictionary channel is created and opened like any other Phidget channel.  Matching is done using either the serial number or the label assigned to the dictionary.&lt;br /&gt;
&lt;br /&gt;
The backing pseudo-device that exports the Dictionary device channel is maintained by the [[Phidget Network Server]], and is created when the server starts.  When the server exits, the pseudo-device is deleted, and any changes to dictionaries are lost.&lt;br /&gt;
&lt;br /&gt;
The basic functionality of a Dictionary is to maintain configuration information in a central location.  Multiple clients can attach to the same dictionary, and set handlers for &#039;&#039;add&#039;&#039;, &#039;&#039;update&#039;&#039; and &#039;&#039;delete&#039;&#039; events, making the dictionary useful for monitoring and communicating state.&lt;br /&gt;
&lt;br /&gt;
==Using The Dictionary==&lt;br /&gt;
&lt;br /&gt;
To use the Dictionary, you would create a Dictionary object within your code, just like you would any other Phidget software object. To &amp;quot;listen&amp;quot; to changes of the value associated with a key, the Dictionary has a {{Code|onAdd()}}, {{Code|onRemove()}}, and {{Code|onUpdate()}} event functions (exact name dependant on programming language used). You can write code in these event handlers to react differently depending on which pair is being changed. &lt;br /&gt;
&lt;br /&gt;
Go into the [[Phidget Control Panel]] and click on the &#039;&#039;&#039;Network Server&#039;&#039; tab and ensure the server is started. &lt;br /&gt;
&lt;br /&gt;
Here&#039;s an example of how you might use the dictionary in a C program:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// Declare the dictionary handle&lt;br /&gt;
PhidgetDictionaryHandle dict;&lt;br /&gt;
&lt;br /&gt;
// Create the new dictionary object using the handle&lt;br /&gt;
PhidgetDictionary_create(&amp;amp;dict);&lt;br /&gt;
PhidgetDictionary_addDictionary(5000,&amp;quot;myDictionary&amp;quot;); //set a serial number and label for the dictionary&lt;br /&gt;
&lt;br /&gt;
// Open connection to the dictionary using the serial number&lt;br /&gt;
Phidget_setDeviceSerialNumber(dict, 5000);&lt;br /&gt;
Phidget_open(dict);&lt;br /&gt;
&lt;br /&gt;
// Add a key-value pair to the dictionary&lt;br /&gt;
char *key1 = &amp;quot;001&amp;quot;;&lt;br /&gt;
char *val1 = &amp;quot;first&amp;quot;;&lt;br /&gt;
PhidgetDictionary_add(dict,key1,val1); &lt;br /&gt;
&lt;br /&gt;
// Access and print a value from the dictionary based on a given key&lt;br /&gt;
char newValue[20];&lt;br /&gt;
PhidgetDictionary_get(dict, key1, &amp;amp;newValue,20); // &#039;20&#039; refers to the length of the buffer used to store the result&lt;br /&gt;
printf(&amp;quot;Value: %s&amp;quot;,newValue);&lt;br /&gt;
&lt;br /&gt;
// Remove a pair from the dictionary based on a given key&lt;br /&gt;
PhidgetDictionary_remove(dict, key1);&lt;br /&gt;
&lt;br /&gt;
// Close and clean up the dictionary object&lt;br /&gt;
Phidget_close(dict);&lt;br /&gt;
PhidgetDictionary_delete(&amp;amp;dict);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Have a look at the {{Phidget22API}} documentation to learn how to use these methods in the language of your choice. Select your preferred language from the first drop-down box, and select &#039;&#039;&#039;Dictionary API&#039;&#039;&#039; from the second box.&lt;br /&gt;
&lt;br /&gt;
When you create a dictionary in your code, the [[Phidget Control Panel]] will also be able to see it if you have remote Phidgets enabled in the options. Here&#039;s what it will look like:&lt;br /&gt;
&lt;br /&gt;
[[File:dictionary-panel.jpg|link=|420px|left]]&lt;br /&gt;
[[File:dictionary-example.jpg|420px|link=]]&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the left, you can see that dictionaries appear just like attached and remote Phidget devices do. On the right, you can see the contents of the dictionary after double-clicking on it. You can add and remove tags from here as well.&lt;br /&gt;
&lt;br /&gt;
Interestingly, you don&#039;t even need a Phidget to use the Dictionary!  You can use our libraries and the [[Phidget Network Server]] without any Phidgets. Usually, there&#039;s not much to broadcast on the Network Server without a Phidget, but the Dictionary is the exception. With it, you&#039;ll have access to centralized key-value pair database management, pre-written, that can work across many computers on a local network.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Language_-_C&amp;diff=28274</id>
		<title>Language - C</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Language_-_C&amp;diff=28274"/>
		<updated>2017-07-12T19:24:47Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Getting started with C/C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Quick Downloads ==&lt;br /&gt;
&lt;br /&gt;
=== Documentation ===&lt;br /&gt;
&lt;br /&gt;
*{{Phidget22API}}  (select C from the drop-down menu)&lt;br /&gt;
&lt;br /&gt;
=== Example Code ===&lt;br /&gt;
&lt;br /&gt;
*{{SampleCode|C|C/C++ Examples}}&lt;br /&gt;
&lt;br /&gt;
===Libraries===&lt;br /&gt;
&lt;br /&gt;
{{AllQuickDownloads}}&lt;br /&gt;
&lt;br /&gt;
== Getting started with C/C++ ==&lt;br /&gt;
Welcome to using Phidgets with C/C++! By using C/C++, you will have access to the complete Phidget22 API, including events. Example code is also provided for each Phidget channel class.&lt;br /&gt;
&lt;br /&gt;
If developing for Windows, keep reading; otherwise, select an operating system:&lt;br /&gt;
*[[#macOS | macOS]]&lt;br /&gt;
*[[#Linux | Linux]]&lt;br /&gt;
&lt;br /&gt;
== Windows ==&lt;br /&gt;
{{Windows_Languages}}&lt;br /&gt;
&lt;br /&gt;
===Visual Studio===&lt;br /&gt;
====Use our examples====&lt;br /&gt;
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install [https://www.visualstudio.com/ Microsoft Visual Studio].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now that you have Microsoft Visual Studio installed, select an example that will work with your Phidget:&lt;br /&gt;
*{{SampleCode|C|C/C++ Examples}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Open the example project and start the example by pressing the &#039;&#039;Local Windows Debugger&#039;&#039; button:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image: c_vs_run.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The application will open the Phidget, list basic information about the Phidget, and demonstrate the Phidget&#039;s functionality. Here is an example of an Accelerometer channel on a Spatial Phidget:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image: c_vs_output.PNG|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!&lt;br /&gt;
&lt;br /&gt;
====Configure your project====&lt;br /&gt;
When you are building a project from scratch, or adding Phidget functionality to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. To begin:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a new Win32 Console application:&lt;br /&gt;
&lt;br /&gt;
[[Image:C_vs_newproject.PNG|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After creating a project with the default settings, access the project&#039;s properties:&lt;br /&gt;
&lt;br /&gt;
[[Image:C_vs_properties.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, navigate to Configuration Properties -&amp;gt; C/C++ -&amp;gt; General and add the following line to the additional include directories:&lt;br /&gt;
*C:\Program Files\Phidgets\Phidget22&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:C_vs_additionalinclude.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Navigate to Configuration Properties -&amp;gt; Linker -&amp;gt; Input and add the following line to the additional dependencies:&lt;br /&gt;
*C:\Program Files\Phidgets\Phidget22\phidget22.lib&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:C_vs_additionadepend.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
Finally, include the Phidget library in your code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;C&#039;&amp;gt;&lt;br /&gt;
#include &amp;lt;phidget22.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Success! The project now has access to Phidgets. Next, view the [[#Write Code | write your own code]] section located below.&lt;br /&gt;
&lt;br /&gt;
===GCC===&lt;br /&gt;
====Cygwin/MinGW====&lt;br /&gt;
=====Use our examples=====&lt;br /&gt;
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install either [http://www.mingw.org/ MinGW] or [https://www.cygwin.com/ Cygwin].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now that you have either MinGW or Cygwin installed, select an example that will work with your Phidget:&lt;br /&gt;
*{{SampleCode|C|C/C++ Examples}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are using Cygwin, navigate to the folder where the example is and open the command prompt. Enter the following command to compile the example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
gcc example.c -o example -I&amp;quot;/cygdrive/c/Program Files/Phidgets/Phidget22&amp;quot; -L&amp;quot;/cygdrive/c/Program Files/Phidgets/Phidget22/x86&amp;quot; -lphidget22&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you are using MinGW, navigate to the folder where the example is and open the command prompt. Enter the following command to compile the example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
gcc example.c -o example -I&amp;quot;C:/Program Files/Phidgets/Phidget22&amp;quot; -L&amp;quot;C:/Program Files/Phidgets/Phidget22/x86&amp;quot; -lphidget22&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After running the commands above for either Cygwin or MinGW, an executable file called &#039;&#039;example.exe&#039;&#039; will be created. Enter the following command to run the example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
example.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You should now have the example up and running. When you are ready, the next step is configuring your project and writing your own code!&lt;br /&gt;
&lt;br /&gt;
=====Configure your project=====&lt;br /&gt;
When you are building a project from scratch, or adding Phidget functionality to an exisiting project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library.&lt;br /&gt;
&lt;br /&gt;
To include the Phidget C/C++ library, add the following line to your code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;C&#039;&amp;gt;&lt;br /&gt;
#include &amp;lt;phidget22.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can now compile the file as shown in the previous section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to Phidgets. Next, view the [[#Write Code | write your own code]] section located below.&lt;br /&gt;
&lt;br /&gt;
===Code::Blocks===&lt;br /&gt;
====Use our examples====&lt;br /&gt;
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install [http://www.codeblocks.org/downloads Code::Blocks].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now that you have Code::Blocks installed, select an example that will work with your Phidget:&lt;br /&gt;
*{{SampleCode|C|C/C++ Examples}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Open the example in Code::Blocks (you do not need to create a new project) and navigate to Settings -&amp;gt; Compiler... as shown in the image below:&lt;br /&gt;
&lt;br /&gt;
[[Image:C_codeblocks_settings.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
From the Global compiler settings screen, navigate to Search directories -&amp;gt; Compiler and add the following directory:&lt;br /&gt;
*C:\Program Files\Phidgets\Phidget22&lt;br /&gt;
&lt;br /&gt;
[[Image:C_codeblocks_compiler.PNG|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, select Search directories -&amp;gt; Linker and add the following directory:&lt;br /&gt;
*C:\Program Files\Phidgets\Phidget22\x86&lt;br /&gt;
&lt;br /&gt;
[[Image:C_codeblocks_linker.PNG|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, from the Global compiler settings screen, navigate to Linker settings and add the following line:&lt;br /&gt;
*phidget22&lt;br /&gt;
&lt;br /&gt;
[[Image:C_codeblocks_libraries.PNG|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can now build and run the example:&lt;br /&gt;
&lt;br /&gt;
[[Image:C_codeblocks_run.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!&lt;br /&gt;
&lt;br /&gt;
====Configure your project====&lt;br /&gt;
When you are building a project from scratch, or adding Phidget functionality to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To include the Phidget C/C++ library, add the following line to your code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;C&#039;&amp;gt;&lt;br /&gt;
#include &amp;lt;phidget22.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can now compile the file as shown in the previous section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to Phidgets. Next, view the [[#Write Code | write your own code]] section located below.&lt;br /&gt;
&lt;br /&gt;
==macOS==&lt;br /&gt;
{{macOS_Languages}}&lt;br /&gt;
===GCC===&lt;br /&gt;
====Use our examples====&lt;br /&gt;
One of the best ways to start programming with Phidgets is to use our example code as a guide. You likely have gcc installed on your macOS machine already, but if not, you can easily get it by downloading [https://developer.apple.com/xcode/ Xcode].&lt;br /&gt;
&lt;br /&gt;
Next, select an example that will work with your Phidget:&lt;br /&gt;
*{{SampleCode|C|C/C++ Examples}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To compile the example program, enter the following command in the terminal:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
gcc example.c -o example -F /Library/Frameworks -framework Phidget22 -I /Library/Frameworks/Phidget22.framework/Headers&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, run the program by entering the following command in the terminal:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
./example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:c_mac_gcc.png|link=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!&lt;br /&gt;
&lt;br /&gt;
====Configure your project====&lt;br /&gt;
When you are building a project from scratch, or adding Phidget functionality to an exisiting project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library.&lt;br /&gt;
&lt;br /&gt;
To include the Phidget C/C++ library, simply add the following line to your code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;C&#039;&amp;gt;&lt;br /&gt;
#include &amp;lt;phidget22.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can now compile the file as shown in the previous section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to Phidgets. Next, view the [[#Write Code | write your own code]] section located below.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
{{Linux_Languages}}&lt;br /&gt;
===GCC===&lt;br /&gt;
====Use our examples====&lt;br /&gt;
One of the best ways to start programming with Phidgets is to use our example code as a guide. You likely have gcc installed on your Linux machine already, but if not, you can easily get it by entering the following command in the terminal:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;C&#039;&amp;gt;&lt;br /&gt;
apt-get install gcc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, select an example that will work with your Phidget:&lt;br /&gt;
*{{SampleCode|C|C/C++ Examples}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To compile the example, enter the following command in the terminal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
gcc example.c -o example -lphidget22&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After compiling, you can run the program by entering the following command in the terminal:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;bash&#039;&amp;gt;&lt;br /&gt;
./example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You should now have the example up and running. When you are ready, the next step is configuring your project and writing your own code!&lt;br /&gt;
&lt;br /&gt;
====Configure your project====&lt;br /&gt;
When you are building a project from scratch, or adding Phidget functionality to an exisiting project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library.&lt;br /&gt;
&lt;br /&gt;
To include the Phidget C/C++ library, simply add the following line to your code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;C&#039;&amp;gt;&lt;br /&gt;
#include &amp;lt;phidget22.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can now compile the file as shown in the previous section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to Phidgets. Next, view the [[#Write Code | write your own code]] section located below.&lt;br /&gt;
&lt;br /&gt;
==Write Code==&lt;br /&gt;
You&#039;ve followed the instructions above for your operating system and now have a working example. Next, we will show you how the example was created and how it works by getting into the code. When you are ready, head to our [[Phidget Programming Basics]] page. There you will find code examples writen in C/C++ and you will be writing your own code in no time!&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28273</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28273"/>
		<updated>2017-07-12T19:21:21Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Use Event Handlers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Check Results ==&lt;br /&gt;
Almost every method in the {{Phidget22API}} returns a result code, or throws an exception if the language supports exceptions.  The result code must be checked, and any exception caught, to ensure each operation is successful.  Ignoring results will in the best case cause programs to crash, and in the worst, lead to invalid or unexpected behavior.&lt;br /&gt;
&lt;br /&gt;
== Use Event Handlers ==&lt;br /&gt;
The Phidget library supports both polling channel properties to determine state and to get current data values, and the library supports firing events when states change and data is received.  Reading channel properties directly is useful to determine the state or current data value at a point in time, but is difficult to implement correctly.  Event handlers are more predicable and easier to implement correctly.  Unless a program has a specific and obvious reason to access properties directly, event handlers should be used.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
Events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in another thread should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using.  After the class, there are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;hub port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be functionally the same; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel as other clients will still be able to attach to the device channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An attach event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches temporarily, those properties will revert to defaults during the next attach.  Setting properties within the attach handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when a program exits, the channel will be released; however, it is good practice to close the channel when you are finished as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment executes a program internally, so if {{Code|Close()}} is not called, the LabVIEW environment will continue to tie up the channel despite the program having been stopped.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28272</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28272"/>
		<updated>2017-07-12T19:12:49Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Don&amp;#039;t Forget to Close() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Use Event Handlers ==&lt;br /&gt;
The Phidget library supports both polling channel properties to determine state and to get current data values, and the library supports firing events when states change and data is received.  Reading channel properties directly is useful to determine the state or current data value at a point in time, but is difficult to implement correctly.  Event handlers are more predicable and easier to implement correctly.  Unless a program has a specific and obvious reason to access properties directly, event handlers should be used.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
Events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in another thread should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using.  After the class, there are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;hub port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be functionally the same; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel as other clients will still be able to attach to the device channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An attach event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches temporarily, those properties will revert to defaults during the next attach.  Setting properties within the attach handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when a program exits, the channel will be released; however, it is good practice to close the channel when you are finished as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment executes a program internally, so if {{Code|Close()}} is not called, the LabVIEW environment will continue to tie up the channel despite the program having been stopped.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28271</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28271"/>
		<updated>2017-07-12T19:08:31Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Set Properties in the Attach Handler */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Use Event Handlers ==&lt;br /&gt;
The Phidget library supports both polling channel properties to determine state and to get current data values, and the library supports firing events when states change and data is received.  Reading channel properties directly is useful to determine the state or current data value at a point in time, but is difficult to implement correctly.  Event handlers are more predicable and easier to implement correctly.  Unless a program has a specific and obvious reason to access properties directly, event handlers should be used.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
Events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in another thread should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using.  After the class, there are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;hub port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be functionally the same; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel as other clients will still be able to attach to the device channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An attach event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches temporarily, those properties will revert to defaults during the next attach.  Setting properties within the attach handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when your program exits, the channel will be released; however, it is good practice to close a channel when you are finished with it as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment merely interprets your program, so if you do not call {{Code|Close()}}, the LabVIEW environment will continue to tie up the channel despite having stopped the program you&#039;re working on.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28270</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28270"/>
		<updated>2017-07-12T19:05:56Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* When In Doubt, Open Remotely */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Use Event Handlers ==&lt;br /&gt;
The Phidget library supports both polling channel properties to determine state and to get current data values, and the library supports firing events when states change and data is received.  Reading channel properties directly is useful to determine the state or current data value at a point in time, but is difficult to implement correctly.  Event handlers are more predicable and easier to implement correctly.  Unless a program has a specific and obvious reason to access properties directly, event handlers should be used.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
Events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in another thread should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using.  After the class, there are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;hub port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be functionally the same; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel as other clients will still be able to attach to the device channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;attach&#039;&#039;&#039; event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches those properties will revert to defaults.  Setting properties within the &#039;&#039;&#039;attach&#039;&#039;&#039; handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when your program exits, the channel will be released; however, it is good practice to close a channel when you are finished with it as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment merely interprets your program, so if you do not call {{Code|Close()}}, the LabVIEW environment will continue to tie up the channel despite having stopped the program you&#039;re working on.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28269</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28269"/>
		<updated>2017-07-12T19:02:57Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Be Specific With Open() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Use Event Handlers ==&lt;br /&gt;
The Phidget library supports both polling channel properties to determine state and to get current data values, and the library supports firing events when states change and data is received.  Reading channel properties directly is useful to determine the state or current data value at a point in time, but is difficult to implement correctly.  Event handlers are more predicable and easier to implement correctly.  Unless a program has a specific and obvious reason to access properties directly, event handlers should be used.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
Events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in another thread should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using.  After the class, there are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;hub port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be the same as the same actual device channel would be attached; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;attach&#039;&#039;&#039; event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches those properties will revert to defaults.  Setting properties within the &#039;&#039;&#039;attach&#039;&#039;&#039; handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when your program exits, the channel will be released; however, it is good practice to close a channel when you are finished with it as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment merely interprets your program, so if you do not call {{Code|Close()}}, the LabVIEW environment will continue to tie up the channel despite having stopped the program you&#039;re working on.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28268</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28268"/>
		<updated>2017-07-12T19:00:48Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Keep Event Handlers Short */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Use Event Handlers ==&lt;br /&gt;
The Phidget library supports both polling channel properties to determine state and to get current data values, and the library supports firing events when states change and data is received.  Reading channel properties directly is useful to determine the state or current data value at a point in time, but is difficult to implement correctly.  Event handlers are more predicable and easier to implement correctly.  Unless a program has a specific and obvious reason to access properties directly, event handlers should be used.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
Events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in another thread should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using. There are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be the same as the same actual device channel would be attached; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;attach&#039;&#039;&#039; event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches those properties will revert to defaults.  Setting properties within the &#039;&#039;&#039;attach&#039;&#039;&#039; handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when your program exits, the channel will be released; however, it is good practice to close a channel when you are finished with it as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment merely interprets your program, so if you do not call {{Code|Close()}}, the LabVIEW environment will continue to tie up the channel despite having stopped the program you&#039;re working on.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28267</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28267"/>
		<updated>2017-07-12T18:53:26Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These &amp;quot;Best Practices&amp;quot; are designed to eliminate common mistakes made when using the {{Phidget22API}}, and to help maintain an stable structure while build more complicated systems.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
User events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in the main of the program (or another thread) should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using. There are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be the same as the same actual device channel would be attached; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;attach&#039;&#039;&#039; event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches those properties will revert to defaults.  Setting properties within the &#039;&#039;&#039;attach&#039;&#039;&#039; handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when your program exits, the channel will be released; however, it is good practice to close a channel when you are finished with it as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment merely interprets your program, so if you do not call {{Code|Close()}}, the LabVIEW environment will continue to tie up the channel despite having stopped the program you&#039;re working on.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28266</id>
		<title>Phidget Programming Basics</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28266"/>
		<updated>2017-07-12T18:48:06Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Close a Channel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
It is important to understand what a Phidget is, and how the hardware and software relate to one another before writing software using the {{Phidget22API}}.  Please refer to [[What is a Phidget?]] for an introduction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software library matches a Phidget device channel with a software channel that is created by a user.&lt;br /&gt;
Software channels are each of a class, where that class defines a set of methods, properties, events and errors.  Phidget devices are aware of the channel classes, and expose access to features of the device through the class interface.&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, you&#039;ll want to:&lt;br /&gt;
# &#039;&#039;&#039;[[#Creating a Channel|Create]]&#039;&#039;&#039; an instance of the channel class that provides access to the device you want to control.&lt;br /&gt;
# &#039;&#039;&#039;[[#Opening a Channel|Open]]&#039;&#039;&#039; the channel.&lt;br /&gt;
# Detect when the channel is &#039;&#039;&#039;[[#Attaching a Channel|attached]]&#039;&#039;&#039; to the Phidget.&lt;br /&gt;
# Use &#039;&#039;&#039;[[#Do Things with a Channel|methods and properties]]&#039;&#039;&#039; of the attached channel.&lt;br /&gt;
# &#039;&#039;&#039;[[#Close a Channel|Close]]&#039;&#039;&#039; the channel.&lt;br /&gt;
&lt;br /&gt;
Small code snippets are provided for each step in the sections below.  [[Language - Java|Java]] and [[Language - C/C++|C]] were selected because Java is a high-level language and C is a low level language.  The best reference to the &#039;&#039;features&#039;&#039; each channel class is the {{Phidget22API}} for your specific language.  This page is intended to be a high-level introduction.&lt;br /&gt;
&lt;br /&gt;
== Creating a Channel ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exposes one or more device channels where each channel is an interface to a feature of the hardware.  A channel class defines an interface to control and query a channel of the Phidget device.  For example, a 1018 - Phidget InterfaceKit device includes support&lt;br /&gt;
for digital input and digital out, and therefore exports &amp;lt;code&amp;gt;DigitalInput&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DigitalOutput&amp;lt;/code&amp;gt; channels.&lt;br /&gt;
&lt;br /&gt;
Phidget devices are controlled by creating an instance of a Phidget channel class.  Each channel class has a function that allows you to &#039;&#039;&#039;create&#039;&#039;&#039; an instance, and a function to later &#039;&#039;&#039;delete&#039;&#039;&#039; the instance.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 Accelerometer accel = new Accelerometer();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 RFID rfid = new RFID();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 PhidgetAccelerometerHandle accel;&lt;br /&gt;
 PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 PhidgetRFIDHandle rfid;&lt;br /&gt;
 PhidgetRFID_create(&amp;amp;rfid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A channel class will have properties and methods specific to the feature they belong to. For example, the &amp;lt;code&amp;gt;VoltageInput&amp;lt;/code&amp;gt; class has a &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property that lets you access the current voltage measured by the device, the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; class has properties to set the sensitivity on each axis and the &amp;lt;code&amp;gt;RFID&amp;lt;/code&amp;gt; has a method that writes to a writable RFID tag.&lt;br /&gt;
&lt;br /&gt;
== Opening a Channel ==&lt;br /&gt;
&lt;br /&gt;
After you have created a [[#Creating a Channel| channel instance]], you can call the &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; method to begin the process of matching the channel to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
For example, with the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channel in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 accel.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_open((PhidgetHandle) accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To see how to use &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; in other languages, please refer to the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function begins the process of matching the channel handle you created to a channel of a Phidget device, and does not actually &#039;&#039;open&#039;&#039; the Phidget itself.  An open channel may match a new Phidget device when it is plugged in if that channel has not already found a Phidget device channel that meets its criteria.&lt;br /&gt;
&lt;br /&gt;
===Details for Open()===&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; simply enables the process of matching a channel to a Phidget device channel, &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; returns immediately and does not wait for the channel to attach to a Phidget device channel. The channel changes state to &#039;&#039;open&#039;&#039;, but is not necessarily &#039;&#039;attached&#039;&#039;.  Until the channel actually becomes &#039;&#039;attached&#039;&#039; to a Phidget device channel, most of the channel class interface will not be available.&lt;br /&gt;
&lt;br /&gt;
As long as the channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel that has not already been matched with a channel.  The channel can only be matched once, and the Phidget device channel can only be matched once.  For example, if you connected a single Phidget Accelerometer to a computer, and you created and opened two &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channels, only one of the channels would attach while the other would remain &#039;&#039;open&#039;&#039; but not &#039;&#039;attached&#039;&#039;. If the &#039;&#039;attached&#039;&#039; channel were to be closed, the other channel would then attach to the Accelerometer.&lt;br /&gt;
&lt;br /&gt;
When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Once a Phidget device channel is matched to a channel, it will not be available to match another channel until the first channel is closed.  The one exception is if the Phidget device channel is &#039;&#039;attached&#039;&#039; over the network through the [[Phidget Network Server]].  In that case, multiple channels are allowed to match and attach to the Phidget device channel. Some Phidgets, such as motor controllers will never match more than one channel at a time.&lt;br /&gt;
&lt;br /&gt;
===Channel Matching===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; can be used without specifying any matching properties.  In this case the system will match a channel with the first Phidget device channel that matches the channel class.  The channel class itself is an implied matching parameter.&lt;br /&gt;
&lt;br /&gt;
If there is more than one Phidget connected to the computer that supports the channel class, the &amp;lt;code&amp;gt;DeviceSerialNumber&amp;lt;/code&amp;gt; property must be set determine which Phidget will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget connected to the computer has more than one channel of the same classes, the &amp;lt;code&amp;gt;Channel&amp;lt;/code&amp;gt; property must be set to determine which Phidget device channel will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget Network Server is enabled on the computer, the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; properties can be used to determine if the channel will match the Phidget device channel or the network channel.&lt;br /&gt;
&lt;br /&gt;
Please refer to the {{Phidget22API}}, and [[Best Phidgets Practices|Best Practices]] for more information on matching channels to Phidget device channels.&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a USB Phidget ====&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-1.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a [{{SERVER}}/products.php?product_id=1018 1018 PhidgetInterfaceKit 8/8/8] is connected to a computer. A [{{SERVER}}/products.php?product_id=1108 1108 Magnetic Sensor] is connected to analog port 3 on the 1018. Here&#039;s how you would open the channel for the magnetic sensor in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageRatioInput ch = new VoltageRatioInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(3);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to open digital input 5 in the same example, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(5);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a VINT Hub Port as a Channel ====&lt;br /&gt;
&lt;br /&gt;
The ports on a [[What is VINT?|VINT]] Hub can be opened as Digital Inputs, Digital Outputs, Voltage Inputs, or Voltage Ratio Inputs. Suppose you had a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub] with a [{{SERVER}}/products.php?product_id=1133 1133 Sound Sensor] connected to its sixth port. &lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-2.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how you would open it in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageInput ch = new VoltageInput();&lt;br /&gt;
ch.setDeviceSerialNumber(370181);&lt;br /&gt;
ch.setIsHubPortDevice(true);&lt;br /&gt;
ch.setHubPort(6);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a VINT Device ====&lt;br /&gt;
&lt;br /&gt;
In this example, we have a [{{SERVER}}/products.php?product_id=TMP1101 TMP1101 4x Thermocouple Phidget] and a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub].&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-3.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
If we wanted to open both the thermocouple connected to port 0 and the integrated temperature sensor on the board, we first need to figure out which channels those are. Go to the [{{SERVER}}/products.php?product_id=TMP1101 product page for the TMP1101] and click on the API tab. From the table at the top of the tab, we can see that the integrated temperature sensor is TemperatureSensor channel 4. In Java, opening these channels would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
TemperatureSensor tc = new TemperatureSensor(); // Handle for the thermocouple&lt;br /&gt;
TemperatureSensor ic = new TemperatureSensor(); // Handle for the integrated temperature chip&lt;br /&gt;
&lt;br /&gt;
tc.setDeviceSerialNumber();&lt;br /&gt;
ic.setDeviceSerialNumber();&lt;br /&gt;
tc.setHubPort(2);&lt;br /&gt;
ic.setHubPort(2);&lt;br /&gt;
tc.setChannel(0);&lt;br /&gt;
ic.setChannel(4);&lt;br /&gt;
&lt;br /&gt;
tc.open();&lt;br /&gt;
ic.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a Remote Phidget ====&lt;br /&gt;
&lt;br /&gt;
Suppose you wanted to open a locally connected Phidget remotely over the Network Service, so that you could have multiple programs connecting to it at the same time. In this example, we have a [{{SERVER}}/products.php?product_id=1024 1024 PhidgetRFID] connected to a computer that has the Phidget Network Server enabled.&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-4.jpg|link=|600px]]&lt;br /&gt;
&lt;br /&gt;
You could open the RFID reader channel remotely with the following code in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
RFID ch = new RFID();&lt;br /&gt;
ch.setDeviceSerialNumber(388624);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Attaching a Channel ==&lt;br /&gt;
&lt;br /&gt;
When a channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel until either a match is found, or the channel is closed.  When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
An event handler can be registered to catch the &#039;&#039;attach event&#039;&#039; through the channel&#039;s &amp;lt;code&amp;gt;Attach&amp;lt;/code&amp;gt; event.  Each channel also has a &amp;lt;code&amp;gt;Attached&amp;lt;/code&amp;gt; property that can be read to determine if the class has&lt;br /&gt;
been matched to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
To set an event handler to detect the attach in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  //create a Phidget accelerometer channel:&lt;br /&gt;
  Accelerometer accel = new Accelerometer();&lt;br /&gt;
  accel.addAttachListener((AttachEvent ae) -&amp;gt; {&lt;br /&gt;
      Accelerometer accel = (Accelerometer) ae.getSource();&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
      }&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  // Define the attach handler function&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onAttachedEventHandler(PhidgetHandle channel, void *userPtr) {&lt;br /&gt;
      printf(&amp;quot;A channel has been attached\n&amp;quot;);&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int&lt;br /&gt;
  main(int argc, char **argv) {&lt;br /&gt;
    // .....Then, in the main code create the channel and set the attach handler:&lt;br /&gt;
    PhidgetAccelerometerHandle accel;&lt;br /&gt;
    PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
    Phidget_setOnAttachHandler((PhidgetHandle)accel, onAttachedEventHandler, NULL);&lt;br /&gt;
 &lt;br /&gt;
    // other stuff in main&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The attach event handler should be set &#039;&#039;&#039;before&#039;&#039;&#039; the channel is opened; otherwise, the attach event could occur before the handler is set.&lt;br /&gt;
&lt;br /&gt;
== Do Things with a Channel ==&lt;br /&gt;
&lt;br /&gt;
After a channel has been &#039;&#039;opened&#039;&#039; and &#039;&#039;attached&#039;&#039;, software can control the Phidget device via the channel class interface.&lt;br /&gt;
&lt;br /&gt;
Like setting an event handler that executes [[#Attaching a Channel|when the channels is attached]], handlers can be set to receive events when a state change occurs, or sensor data is received.&lt;br /&gt;
&lt;br /&gt;
A Voltage Input channel for example, like one supported by the  [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit], can set an event handler that gets called as the voltage is processed.&lt;br /&gt;
&lt;br /&gt;
In Java, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  sensorIn.addVoltageChangeListener((VoltageInputVoltageChangeEvent de) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Voltage: &amp;quot; + de.getVoltage());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onVoltageChangeHandler(PhidgetVoltageInputHandle voltageInput, void *ctx, double voltage) {				&lt;br /&gt;
	printf(&amp;quot;Voltage: %lf V\n&amp;quot;, voltage);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // .....Then, in the main code:&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  PhidgetVoltageInput_setOnVoltageChangeHandler(sensorIn, onVoltageChangeHandler, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The voltage value delivered in the above snippets could also be accessed via the &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property of the channel.  Properties that have related events are automatically updated each time the event is received by the channel, and calls between events always return the value set by the last event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    double val = sensorIn.getVoltage();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
    double val;&lt;br /&gt;
    PhidgetVoltageInput_getVoltage(sensorIn, &amp;amp;val);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sensors, Input, and Output===&lt;br /&gt;
&lt;br /&gt;
Often, your Phidget will support more than one channel class.  For example a [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit] has voltage inputs (black sensor ports), digital inputs and digital outputs (green terminal blocks). You can determine the channel classes supported by your Phidget from the product page and the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
For InterfaceKits like the 1018:&lt;br /&gt;
&lt;br /&gt;
* To the voltage inputs, you can attach various sensors (temperature, humidity, light, sound)   &lt;br /&gt;
* To the digital inputs, you can attach various input devices (switches)&lt;br /&gt;
* To the digital outputs, you can attach simple indicators (LEDs, buzzers, relays)&lt;br /&gt;
&lt;br /&gt;
The features and channels supported by each Phidget are many and varied, and we only include general concepts on this page. You can view the complete list of channels supported by a Phidget in the {{Phidget22API}}. Select a device at the top of the screen, select your preferred programming language, and then select which channel class you want to view the documentation for.&lt;br /&gt;
&lt;br /&gt;
== Close a Channel ==&lt;br /&gt;
&lt;br /&gt;
When you are finished with a channel, you should close and (in some languages) delete it.  Once closed, the Phidget device channel the channel was matched to will be released and made available for another channel to match.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  device.close();&lt;br /&gt;
  device = null;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_close((PhidgetHandle)channel);&lt;br /&gt;
 Phidget_release((PhidgetHandle *)&amp;amp;channel); // will NULL the pointer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - How to know you are attaching to the Phidget you want.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28265</id>
		<title>Phidget Programming Basics</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28265"/>
		<updated>2017-07-12T18:45:26Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Attaching a Channel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
It is important to understand what a Phidget is, and how the hardware and software relate to one another before writing software using the {{Phidget22API}}.  Please refer to [[What is a Phidget?]] for an introduction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software library matches a Phidget device channel with a software channel that is created by a user.&lt;br /&gt;
Software channels are each of a class, where that class defines a set of methods, properties, events and errors.  Phidget devices are aware of the channel classes, and expose access to features of the device through the class interface.&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, you&#039;ll want to:&lt;br /&gt;
# &#039;&#039;&#039;[[#Creating a Channel|Create]]&#039;&#039;&#039; an instance of the channel class that provides access to the device you want to control.&lt;br /&gt;
# &#039;&#039;&#039;[[#Opening a Channel|Open]]&#039;&#039;&#039; the channel.&lt;br /&gt;
# Detect when the channel is &#039;&#039;&#039;[[#Attaching a Channel|attached]]&#039;&#039;&#039; to the Phidget.&lt;br /&gt;
# Use &#039;&#039;&#039;[[#Do Things with a Channel|methods and properties]]&#039;&#039;&#039; of the attached channel.&lt;br /&gt;
# &#039;&#039;&#039;[[#Close a Channel|Close]]&#039;&#039;&#039; the channel.&lt;br /&gt;
&lt;br /&gt;
Small code snippets are provided for each step in the sections below.  [[Language - Java|Java]] and [[Language - C/C++|C]] were selected because Java is a high-level language and C is a low level language.  The best reference to the &#039;&#039;features&#039;&#039; each channel class is the {{Phidget22API}} for your specific language.  This page is intended to be a high-level introduction.&lt;br /&gt;
&lt;br /&gt;
== Creating a Channel ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exposes one or more device channels where each channel is an interface to a feature of the hardware.  A channel class defines an interface to control and query a channel of the Phidget device.  For example, a 1018 - Phidget InterfaceKit device includes support&lt;br /&gt;
for digital input and digital out, and therefore exports &amp;lt;code&amp;gt;DigitalInput&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DigitalOutput&amp;lt;/code&amp;gt; channels.&lt;br /&gt;
&lt;br /&gt;
Phidget devices are controlled by creating an instance of a Phidget channel class.  Each channel class has a function that allows you to &#039;&#039;&#039;create&#039;&#039;&#039; an instance, and a function to later &#039;&#039;&#039;delete&#039;&#039;&#039; the instance.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 Accelerometer accel = new Accelerometer();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 RFID rfid = new RFID();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 PhidgetAccelerometerHandle accel;&lt;br /&gt;
 PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 PhidgetRFIDHandle rfid;&lt;br /&gt;
 PhidgetRFID_create(&amp;amp;rfid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A channel class will have properties and methods specific to the feature they belong to. For example, the &amp;lt;code&amp;gt;VoltageInput&amp;lt;/code&amp;gt; class has a &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property that lets you access the current voltage measured by the device, the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; class has properties to set the sensitivity on each axis and the &amp;lt;code&amp;gt;RFID&amp;lt;/code&amp;gt; has a method that writes to a writable RFID tag.&lt;br /&gt;
&lt;br /&gt;
== Opening a Channel ==&lt;br /&gt;
&lt;br /&gt;
After you have created a [[#Creating a Channel| channel instance]], you can call the &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; method to begin the process of matching the channel to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
For example, with the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channel in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 accel.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_open((PhidgetHandle) accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To see how to use &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; in other languages, please refer to the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function begins the process of matching the channel handle you created to a channel of a Phidget device, and does not actually &#039;&#039;open&#039;&#039; the Phidget itself.  An open channel may match a new Phidget device when it is plugged in if that channel has not already found a Phidget device channel that meets its criteria.&lt;br /&gt;
&lt;br /&gt;
===Details for Open()===&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; simply enables the process of matching a channel to a Phidget device channel, &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; returns immediately and does not wait for the channel to attach to a Phidget device channel. The channel changes state to &#039;&#039;open&#039;&#039;, but is not necessarily &#039;&#039;attached&#039;&#039;.  Until the channel actually becomes &#039;&#039;attached&#039;&#039; to a Phidget device channel, most of the channel class interface will not be available.&lt;br /&gt;
&lt;br /&gt;
As long as the channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel that has not already been matched with a channel.  The channel can only be matched once, and the Phidget device channel can only be matched once.  For example, if you connected a single Phidget Accelerometer to a computer, and you created and opened two &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channels, only one of the channels would attach while the other would remain &#039;&#039;open&#039;&#039; but not &#039;&#039;attached&#039;&#039;. If the &#039;&#039;attached&#039;&#039; channel were to be closed, the other channel would then attach to the Accelerometer.&lt;br /&gt;
&lt;br /&gt;
When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Once a Phidget device channel is matched to a channel, it will not be available to match another channel until the first channel is closed.  The one exception is if the Phidget device channel is &#039;&#039;attached&#039;&#039; over the network through the [[Phidget Network Server]].  In that case, multiple channels are allowed to match and attach to the Phidget device channel. Some Phidgets, such as motor controllers will never match more than one channel at a time.&lt;br /&gt;
&lt;br /&gt;
===Channel Matching===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; can be used without specifying any matching properties.  In this case the system will match a channel with the first Phidget device channel that matches the channel class.  The channel class itself is an implied matching parameter.&lt;br /&gt;
&lt;br /&gt;
If there is more than one Phidget connected to the computer that supports the channel class, the &amp;lt;code&amp;gt;DeviceSerialNumber&amp;lt;/code&amp;gt; property must be set determine which Phidget will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget connected to the computer has more than one channel of the same classes, the &amp;lt;code&amp;gt;Channel&amp;lt;/code&amp;gt; property must be set to determine which Phidget device channel will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget Network Server is enabled on the computer, the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; properties can be used to determine if the channel will match the Phidget device channel or the network channel.&lt;br /&gt;
&lt;br /&gt;
Please refer to the {{Phidget22API}}, and [[Best Phidgets Practices|Best Practices]] for more information on matching channels to Phidget device channels.&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a USB Phidget ====&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-1.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a [{{SERVER}}/products.php?product_id=1018 1018 PhidgetInterfaceKit 8/8/8] is connected to a computer. A [{{SERVER}}/products.php?product_id=1108 1108 Magnetic Sensor] is connected to analog port 3 on the 1018. Here&#039;s how you would open the channel for the magnetic sensor in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageRatioInput ch = new VoltageRatioInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(3);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to open digital input 5 in the same example, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(5);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a VINT Hub Port as a Channel ====&lt;br /&gt;
&lt;br /&gt;
The ports on a [[What is VINT?|VINT]] Hub can be opened as Digital Inputs, Digital Outputs, Voltage Inputs, or Voltage Ratio Inputs. Suppose you had a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub] with a [{{SERVER}}/products.php?product_id=1133 1133 Sound Sensor] connected to its sixth port. &lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-2.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how you would open it in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageInput ch = new VoltageInput();&lt;br /&gt;
ch.setDeviceSerialNumber(370181);&lt;br /&gt;
ch.setIsHubPortDevice(true);&lt;br /&gt;
ch.setHubPort(6);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a VINT Device ====&lt;br /&gt;
&lt;br /&gt;
In this example, we have a [{{SERVER}}/products.php?product_id=TMP1101 TMP1101 4x Thermocouple Phidget] and a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub].&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-3.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
If we wanted to open both the thermocouple connected to port 0 and the integrated temperature sensor on the board, we first need to figure out which channels those are. Go to the [{{SERVER}}/products.php?product_id=TMP1101 product page for the TMP1101] and click on the API tab. From the table at the top of the tab, we can see that the integrated temperature sensor is TemperatureSensor channel 4. In Java, opening these channels would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
TemperatureSensor tc = new TemperatureSensor(); // Handle for the thermocouple&lt;br /&gt;
TemperatureSensor ic = new TemperatureSensor(); // Handle for the integrated temperature chip&lt;br /&gt;
&lt;br /&gt;
tc.setDeviceSerialNumber();&lt;br /&gt;
ic.setDeviceSerialNumber();&lt;br /&gt;
tc.setHubPort(2);&lt;br /&gt;
ic.setHubPort(2);&lt;br /&gt;
tc.setChannel(0);&lt;br /&gt;
ic.setChannel(4);&lt;br /&gt;
&lt;br /&gt;
tc.open();&lt;br /&gt;
ic.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a Remote Phidget ====&lt;br /&gt;
&lt;br /&gt;
Suppose you wanted to open a locally connected Phidget remotely over the Network Service, so that you could have multiple programs connecting to it at the same time. In this example, we have a [{{SERVER}}/products.php?product_id=1024 1024 PhidgetRFID] connected to a computer that has the Phidget Network Server enabled.&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-4.jpg|link=|600px]]&lt;br /&gt;
&lt;br /&gt;
You could open the RFID reader channel remotely with the following code in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
RFID ch = new RFID();&lt;br /&gt;
ch.setDeviceSerialNumber(388624);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Attaching a Channel ==&lt;br /&gt;
&lt;br /&gt;
When a channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel until either a match is found, or the channel is closed.  When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
An event handler can be registered to catch the &#039;&#039;attach event&#039;&#039; through the channel&#039;s &amp;lt;code&amp;gt;Attach&amp;lt;/code&amp;gt; event.  Each channel also has a &amp;lt;code&amp;gt;Attached&amp;lt;/code&amp;gt; property that can be read to determine if the class has&lt;br /&gt;
been matched to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
To set an event handler to detect the attach in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  //create a Phidget accelerometer channel:&lt;br /&gt;
  Accelerometer accel = new Accelerometer();&lt;br /&gt;
  accel.addAttachListener((AttachEvent ae) -&amp;gt; {&lt;br /&gt;
      Accelerometer accel = (Accelerometer) ae.getSource();&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
      }&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  // Define the attach handler function&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onAttachedEventHandler(PhidgetHandle channel, void *userPtr) {&lt;br /&gt;
      printf(&amp;quot;A channel has been attached\n&amp;quot;);&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int&lt;br /&gt;
  main(int argc, char **argv) {&lt;br /&gt;
    // .....Then, in the main code create the channel and set the attach handler:&lt;br /&gt;
    PhidgetAccelerometerHandle accel;&lt;br /&gt;
    PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
    Phidget_setOnAttachHandler((PhidgetHandle)accel, onAttachedEventHandler, NULL);&lt;br /&gt;
 &lt;br /&gt;
    // other stuff in main&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The attach event handler should be set &#039;&#039;&#039;before&#039;&#039;&#039; the channel is opened; otherwise, the attach event could occur before the handler is set.&lt;br /&gt;
&lt;br /&gt;
== Do Things with a Channel ==&lt;br /&gt;
&lt;br /&gt;
After a channel has been &#039;&#039;opened&#039;&#039; and &#039;&#039;attached&#039;&#039;, software can control the Phidget device via the channel class interface.&lt;br /&gt;
&lt;br /&gt;
Like setting an event handler that executes [[#Attaching a Channel|when the channels is attached]], handlers can be set to receive events when a state change occurs, or sensor data is received.&lt;br /&gt;
&lt;br /&gt;
A Voltage Input channel for example, like one supported by the  [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit], can set an event handler that gets called as the voltage is processed.&lt;br /&gt;
&lt;br /&gt;
In Java, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  sensorIn.addVoltageChangeListener((VoltageInputVoltageChangeEvent de) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Voltage: &amp;quot; + de.getVoltage());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onVoltageChangeHandler(PhidgetVoltageInputHandle voltageInput, void *ctx, double voltage) {				&lt;br /&gt;
	printf(&amp;quot;Voltage: %lf V\n&amp;quot;, voltage);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // .....Then, in the main code:&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  PhidgetVoltageInput_setOnVoltageChangeHandler(sensorIn, onVoltageChangeHandler, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The voltage value delivered in the above snippets could also be accessed via the &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property of the channel.  Properties that have related events are automatically updated each time the event is received by the channel, and calls between events always return the value set by the last event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    double val = sensorIn.getVoltage();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
    double val;&lt;br /&gt;
    PhidgetVoltageInput_getVoltage(sensorIn, &amp;amp;val);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sensors, Input, and Output===&lt;br /&gt;
&lt;br /&gt;
Often, your Phidget will support more than one channel class.  For example a [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit] has voltage inputs (black sensor ports), digital inputs and digital outputs (green terminal blocks). You can determine the channel classes supported by your Phidget from the product page and the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
For InterfaceKits like the 1018:&lt;br /&gt;
&lt;br /&gt;
* To the voltage inputs, you can attach various sensors (temperature, humidity, light, sound)   &lt;br /&gt;
* To the digital inputs, you can attach various input devices (switches)&lt;br /&gt;
* To the digital outputs, you can attach simple indicators (LEDs, buzzers, relays)&lt;br /&gt;
&lt;br /&gt;
The features and channels supported by each Phidget are many and varied, and we only include general concepts on this page. You can view the complete list of channels supported by a Phidget in the {{Phidget22API}}. Select a device at the top of the screen, select your preferred programming language, and then select which channel class you want to view the documentation for.&lt;br /&gt;
&lt;br /&gt;
== Close a Channel ==&lt;br /&gt;
&lt;br /&gt;
When you are finished with a channel, you should close and (in some languages) delete it.  Once closed, the Phidget device channel the channel was matched to will be released and made available for another channel to match.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  device.close();&lt;br /&gt;
  device = null;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_close((PhidgetHandle)channel);&lt;br /&gt;
 Phidget_release((PhidgetHandle)&amp;amp;channel); // will NULL the pointer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - How to know you are attaching to the Phidget you want.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28264</id>
		<title>Phidget Programming Basics</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28264"/>
		<updated>2017-07-12T18:42:44Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Attaching a Channel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
It is important to understand what a Phidget is, and how the hardware and software relate to one another before writing software using the {{Phidget22API}}.  Please refer to [[What is a Phidget?]] for an introduction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software library matches a Phidget device channel with a software channel that is created by a user.&lt;br /&gt;
Software channels are each of a class, where that class defines a set of methods, properties, events and errors.  Phidget devices are aware of the channel classes, and expose access to features of the device through the class interface.&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, you&#039;ll want to:&lt;br /&gt;
# &#039;&#039;&#039;[[#Creating a Channel|Create]]&#039;&#039;&#039; an instance of the channel class that provides access to the device you want to control.&lt;br /&gt;
# &#039;&#039;&#039;[[#Opening a Channel|Open]]&#039;&#039;&#039; the channel.&lt;br /&gt;
# Detect when the channel is &#039;&#039;&#039;[[#Attaching a Channel|attached]]&#039;&#039;&#039; to the Phidget.&lt;br /&gt;
# Use &#039;&#039;&#039;[[#Do Things with a Channel|methods and properties]]&#039;&#039;&#039; of the attached channel.&lt;br /&gt;
# &#039;&#039;&#039;[[#Close a Channel|Close]]&#039;&#039;&#039; the channel.&lt;br /&gt;
&lt;br /&gt;
Small code snippets are provided for each step in the sections below.  [[Language - Java|Java]] and [[Language - C/C++|C]] were selected because Java is a high-level language and C is a low level language.  The best reference to the &#039;&#039;features&#039;&#039; each channel class is the {{Phidget22API}} for your specific language.  This page is intended to be a high-level introduction.&lt;br /&gt;
&lt;br /&gt;
== Creating a Channel ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exposes one or more device channels where each channel is an interface to a feature of the hardware.  A channel class defines an interface to control and query a channel of the Phidget device.  For example, a 1018 - Phidget InterfaceKit device includes support&lt;br /&gt;
for digital input and digital out, and therefore exports &amp;lt;code&amp;gt;DigitalInput&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DigitalOutput&amp;lt;/code&amp;gt; channels.&lt;br /&gt;
&lt;br /&gt;
Phidget devices are controlled by creating an instance of a Phidget channel class.  Each channel class has a function that allows you to &#039;&#039;&#039;create&#039;&#039;&#039; an instance, and a function to later &#039;&#039;&#039;delete&#039;&#039;&#039; the instance.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 Accelerometer accel = new Accelerometer();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 RFID rfid = new RFID();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 PhidgetAccelerometerHandle accel;&lt;br /&gt;
 PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 PhidgetRFIDHandle rfid;&lt;br /&gt;
 PhidgetRFID_create(&amp;amp;rfid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A channel class will have properties and methods specific to the feature they belong to. For example, the &amp;lt;code&amp;gt;VoltageInput&amp;lt;/code&amp;gt; class has a &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property that lets you access the current voltage measured by the device, the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; class has properties to set the sensitivity on each axis and the &amp;lt;code&amp;gt;RFID&amp;lt;/code&amp;gt; has a method that writes to a writable RFID tag.&lt;br /&gt;
&lt;br /&gt;
== Opening a Channel ==&lt;br /&gt;
&lt;br /&gt;
After you have created a [[#Creating a Channel| channel instance]], you can call the &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; method to begin the process of matching the channel to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
For example, with the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channel in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 accel.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_open((PhidgetHandle) accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To see how to use &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; in other languages, please refer to the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function begins the process of matching the channel handle you created to a channel of a Phidget device, and does not actually &#039;&#039;open&#039;&#039; the Phidget itself.  An open channel may match a new Phidget device when it is plugged in if that channel has not already found a Phidget device channel that meets its criteria.&lt;br /&gt;
&lt;br /&gt;
===Details for Open()===&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; simply enables the process of matching a channel to a Phidget device channel, &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; returns immediately and does not wait for the channel to attach to a Phidget device channel. The channel changes state to &#039;&#039;open&#039;&#039;, but is not necessarily &#039;&#039;attached&#039;&#039;.  Until the channel actually becomes &#039;&#039;attached&#039;&#039; to a Phidget device channel, most of the channel class interface will not be available.&lt;br /&gt;
&lt;br /&gt;
As long as the channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel that has not already been matched with a channel.  The channel can only be matched once, and the Phidget device channel can only be matched once.  For example, if you connected a single Phidget Accelerometer to a computer, and you created and opened two &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channels, only one of the channels would attach while the other would remain &#039;&#039;open&#039;&#039; but not &#039;&#039;attached&#039;&#039;. If the &#039;&#039;attached&#039;&#039; channel were to be closed, the other channel would then attach to the Accelerometer.&lt;br /&gt;
&lt;br /&gt;
When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Once a Phidget device channel is matched to a channel, it will not be available to match another channel until the first channel is closed.  The one exception is if the Phidget device channel is &#039;&#039;attached&#039;&#039; over the network through the [[Phidget Network Server]].  In that case, multiple channels are allowed to match and attach to the Phidget device channel. Some Phidgets, such as motor controllers will never match more than one channel at a time.&lt;br /&gt;
&lt;br /&gt;
===Channel Matching===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; can be used without specifying any matching properties.  In this case the system will match a channel with the first Phidget device channel that matches the channel class.  The channel class itself is an implied matching parameter.&lt;br /&gt;
&lt;br /&gt;
If there is more than one Phidget connected to the computer that supports the channel class, the &amp;lt;code&amp;gt;DeviceSerialNumber&amp;lt;/code&amp;gt; property must be set determine which Phidget will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget connected to the computer has more than one channel of the same classes, the &amp;lt;code&amp;gt;Channel&amp;lt;/code&amp;gt; property must be set to determine which Phidget device channel will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget Network Server is enabled on the computer, the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; properties can be used to determine if the channel will match the Phidget device channel or the network channel.&lt;br /&gt;
&lt;br /&gt;
Please refer to the {{Phidget22API}}, and [[Best Phidgets Practices|Best Practices]] for more information on matching channels to Phidget device channels.&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a USB Phidget ====&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-1.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a [{{SERVER}}/products.php?product_id=1018 1018 PhidgetInterfaceKit 8/8/8] is connected to a computer. A [{{SERVER}}/products.php?product_id=1108 1108 Magnetic Sensor] is connected to analog port 3 on the 1018. Here&#039;s how you would open the channel for the magnetic sensor in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageRatioInput ch = new VoltageRatioInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(3);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to open digital input 5 in the same example, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(5);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a VINT Hub Port as a Channel ====&lt;br /&gt;
&lt;br /&gt;
The ports on a [[What is VINT?|VINT]] Hub can be opened as Digital Inputs, Digital Outputs, Voltage Inputs, or Voltage Ratio Inputs. Suppose you had a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub] with a [{{SERVER}}/products.php?product_id=1133 1133 Sound Sensor] connected to its sixth port. &lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-2.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how you would open it in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageInput ch = new VoltageInput();&lt;br /&gt;
ch.setDeviceSerialNumber(370181);&lt;br /&gt;
ch.setIsHubPortDevice(true);&lt;br /&gt;
ch.setHubPort(6);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a VINT Device ====&lt;br /&gt;
&lt;br /&gt;
In this example, we have a [{{SERVER}}/products.php?product_id=TMP1101 TMP1101 4x Thermocouple Phidget] and a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub].&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-3.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
If we wanted to open both the thermocouple connected to port 0 and the integrated temperature sensor on the board, we first need to figure out which channels those are. Go to the [{{SERVER}}/products.php?product_id=TMP1101 product page for the TMP1101] and click on the API tab. From the table at the top of the tab, we can see that the integrated temperature sensor is TemperatureSensor channel 4. In Java, opening these channels would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
TemperatureSensor tc = new TemperatureSensor(); // Handle for the thermocouple&lt;br /&gt;
TemperatureSensor ic = new TemperatureSensor(); // Handle for the integrated temperature chip&lt;br /&gt;
&lt;br /&gt;
tc.setDeviceSerialNumber();&lt;br /&gt;
ic.setDeviceSerialNumber();&lt;br /&gt;
tc.setHubPort(2);&lt;br /&gt;
ic.setHubPort(2);&lt;br /&gt;
tc.setChannel(0);&lt;br /&gt;
ic.setChannel(4);&lt;br /&gt;
&lt;br /&gt;
tc.open();&lt;br /&gt;
ic.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a Remote Phidget ====&lt;br /&gt;
&lt;br /&gt;
Suppose you wanted to open a locally connected Phidget remotely over the Network Service, so that you could have multiple programs connecting to it at the same time. In this example, we have a [{{SERVER}}/products.php?product_id=1024 1024 PhidgetRFID] connected to a computer that has the Phidget Network Server enabled.&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-4.jpg|link=|600px]]&lt;br /&gt;
&lt;br /&gt;
You could open the RFID reader channel remotely with the following code in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
RFID ch = new RFID();&lt;br /&gt;
ch.setDeviceSerialNumber(388624);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Attaching a Channel ==&lt;br /&gt;
&lt;br /&gt;
When a channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel until either a match is found, or the channel is closed.  When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
An event handler can be registered to catch the &#039;&#039;attach event&#039;&#039; through the channel&#039;s &amp;lt;code&amp;gt;Attach&amp;lt;/code&amp;gt; event.  Each channel also has a &amp;lt;code&amp;gt;Attached&amp;lt;/code&amp;gt; property that can be read to determine if the class has&lt;br /&gt;
been matched to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
To set an event handler to detect the attach in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  //create a Phidget accelerometer channel:&lt;br /&gt;
  Accelerometer accel = new Accelerometer();&lt;br /&gt;
  accel.addAttachListener((AttachEvent ae) -&amp;gt; {&lt;br /&gt;
      Accelerometer accel = (Accelerometer) ae.getSource();&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
      }&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  // Define the attach handler function&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onAttachedEventHandler(PhidgetHandle channel, void *userPtr) {&lt;br /&gt;
      printf(&amp;quot;A channel has been attached\n&amp;quot;);&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int&lt;br /&gt;
  main(int argc, char **argv) {&lt;br /&gt;
    // .....Then, in the main code create the channel and set the attach handler:&lt;br /&gt;
    PhidgetAccelerometerHandle accel;&lt;br /&gt;
    PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
    Phidget_setOnAttachHandler((PhidgetHandle)accel, onAttachedEventHandler, NULL);&lt;br /&gt;
 &lt;br /&gt;
    // other stuff in main&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should set the attach event handler &#039;&#039;&#039;before&#039;&#039;&#039; you &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; the channel; otherwise, you may miss attach event if it occurs between opening the channel and setting the handler.&lt;br /&gt;
&lt;br /&gt;
== Do Things with a Channel ==&lt;br /&gt;
&lt;br /&gt;
After a channel has been &#039;&#039;opened&#039;&#039; and &#039;&#039;attached&#039;&#039;, software can control the Phidget device via the channel class interface.&lt;br /&gt;
&lt;br /&gt;
Like setting an event handler that executes [[#Attaching a Channel|when the channels is attached]], handlers can be set to receive events when a state change occurs, or sensor data is received.&lt;br /&gt;
&lt;br /&gt;
A Voltage Input channel for example, like one supported by the  [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit], can set an event handler that gets called as the voltage is processed.&lt;br /&gt;
&lt;br /&gt;
In Java, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  sensorIn.addVoltageChangeListener((VoltageInputVoltageChangeEvent de) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Voltage: &amp;quot; + de.getVoltage());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onVoltageChangeHandler(PhidgetVoltageInputHandle voltageInput, void *ctx, double voltage) {				&lt;br /&gt;
	printf(&amp;quot;Voltage: %lf V\n&amp;quot;, voltage);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // .....Then, in the main code:&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  PhidgetVoltageInput_setOnVoltageChangeHandler(sensorIn, onVoltageChangeHandler, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The voltage value delivered in the above snippets could also be accessed via the &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property of the channel.  Properties that have related events are automatically updated each time the event is received by the channel, and calls between events always return the value set by the last event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    double val = sensorIn.getVoltage();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
    double val;&lt;br /&gt;
    PhidgetVoltageInput_getVoltage(sensorIn, &amp;amp;val);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sensors, Input, and Output===&lt;br /&gt;
&lt;br /&gt;
Often, your Phidget will support more than one channel class.  For example a [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit] has voltage inputs (black sensor ports), digital inputs and digital outputs (green terminal blocks). You can determine the channel classes supported by your Phidget from the product page and the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
For InterfaceKits like the 1018:&lt;br /&gt;
&lt;br /&gt;
* To the voltage inputs, you can attach various sensors (temperature, humidity, light, sound)   &lt;br /&gt;
* To the digital inputs, you can attach various input devices (switches)&lt;br /&gt;
* To the digital outputs, you can attach simple indicators (LEDs, buzzers, relays)&lt;br /&gt;
&lt;br /&gt;
The features and channels supported by each Phidget are many and varied, and we only include general concepts on this page. You can view the complete list of channels supported by a Phidget in the {{Phidget22API}}. Select a device at the top of the screen, select your preferred programming language, and then select which channel class you want to view the documentation for.&lt;br /&gt;
&lt;br /&gt;
== Close a Channel ==&lt;br /&gt;
&lt;br /&gt;
When you are finished with a channel, you should close and (in some languages) delete it.  Once closed, the Phidget device channel the channel was matched to will be released and made available for another channel to match.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  device.close();&lt;br /&gt;
  device = null;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_close((PhidgetHandle)channel);&lt;br /&gt;
 Phidget_release((PhidgetHandle)&amp;amp;channel); // will NULL the pointer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - How to know you are attaching to the Phidget you want.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Polling_vs._Events&amp;diff=28263</id>
		<title>Polling vs. Events</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Polling_vs._Events&amp;diff=28263"/>
		<updated>2017-07-12T16:38:11Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
&lt;br /&gt;
The Phidget library operates asynchronously.  The library fires attach and detach events when a device channel appears and disappears (plugged in or unplugged).  After a user channel is attached, data and status updates from the device channel begin being delivered.  These events cause the library to update properties of the user channel, and to call any event handlers set by the program.&lt;br /&gt;
&lt;br /&gt;
===Discussion===&lt;br /&gt;
&lt;br /&gt;
====Attach====&lt;br /&gt;
Once a channel is opened, the Phidget library begins attempting to match the channel to a device channel.  While the channel is not &#039;&#039;&#039;attached&#039;&#039;&#039;, the {{Code|Attached}} property of the channel will be &#039;&#039;&#039;false&#039;&#039;&#039;.  As soon as the Phidget library matches the channel with a device channel the {{Code|Attached}} property is updated to be &#039;&#039;&#039;true&#039;&#039;&#039;.  A program could poll the {{Code|Attached}} property to determine when the channel becomes &#039;&#039;&#039;attached&#039;&#039;&#039;, or set an attach handler to be notified asynchronously.&lt;br /&gt;
&lt;br /&gt;
====Detach====&lt;br /&gt;
While a channel is &#039;&#039;&#039;open&#039;&#039;&#039; and &#039;&#039;&#039;attached&#039;&#039;&#039;, the device channel could disappear for some reason (could be unplugged).  As a result, the Phidget library would &#039;&#039;&#039;detach&#039;&#039;&#039; the user channel, and update the {{Code|Attached}} property to be &#039;&#039;&#039;false&#039;&#039;&#039;.  A program could poll the {{Code|Attached}} property to detect when the channel becomes &#039;&#039;&#039;dettached&#039;&#039;&#039;, or set a detach handler to be notified asynchronously.&lt;br /&gt;
&lt;br /&gt;
====Data====&lt;br /&gt;
While a channel is &#039;&#039;&#039;open&#039;&#039;&#039; and &#039;&#039;&#039;attached&#039;&#039;&#039;, data and status updates received by the Phidget library are delivered to the channel.  A program could poll data and status properties (for example, {{Code|Acceleration}}, {{Code|Temperature}}, and {{Code|State}}), or set handlers that would be called when the data or state changes.&lt;br /&gt;
&lt;br /&gt;
===Recommendations===&lt;br /&gt;
Generally, setting event handlers is preferred and the most flexible.  The code required to poll for attach and detach states, and to detect data and state changes can be complicated and error prone.  Event handlers are simple and work in a very predicable manor.&lt;br /&gt;
&lt;br /&gt;
A simple program that only wants to read a small amount of data from a device could simply open a channel and wait for attachment, read a data or state property, and then close the channel.  In that case, the code to implement and set event handlers might not be worth the effort.  Error checking however becomes very important, as accessing some channel properties while the channel is &#039;&#039;&#039;detached&#039;&#039;&#039; will result in an error or exception that could kill the program.  There will always be a race between checking the {{Code|Attached}} property and accessing a data or state property.&lt;br /&gt;
&lt;br /&gt;
All but the most simple program should set handlers that will be called when a channel attaches or detaches, and when the channel receives data or changes state.  The channel will only receive change events while &#039;&#039;&#039;attached&#039;&#039;&#039;, and no external calls are required to access the changed data, which eliminates a chance for error.  In addition, change triggers and data interval settings will not get reset accidentally if they are always set from within an attach handler, eliminating another common mistake.&lt;br /&gt;
&lt;br /&gt;
When in doubt, use event handlers.&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Logging,_Exceptions,_and_Errors&amp;diff=28262</id>
		<title>Logging, Exceptions, and Errors</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Logging,_Exceptions,_and_Errors&amp;diff=28262"/>
		<updated>2017-07-11T21:01:02Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Error Codes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
You&#039;ve written your code, fixed the compiler errors, and yet, the program still isn&#039;t behaving as intended. The tools described on this page will help you further debug your program and figure out where in the code things are going wrong.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Logging==&lt;br /&gt;
&lt;br /&gt;
The {{Phidget22API}} supports a general logging API that supports log levels, named sources of logs, and the writing of logs to a file or to a Phidget Network Server.  The Phidget software library uses this logging system internally, so in addition to being able to log within your own software, information about what the Phidget software is doing is also available.  Refer to the &#039;&#039;Logging API&#039;&#039; in the {{Phidget22API}} for language specific information.&lt;br /&gt;
&lt;br /&gt;
To begin, logging must be enabled &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
PhidgetLog_enable(PHIDGET_LOG_DEBUG, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
Log.enable(LogLevel.DEBUG, null);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; is passed to &amp;lt;code&amp;gt;enable()&amp;lt;/code&amp;gt; in the above examples, the logging system will output to &amp;lt;code&amp;gt;STDERR&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
There are five different logging levels, ranging from &amp;quot;Give me Everything!&amp;quot; to &amp;quot;Tell me only about critical problems&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CRITICAL&#039;&#039;&#039;&lt;br /&gt;
:Critical error messages.&lt;br /&gt;
:Errors at this level are generally non-recoverable and indicate either hardware problems, library bugs, or other serious issues.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;ERROR&#039;&#039;&#039;&lt;br /&gt;
:Non-critical error messages.&lt;br /&gt;
:Errors at this level are generally automatically recoverable, but may help to track down issues.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;WARNING&#039;&#039;&#039;&lt;br /&gt;
:Warning messages.&lt;br /&gt;
:Warnings are used to log behavior that is not necessarily in error, but is nevertheless odd or unexpected.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;INFO&#039;&#039;&#039;&lt;br /&gt;
:Informational messages.&lt;br /&gt;
:Informational messages communicate useful operations and states changes within the software&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VERBOSE&#039;&#039;&#039;&lt;br /&gt;
:Verbose messages.&lt;br /&gt;
:Verbose messages are informational messages that are expected to happen so frequently that they tend to drown out other log messages.&lt;br /&gt;
&lt;br /&gt;
These are available in the &#039;&#039;&#039;Enumerations&#039;&#039;&#039; section of Logging API in the {{Phidget22API}} documentation.&lt;br /&gt;
&lt;br /&gt;
=== Logging in Your Program ===&lt;br /&gt;
&lt;br /&gt;
Logging is performed using the &amp;lt;code&amp;gt;log()&amp;lt;/code&amp;gt; method from the Logging API.&lt;br /&gt;
&lt;br /&gt;
For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
PhidgetLog_log(PHIDGET_LOG_INFO, &amp;quot;Something happened in loop iteration %d!&amp;quot;, i);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
Log.log(LogLevel.INFO, &amp;quot;Something happened in loop iteration &amp;quot; + i + &amp;quot;!&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The logging API supports the concept of a &#039;&#039;log source&#039;&#039;, were the &#039;&#039;log source&#039;&#039; is a textual identifier of where the log message originated.  The &#039;&#039;log source&#039;&#039; is output to log the log file along with the log message to help organize the data.&lt;br /&gt;
&lt;br /&gt;
The following examples define and create a log source, and then write a log message using that source.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
#define MYSOURCE &amp;quot;mysource&amp;quot;&lt;br /&gt;
PhidgetLog_addSource(MYSOURCE, PHIDGET_LOG_INFO);&lt;br /&gt;
PhidgetLog_log(__FILE__, __LINE__, __func__, MYSOURCE, PHIDGET_LOG_INFO, &amp;quot;Something happened&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public static final String MYSOURCE = &amp;quot;mysource&amp;quot;;&lt;br /&gt;
Log.addSource(MYSOURCE, LogLevel.INFO);&lt;br /&gt;
Log.log(LogLevel.INFO, MYSOURCE, &amp;quot;Something happened);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exceptions and Errors==&lt;br /&gt;
&lt;br /&gt;
There are two different types of errors that you can use to determine where a problem exists in your program.&lt;br /&gt;
&lt;br /&gt;
===Error Generated By Methods===&lt;br /&gt;
&lt;br /&gt;
When an error occurs during a method invocation, either a language specific exception is thrown, or an error is returned.  It is important to check the return value from each method call, and to catch and handle exceptions.&lt;br /&gt;
&lt;br /&gt;
For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  int&lt;br /&gt;
  main(int argc, char **argv) {&lt;br /&gt;
    PhidgetReturnCode res;&lt;br /&gt;
&lt;br /&gt;
    res = PhidgetLog_enable(PHIDGET_LOG_ERROR, NULL);&lt;br /&gt;
    if (res != EPHIDGET_OK) {&lt;br /&gt;
      fprintf(stderr, &amp;quot;failed to enable Phidget logging: %d\n&amp;quot;, res);&lt;br /&gt;
      exit(1);&lt;br /&gt;
    }&lt;br /&gt;
    exit(0);&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  try {&lt;br /&gt;
      Log.enable(LogLevel.INFO, null);&lt;br /&gt;
  } catch (PhidgetException ex) {&lt;br /&gt;
      System.out.println(&amp;quot;Failed to enable Phidget logging: &amp;quot; + ex.getMessage());&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The consequence of not handling errors correctly ranges from the program crashing if an unhandled exception occurs to improper behavior.  Program should check for errors on each method call, and handle any errors.&lt;br /&gt;
&lt;br /&gt;
====Error Codes====&lt;br /&gt;
&lt;br /&gt;
A list of error codes can be found in the {{Phidget22API}}.  Refer to language specific exceptions in languages that support them.&lt;br /&gt;
&lt;br /&gt;
===Error Events===&lt;br /&gt;
&lt;br /&gt;
Error events are asynchronously generated by Phidget devices, and the Phidget library isself, during runtime.  A Phidget device might experience an excessive value (temperature, voltage, acceleration etc.), and trigger an error event to inform the program that is attached.  This would not necessarily be due to any operation the program performed; instead, the operating environment of the Phidget device would likely be the cause.  Error events are also fired when network errors occur, or the library is unable to handle incoming change events quickly enough (event handlers could be too slow).&lt;br /&gt;
&lt;br /&gt;
Error events are handled by setting an error event handler.&lt;br /&gt;
&lt;br /&gt;
In C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  errorEventHandler(PhidgetHandle ch, void *ctx, Phidget_ErrorEventCode code,&lt;br /&gt;
    const char *errorDescription) {&lt;br /&gt;
        Phidget_log(PHIDGET_LOG_ERROR, &amp;quot;Error Event [%d] %s&amp;quot;, code, errorDescription);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // set the error event handler before opening the channel&lt;br /&gt;
  res = Phidget_setOnErrorHandler((PhidgetHandle) ch, errorEventHandler, NULL);&lt;br /&gt;
  if (res != EPHIDGET_OK) {&lt;br /&gt;
    Phidget_log(PHIDGET_LOG_ERROR, &amp;quot;Failed to set error handler for %P&amp;quot;, ch);&lt;br /&gt;
    // XXX handle the error&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  ch.addErrorListener((ErrorEvent ee) -&amp;gt; {&lt;br /&gt;
    System.err.println(&amp;quot;Error: &amp;quot; + ee.getDescription());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Error Codes====&lt;br /&gt;
&lt;br /&gt;
These codes are passed to an  error event handler. See the {{Phidget22API}} documentation for your device to see which error event codes are supported.  The error event codes are generic, and the error description passed to the error event handler may contain more detailed information about the actual cause of the error event.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BADVERSION&#039;&#039;&#039;&lt;br /&gt;
:Version Mismatch Error. Usually means client and server library versions are out of sync. Update to the latest version of the Phidget software.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BUSY&#039;&#039;&#039;&lt;br /&gt;
:The Phidget device channel has already been locally attached. Only one channel may attach locally to a device channel.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NETWORK&#039;&#039;&#039;&lt;br /&gt;
:An error occurred with the network communications. See the error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DISPATCH&#039;&#039;&#039;&lt;br /&gt;
:An error occurred when trying to dispatch an event. It&#039;s possible that the data rate is too fast for the computer (or network) and the event queue has filled up.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OK&#039;&#039;&#039;&lt;br /&gt;
: An error state has ended. You can use the &amp;lt;code&amp;gt;getDescription&amp;lt;/code&amp;gt; method of the error event to get more information.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERRUN&#039;&#039;&#039;&lt;br /&gt;
:Sampling overrun. Some samples were lost in firmware because the queue filled up. This error is exclusive to Phidget InterfaceKits. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PACKETLOST&#039;&#039;&#039;&lt;br /&gt;
:Packet(s) were lost. This error is often an indication that your event handlers are not executing fast enough. Try to remove slow processes like GUI updates or user input from your handlers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;WRAP&#039;&#039;&#039;&lt;br /&gt;
:A variable has wrapped. For example, the encoder position can wrap from 2,147,483,647 to -2,147,483,648 because of an integer overflow.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERTEMP&#039;&#039;&#039;&lt;br /&gt;
:Over-Temperature condition detected. See error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERCURRENT&#039;&#039;&#039;&lt;br /&gt;
:Over-Current condition detected. See error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OUTOFRANGE&#039;&#039;&#039;&lt;br /&gt;
:Out of range condition detected. Usually an input on the board is reporting a value that is outside of the allowed range.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BADPOWER&#039;&#039;&#039;&lt;br /&gt;
:Power supply problem detected. Either the power supply is being overloaded, or it is under-powered (possibly not plugged in).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;SATURATION&#039;&#039;&#039;&lt;br /&gt;
:A sensor&#039;s value has reached the maximum or minimum of its sensing range. For example, a 1000 lux light sensor will throw this error event when a value greater than 1000 lux is detected. The sensor will still be reporting 1000 lux, but this error tells you that you don&#039;t know for certain how much higher than 1000 the real reading is.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERVOLTAGE&#039;&#039;&#039;&lt;br /&gt;
:Over-Voltage condition detected. Occurs in power-providing Phidgets when the output voltage exceeds the set value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VOLTAGEERROR&#039;&#039;&#039;&lt;br /&gt;
:A voltage error occurs when a Phidget that provides a voltage output has the value drop below what what specified. This can happen if the device being powered draws too much current, which causes a voltage drop. Stay within output current specifications to avoid voltage errors.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;ENERGYDUMP&#039;&#039;&#039;&lt;br /&gt;
:An energy dump occurs when a power-providing Phidget needs to dissipate its extra energy. See the [[:Category:UserGuide|User Guide]] for your device for more information.&lt;br /&gt;
&lt;br /&gt;
== Other Problems ==&lt;br /&gt;
&lt;br /&gt;
If your Phidget is still not behaving as expected after handling errors and exceptions, have a look at our [[General Troubleshooting]] guide to track down the problem.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Logging,_Exceptions,_and_Errors&amp;diff=28261</id>
		<title>Logging, Exceptions, and Errors</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Logging,_Exceptions,_and_Errors&amp;diff=28261"/>
		<updated>2017-07-11T20:58:38Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Error Generated From The Device */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
You&#039;ve written your code, fixed the compiler errors, and yet, the program still isn&#039;t behaving as intended. The tools described on this page will help you further debug your program and figure out where in the code things are going wrong.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Logging==&lt;br /&gt;
&lt;br /&gt;
The {{Phidget22API}} supports a general logging API that supports log levels, named sources of logs, and the writing of logs to a file or to a Phidget Network Server.  The Phidget software library uses this logging system internally, so in addition to being able to log within your own software, information about what the Phidget software is doing is also available.  Refer to the &#039;&#039;Logging API&#039;&#039; in the {{Phidget22API}} for language specific information.&lt;br /&gt;
&lt;br /&gt;
To begin, logging must be enabled &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
PhidgetLog_enable(PHIDGET_LOG_DEBUG, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
Log.enable(LogLevel.DEBUG, null);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; is passed to &amp;lt;code&amp;gt;enable()&amp;lt;/code&amp;gt; in the above examples, the logging system will output to &amp;lt;code&amp;gt;STDERR&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
There are five different logging levels, ranging from &amp;quot;Give me Everything!&amp;quot; to &amp;quot;Tell me only about critical problems&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CRITICAL&#039;&#039;&#039;&lt;br /&gt;
:Critical error messages.&lt;br /&gt;
:Errors at this level are generally non-recoverable and indicate either hardware problems, library bugs, or other serious issues.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;ERROR&#039;&#039;&#039;&lt;br /&gt;
:Non-critical error messages.&lt;br /&gt;
:Errors at this level are generally automatically recoverable, but may help to track down issues.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;WARNING&#039;&#039;&#039;&lt;br /&gt;
:Warning messages.&lt;br /&gt;
:Warnings are used to log behavior that is not necessarily in error, but is nevertheless odd or unexpected.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;INFO&#039;&#039;&#039;&lt;br /&gt;
:Informational messages.&lt;br /&gt;
:Informational messages communicate useful operations and states changes within the software&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VERBOSE&#039;&#039;&#039;&lt;br /&gt;
:Verbose messages.&lt;br /&gt;
:Verbose messages are informational messages that are expected to happen so frequently that they tend to drown out other log messages.&lt;br /&gt;
&lt;br /&gt;
These are available in the &#039;&#039;&#039;Enumerations&#039;&#039;&#039; section of Logging API in the {{Phidget22API}} documentation.&lt;br /&gt;
&lt;br /&gt;
=== Logging in Your Program ===&lt;br /&gt;
&lt;br /&gt;
Logging is performed using the &amp;lt;code&amp;gt;log()&amp;lt;/code&amp;gt; method from the Logging API.&lt;br /&gt;
&lt;br /&gt;
For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
PhidgetLog_log(PHIDGET_LOG_INFO, &amp;quot;Something happened in loop iteration %d!&amp;quot;, i);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
Log.log(LogLevel.INFO, &amp;quot;Something happened in loop iteration &amp;quot; + i + &amp;quot;!&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The logging API supports the concept of a &#039;&#039;log source&#039;&#039;, were the &#039;&#039;log source&#039;&#039; is a textual identifier of where the log message originated.  The &#039;&#039;log source&#039;&#039; is output to log the log file along with the log message to help organize the data.&lt;br /&gt;
&lt;br /&gt;
The following examples define and create a log source, and then write a log message using that source.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
#define MYSOURCE &amp;quot;mysource&amp;quot;&lt;br /&gt;
PhidgetLog_addSource(MYSOURCE, PHIDGET_LOG_INFO);&lt;br /&gt;
PhidgetLog_log(__FILE__, __LINE__, __func__, MYSOURCE, PHIDGET_LOG_INFO, &amp;quot;Something happened&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public static final String MYSOURCE = &amp;quot;mysource&amp;quot;;&lt;br /&gt;
Log.addSource(MYSOURCE, LogLevel.INFO);&lt;br /&gt;
Log.log(LogLevel.INFO, MYSOURCE, &amp;quot;Something happened);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exceptions and Errors==&lt;br /&gt;
&lt;br /&gt;
There are two different types of errors that you can use to determine where a problem exists in your program.&lt;br /&gt;
&lt;br /&gt;
===Error Generated By Methods===&lt;br /&gt;
&lt;br /&gt;
When an error occurs during a method invocation, either a language specific exception is thrown, or an error is returned.  It is important to check the return value from each method call, and to catch and handle exceptions.&lt;br /&gt;
&lt;br /&gt;
For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  int&lt;br /&gt;
  main(int argc, char **argv) {&lt;br /&gt;
    PhidgetReturnCode res;&lt;br /&gt;
&lt;br /&gt;
    res = PhidgetLog_enable(PHIDGET_LOG_ERROR, NULL);&lt;br /&gt;
    if (res != EPHIDGET_OK) {&lt;br /&gt;
      fprintf(stderr, &amp;quot;failed to enable Phidget logging: %d\n&amp;quot;, res);&lt;br /&gt;
      exit(1);&lt;br /&gt;
    }&lt;br /&gt;
    exit(0);&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  try {&lt;br /&gt;
      Log.enable(LogLevel.INFO, null);&lt;br /&gt;
  } catch (PhidgetException ex) {&lt;br /&gt;
      System.out.println(&amp;quot;Failed to enable Phidget logging: &amp;quot; + ex.getMessage());&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The consequence of not handling errors correctly ranges from the program crashing if an unhandled exception occurs to improper behavior.  Program should check for errors on each method call, and handle any errors.&lt;br /&gt;
&lt;br /&gt;
====Error Codes====&lt;br /&gt;
&lt;br /&gt;
A list of error codes can be found in the {{Phidget22API}}.  Refer to language specific exceptions in language that support them.&lt;br /&gt;
&lt;br /&gt;
===Error Events===&lt;br /&gt;
&lt;br /&gt;
Error events are asynchronously generated by Phidget devices, and the Phidget library isself, during runtime.  A Phidget device might experience an excessive value (temperature, voltage, acceleration etc.), and trigger an error event to inform the program that is attached.  This would not necessarily be due to any operation the program performed; instead, the operating environment of the Phidget device would likely be the cause.  Error events are also fired when network errors occur, or the library is unable to handle incoming change events quickly enough (event handlers could be too slow).&lt;br /&gt;
&lt;br /&gt;
Error events are handled by setting an error event handler.&lt;br /&gt;
&lt;br /&gt;
In C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  errorEventHandler(PhidgetHandle ch, void *ctx, Phidget_ErrorEventCode code,&lt;br /&gt;
    const char *errorDescription) {&lt;br /&gt;
        Phidget_log(PHIDGET_LOG_ERROR, &amp;quot;Error Event [%d] %s&amp;quot;, code, errorDescription);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // set the error event handler before opening the channel&lt;br /&gt;
  res = Phidget_setOnErrorHandler((PhidgetHandle) ch, errorEventHandler, NULL);&lt;br /&gt;
  if (res != EPHIDGET_OK) {&lt;br /&gt;
    Phidget_log(PHIDGET_LOG_ERROR, &amp;quot;Failed to set error handler for %P&amp;quot;, ch);&lt;br /&gt;
    // XXX handle the error&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  ch.addErrorListener((ErrorEvent ee) -&amp;gt; {&lt;br /&gt;
    System.err.println(&amp;quot;Error: &amp;quot; + ee.getDescription());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Error Codes====&lt;br /&gt;
&lt;br /&gt;
These codes are passed to an  error event handler. See the {{Phidget22API}} documentation for your device to see which error event codes are supported.  The error event codes are generic, and the error description passed to the error event handler may contain more detailed information about the actual cause of the error event.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BADVERSION&#039;&#039;&#039;&lt;br /&gt;
:Version Mismatch Error. Usually means client and server library versions are out of sync. Update to the latest version of the Phidget software.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BUSY&#039;&#039;&#039;&lt;br /&gt;
:The Phidget device channel has already been locally attached. Only one channel may attach locally to a device channel.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NETWORK&#039;&#039;&#039;&lt;br /&gt;
:An error occurred with the network communications. See the error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DISPATCH&#039;&#039;&#039;&lt;br /&gt;
:An error occurred when trying to dispatch an event. It&#039;s possible that the data rate is too fast for the computer (or network) and the event queue has filled up.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OK&#039;&#039;&#039;&lt;br /&gt;
: An error state has ended. You can use the &amp;lt;code&amp;gt;getDescription&amp;lt;/code&amp;gt; method of the error event to get more information.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERRUN&#039;&#039;&#039;&lt;br /&gt;
:Sampling overrun. Some samples were lost in firmware because the queue filled up. This error is exclusive to Phidget InterfaceKits. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PACKETLOST&#039;&#039;&#039;&lt;br /&gt;
:Packet(s) were lost. This error is often an indication that your event handlers are not executing fast enough. Try to remove slow processes like GUI updates or user input from your handlers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;WRAP&#039;&#039;&#039;&lt;br /&gt;
:A variable has wrapped. For example, the encoder position can wrap from 2,147,483,647 to -2,147,483,648 because of an integer overflow.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERTEMP&#039;&#039;&#039;&lt;br /&gt;
:Over-Temperature condition detected. See error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERCURRENT&#039;&#039;&#039;&lt;br /&gt;
:Over-Current condition detected. See error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OUTOFRANGE&#039;&#039;&#039;&lt;br /&gt;
:Out of range condition detected. Usually an input on the board is reporting a value that is outside of the allowed range.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BADPOWER&#039;&#039;&#039;&lt;br /&gt;
:Power supply problem detected. Either the power supply is being overloaded, or it is under-powered (possibly not plugged in).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;SATURATION&#039;&#039;&#039;&lt;br /&gt;
:A sensor&#039;s value has reached the maximum or minimum of its sensing range. For example, a 1000 lux light sensor will throw this error event when a value greater than 1000 lux is detected. The sensor will still be reporting 1000 lux, but this error tells you that you don&#039;t know for certain how much higher than 1000 the real reading is.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERVOLTAGE&#039;&#039;&#039;&lt;br /&gt;
:Over-Voltage condition detected. Occurs in power-providing Phidgets when the output voltage exceeds the set value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VOLTAGEERROR&#039;&#039;&#039;&lt;br /&gt;
:A voltage error occurs when a Phidget that provides a voltage output has the value drop below what what specified. This can happen if the device being powered draws too much current, which causes a voltage drop. Stay within output current specifications to avoid voltage errors.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;ENERGYDUMP&#039;&#039;&#039;&lt;br /&gt;
:An energy dump occurs when a power-providing Phidget needs to dissipate its extra energy. See the [[:Category:UserGuide|User Guide]] for your device for more information.&lt;br /&gt;
&lt;br /&gt;
== Other Problems ==&lt;br /&gt;
&lt;br /&gt;
If your Phidget is still not behaving as expected after handling errors and exceptions, have a look at our [[General Troubleshooting]] guide to track down the problem.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Logging,_Exceptions,_and_Errors&amp;diff=28260</id>
		<title>Logging, Exceptions, and Errors</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Logging,_Exceptions,_and_Errors&amp;diff=28260"/>
		<updated>2017-07-11T19:56:33Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
You&#039;ve written your code, fixed the compiler errors, and yet, the program still isn&#039;t behaving as intended. The tools described on this page will help you further debug your program and figure out where in the code things are going wrong.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Logging==&lt;br /&gt;
&lt;br /&gt;
The {{Phidget22API}} supports a general logging API that supports log levels, named sources of logs, and the writing of logs to a file or to a Phidget Network Server.  The Phidget software library uses this logging system internally, so in addition to being able to log within your own software, information about what the Phidget software is doing is also available.  Refer to the &#039;&#039;Logging API&#039;&#039; in the {{Phidget22API}} for language specific information.&lt;br /&gt;
&lt;br /&gt;
To begin, logging must be enabled &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
PhidgetLog_enable(PHIDGET_LOG_DEBUG, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
Log.enable(LogLevel.DEBUG, null);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When &amp;lt;code&amp;gt;NULL&amp;lt;/code&amp;gt; is passed to &amp;lt;code&amp;gt;enable()&amp;lt;/code&amp;gt; in the above examples, the logging system will output to &amp;lt;code&amp;gt;STDERR&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
There are five different logging levels, ranging from &amp;quot;Give me Everything!&amp;quot; to &amp;quot;Tell me only about critical problems&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CRITICAL&#039;&#039;&#039;&lt;br /&gt;
:Critical error messages.&lt;br /&gt;
:Errors at this level are generally non-recoverable and indicate either hardware problems, library bugs, or other serious issues.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;ERROR&#039;&#039;&#039;&lt;br /&gt;
:Non-critical error messages.&lt;br /&gt;
:Errors at this level are generally automatically recoverable, but may help to track down issues.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;WARNING&#039;&#039;&#039;&lt;br /&gt;
:Warning messages.&lt;br /&gt;
:Warnings are used to log behavior that is not necessarily in error, but is nevertheless odd or unexpected.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;INFO&#039;&#039;&#039;&lt;br /&gt;
:Informational messages.&lt;br /&gt;
:Informational messages communicate useful operations and states changes within the software&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VERBOSE&#039;&#039;&#039;&lt;br /&gt;
:Verbose messages.&lt;br /&gt;
:Verbose messages are informational messages that are expected to happen so frequently that they tend to drown out other log messages.&lt;br /&gt;
&lt;br /&gt;
These are available in the &#039;&#039;&#039;Enumerations&#039;&#039;&#039; section of Logging API in the {{Phidget22API}} documentation.&lt;br /&gt;
&lt;br /&gt;
=== Logging in Your Program ===&lt;br /&gt;
&lt;br /&gt;
Logging is performed using the &amp;lt;code&amp;gt;log()&amp;lt;/code&amp;gt; method from the Logging API.&lt;br /&gt;
&lt;br /&gt;
For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
PhidgetLog_log(PHIDGET_LOG_INFO, &amp;quot;Something happened in loop iteration %d!&amp;quot;, i);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
Log.log(LogLevel.INFO, &amp;quot;Something happened in loop iteration &amp;quot; + i + &amp;quot;!&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The logging API supports the concept of a &#039;&#039;log source&#039;&#039;, were the &#039;&#039;log source&#039;&#039; is a textual identifier of where the log message originated.  The &#039;&#039;log source&#039;&#039; is output to log the log file along with the log message to help organize the data.&lt;br /&gt;
&lt;br /&gt;
The following examples define and create a log source, and then write a log message using that source.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
#define MYSOURCE &amp;quot;mysource&amp;quot;&lt;br /&gt;
PhidgetLog_addSource(MYSOURCE, PHIDGET_LOG_INFO);&lt;br /&gt;
PhidgetLog_log(__FILE__, __LINE__, __func__, MYSOURCE, PHIDGET_LOG_INFO, &amp;quot;Something happened&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public static final String MYSOURCE = &amp;quot;mysource&amp;quot;;&lt;br /&gt;
Log.addSource(MYSOURCE, LogLevel.INFO);&lt;br /&gt;
Log.log(LogLevel.INFO, MYSOURCE, &amp;quot;Something happened);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exceptions and Errors==&lt;br /&gt;
&lt;br /&gt;
There are two different types of errors that you can use to determine where a problem exists in your program.&lt;br /&gt;
&lt;br /&gt;
===Error Generated By Methods===&lt;br /&gt;
&lt;br /&gt;
When an error occurs during a method invocation, either a language specific exception is thrown, or an error is returned.  It is important to check the return value from each method call, and to catch and handle exceptions.&lt;br /&gt;
&lt;br /&gt;
For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  int&lt;br /&gt;
  main(int argc, char **argv) {&lt;br /&gt;
    PhidgetReturnCode res;&lt;br /&gt;
&lt;br /&gt;
    res = PhidgetLog_enable(PHIDGET_LOG_ERROR, NULL);&lt;br /&gt;
    if (res != EPHIDGET_OK) {&lt;br /&gt;
      fprintf(stderr, &amp;quot;failed to enable Phidget logging: %d\n&amp;quot;, res);&lt;br /&gt;
      exit(1);&lt;br /&gt;
    }&lt;br /&gt;
    exit(0);&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  try {&lt;br /&gt;
      Log.enable(LogLevel.INFO, null);&lt;br /&gt;
  } catch (PhidgetException ex) {&lt;br /&gt;
      System.out.println(&amp;quot;Failed to enable Phidget logging: &amp;quot; + ex.getMessage());&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The consequence of not handling errors correctly ranges from the program crashing if an unhandled exception occurs to improper behavior.  Program should check for errors on each method call, and handle any errors.&lt;br /&gt;
&lt;br /&gt;
====Error Codes====&lt;br /&gt;
&lt;br /&gt;
A list of error codes can be found in the {{Phidget22API}}.  Refer to language specific exceptions in language that support them.&lt;br /&gt;
&lt;br /&gt;
===Error Generated From The Device===&lt;br /&gt;
&lt;br /&gt;
These errors are generated by the Phidget library during runtime.  For example, the Phidget device might be experiencing too high a temperature, and trigger an error.  This would not necessarily be due to any one function your program called; rather, the error would appear when the problem appears and would trigger an event. &lt;br /&gt;
&lt;br /&gt;
So, these errors happen &#039;&#039;asynchronously&#039;&#039;, that is, something happens apart from any function calls and an error event is triggered. You can handle these by setting up an Error Event Handler.&lt;br /&gt;
&lt;br /&gt;
In C, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
  int ErrorEventHandler (PhidgetHandle device, void *usrptr, Phidget_ErrorEventCode errorCode, const char *errorDescription) {&lt;br /&gt;
    printf(&amp;quot;The description for error %d is: %s\n&amp;quot;, errorCode, errorDescription);&lt;br /&gt;
    // Do something useful based on the error code&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // At the beginning of your program, hook the Error Event Handler in to receive events&lt;br /&gt;
  LocalErrorCatcher(&lt;br /&gt;
      Phidget_setOnErrorHandler((PhidgetHandle) device, ErrorEventHandler, NULL));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You&#039;ll note that the &#039;&#039;&#039;ErrorEventHandler&#039;&#039;&#039; function is what gets called when an error event occurs, and it gives you an opportunity to handle the error.  &lt;br /&gt;
&lt;br /&gt;
You&#039;ll also note that there is a second function called &#039;&#039;&#039;LocalErrorCatcher&#039;&#039;&#039;.  This second function handles the return value from setting the  error handler.  We included it here because, as these are examples on how to handle errors, the example would leave a possibly unhandled error without it. This second type of error handling  and the &#039;&#039;&#039;LocalErrorCatcher&#039;&#039;&#039; function are [[#Phidget Return Codes|described above]].&lt;br /&gt;
&lt;br /&gt;
In Java, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  try {&lt;br /&gt;
    device.addErrorListener((ErrorEvent ee) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Error: &amp;quot; + ee.getDescription());&lt;br /&gt;
        });&lt;br /&gt;
    });&lt;br /&gt;
  } catch (PhidgetException exception) {&lt;br /&gt;
      system.out.println(&amp;quot;The description for error &amp;quot; + Integer.toString(exception.getErrorCode()) + &amp;quot; is: &amp;quot;, exception.getDescription());&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like C above, the act of hooking an error listener in to the device can itself generate an error, which should be caught.  &lt;br /&gt;
&lt;br /&gt;
These event-type errors are also how a Phidget being run over a network announces bad passwords, lost packets, and other network problems.  Locally, the errors can announce incorrect temperatures, current, and power. So, it is in your best interest to set up an error event handler and take action based on the codes and error types in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
====Error Codes====&lt;br /&gt;
&lt;br /&gt;
These codes are used within the Error Event. See the {{Phidget22API}} documentation for your device to see which codes can be returned by which functions. These codes can be very generalized so it’s important to look at the accompanying description. These codes are broken down into errors that stem from within the library, errors which are directly reported by Phidget hardware, and errors that occur in your software.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BADVERSION&#039;&#039;&#039;&lt;br /&gt;
:Version Mismatch Error. Usually means client and host side of a network connection are out of sync. Update the latest versions of the Phidget drivers on both the client and the host.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BUSY&#039;&#039;&#039;&lt;br /&gt;
:The Phidget channel has already been opened locally by another process, so it can&#039;t be opened. You can have multiple processes open the same channel if they all open it remotely (with the exception of some Phidgets like motor controllers, which only allow a single connection regardless of whether it&#039;s local or remote).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NETWORK&#039;&#039;&#039;&lt;br /&gt;
:An error occurred with the network communications. See the error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DISPATCH&#039;&#039;&#039;&lt;br /&gt;
:An error occurred when trying to dispatch an event. It&#039;s possible that the data rate is too fast for the computer you&#039;re using and the event queue has filled up.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OK&#039;&#039;&#039;&lt;br /&gt;
: An error state has ended. You can use the &amp;lt;code&amp;gt;getDescription&amp;lt;/code&amp;gt; method of the error event to get more information. For example, if there is an over-voltage condition occurring, this error code will be thrown when the voltage returns to acceptable levels.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERRUN&#039;&#039;&#039;&lt;br /&gt;
:Sampling overrun. Some samples were lost in firmware because the queue filled up. This error is exclusive to Phidget InterfaceKits. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PACKETLOST&#039;&#039;&#039;&lt;br /&gt;
:Packet(s) were lost. This error is often an indication that your event handlers are not executing fast enough. Try to remove slow processes like GUI updates or user input from your handlers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;WRAP&#039;&#039;&#039;&lt;br /&gt;
:A variable has wrapped. For example, the encoder position can wrap from 2,147,483,647 to -2,147,483,648 because of an integer overflow.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERTEMP&#039;&#039;&#039;&lt;br /&gt;
:Over-Temperature condition detected. See error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERCURRENT&#039;&#039;&#039;&lt;br /&gt;
:Over-Current condition detected. See error description for more details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OUTOFRANGE&#039;&#039;&#039;&lt;br /&gt;
:Out of range condition detected. Usually an input on the board is reporting a value that is outside of the allowed range.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;BADPOWER&#039;&#039;&#039;&lt;br /&gt;
:Power supply problem detected. Either the power supply is being overloaded, or it is under-powered (possibly not plugged in).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;SATURATION&#039;&#039;&#039;&lt;br /&gt;
:A sensor&#039;s value has reached the maximum or minimum of its sensing range. For example, a 1000 lux light sensor will throw this error event when a value greater than 1000 lux is detected. The sensor will still be reporting 1000 lux, but this error tells you that you don&#039;t know for certain how much higher than 1000 the real reading is.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;OVERVOLTAGE&#039;&#039;&#039;&lt;br /&gt;
:Over-Voltage condition detected. Occurs in power-providing Phidgets when the output voltage exceeds the set value.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;VOLTAGEERROR&#039;&#039;&#039;&lt;br /&gt;
:A voltage error occurs when a Phidget that provides a voltage output has the value drop below what what specified. This can happen if the device being powered draws too much current, which causes a voltage drop. Stay within output current specifications to avoid voltage errors.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;ENERGYDUMP&#039;&#039;&#039;&lt;br /&gt;
:An energy dump occurs when a power-providing Phidget needs to dissipate its extra energy. See the [[:Category:UserGuide|User Guide]] for your device for more information.&lt;br /&gt;
&lt;br /&gt;
== Other Problems ==&lt;br /&gt;
&lt;br /&gt;
If your Phidget is still not behaving as expected after handling errors and exceptions, have a look at our [[General Troubleshooting]] guide to track down the problem.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28259</id>
		<title>Using Multiple Phidgets</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28259"/>
		<updated>2017-07-11T18:51:52Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
&lt;br /&gt;
When only one Phidget is connected to a computer, it is pretty easy for a simple program to open and attach to the channels on that one Phidget; however, when the program becomes more complicated or more than one Phidget is connected, attaching to the correct channel can be more difficult.&lt;br /&gt;
&lt;br /&gt;
Before opening a channel, it is important to set enough of the matching properties to ensure the desired device channel is matched.  By default, the matching code in the Phidget library will match the first available device channel that is of the correct class.  For example, if two temperature sensor devices are connected to a computer, it is undefined which will attach when the device serial number is not specified before the channel is opened.&lt;br /&gt;
&lt;br /&gt;
The best practice is to always specify at least the device serial number and the channel ID of the device channel that should be attached to the user channel.  Even when not strictly necessary, setting as many matching properties as possible can ensure that code will not break in the future.&lt;br /&gt;
&lt;br /&gt;
===Using the Channel ID===&lt;br /&gt;
&lt;br /&gt;
Each channel exported by a Phidget device has an id, normally starting at 0. The {{Code|channel}} property must be set to ensure the device channel the Phidget software library matches is right one.  If a 4-channel temperature sensor is connected, and the {{Code|channel}} property is not specified, the matching code will attach to channel 0 if available, and the next available channel if not.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;channel id&#039;&#039;&#039; with the {{Code|Channel}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Hub Port===&lt;br /&gt;
&lt;br /&gt;
VINT hubs have a number of ports that VINT devices can be connected to.  To ensure the correct VINT device is attached, the hub port must be specified.  If two temperature sensors are attached to the same hub, and the hub port is not specified prior to opening a channel, it is undefined which temperature sensor will be attached.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;hub port&#039;&#039;&#039; with the {{Code|HubPort}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Serial Number===&lt;br /&gt;
&lt;br /&gt;
Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub). When there is more than one device that exports the same channel class, the device serial number must be specified to ensure the channel on the desired device is matched.  The device serial number can be found on a label on the bottom of the Phidget, or determined by reading the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device serial number&#039;&#039;&#039; with the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Label===&lt;br /&gt;
&lt;br /&gt;
Most Phidgets can be assigned a label that may be used as a human-readable way to reference them (VINT devices inherit the serial number of the hub).  In software that will be distributed, or cannot easily be modified it is useful to attach to channels based on the label of the device.  This way, the device can be labelled prior to running the software, and the new device will be matched&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device label&#039;&#039;&#039; with the {{Code|DeviceLabel}} property.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some limitations are:&lt;br /&gt;
* In [[OS - Windows|Windows]], label can be read on any Phidget that has a serial number, but label can only be written for Phidgets that support firmware upgrading.&lt;br /&gt;
* Some programming languages do not support writing to labels. See the {{Phidget22API}}. &lt;br /&gt;
&lt;br /&gt;
===Code Examples===&lt;br /&gt;
&lt;br /&gt;
For example, in Java, this would be:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 ch.setDeviceSerialNumber(12345);  // match device 12345&lt;br /&gt;
 ch.setHubPort(4);                 // match hub port 4&lt;br /&gt;
 ch.setChannel(1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 ch.open();                        // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_setDeviceSerialNumber((PhidgetHandle)ch, 12345);  // match device 12345&lt;br /&gt;
 Phidget_setHubPort((PhidgetHandle)ch, 4);                 // match hub port 4&lt;br /&gt;
 Phidget_setChannel((PhidgetHandle)ch, 1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 Phidget_open((PhidgetHandle)ch);                         // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Distinguishing Events===&lt;br /&gt;
&lt;br /&gt;
When using [[Polling vs. Events|events]], it is recommended that a different event handler be used for each channel to simplify the code and prevent programming errors.  In the case where it is not practical to write an individual handler for each channel, the matching properties of the channel can be read to determine what device channel the event is from.&lt;br /&gt;
&lt;br /&gt;
Prior to the channel being attached to a device channel, the matching properties will return the values that will be used to perform the match; however, once the channel is attached, the matching properties will return the physical values from the device channel.  For example, if &#039;&#039;&#039;serial number&#039;&#039;&#039; has not been specified, {{Code|DeviceSerialNumber}} would be {{Code|PHIDGET_SERIALNUMBER_ANY}}, but once attached {{Code|DeviceSerialNumber}} would be the serial number of the attached device.&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28258</id>
		<title>Using Multiple Phidgets</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28258"/>
		<updated>2017-07-11T18:42:59Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Code Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
&lt;br /&gt;
Before opening a channel, it is important to set enough of the matching properties to ensure the desired device channel is matched.  By default, the matching code in the Phidget library will match the first available device channel that is of the correct class.  For example, if two temperature sensor devices are connected to a computer, it is undefined which will attach when the device serial number is not specified before the channel is opened.&lt;br /&gt;
&lt;br /&gt;
===Using the Channel ID===&lt;br /&gt;
&lt;br /&gt;
Each channel exported by a Phidget device has an id, normally starting at 0. The {{Code|channel}} property must be set to ensure the device channel the Phidget software library matches is right one.  If a 4-channel temperature sensor is connected, and the {{Code|channel}} property is not specified, the matching code will attach to channel 0 if available, and the next available channel if not.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;channel id&#039;&#039;&#039; with the {{Code|Channel}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Hub Port===&lt;br /&gt;
&lt;br /&gt;
VINT hubs have a number of ports that VINT devices can be connected to.  To ensure the correct VINT device is attached, the hub port must be specified.  If two temperature sensors are attached to the same hub, and the hub port is not specified prior to opening a channel, it is undefined which temperature sensor will be attached.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;hub port&#039;&#039;&#039; with the {{Code|HubPort}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Serial Number===&lt;br /&gt;
&lt;br /&gt;
Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub). When there is more than one device that exports the same channel class, the device serial number must be specified to ensure the channel on the desired device is matched.  The device serial number can be found on a label on the bottom of the Phidget, or determined by reading the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device serial number&#039;&#039;&#039; with the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Label===&lt;br /&gt;
&lt;br /&gt;
Most Phidgets can be assigned a label that may be used as a human-readable way to reference them (VINT devices inherit the serial number of the hub).  In software that will be distributed, or cannot easily be modified it is useful to attach to channels based on the label of the device.  This way, the device can be labelled prior to running the software, and the new device will be matched&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device label&#039;&#039;&#039; with the {{Code|DeviceLabel}} property.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some limitations are:&lt;br /&gt;
* In [[OS - Windows|Windows]], label can be read on any Phidget that has a serial number, but label can only be written for Phidgets that support firmware upgrading.&lt;br /&gt;
* Some programming languages do not support writing to labels. See the {{Phidget22API}}. &lt;br /&gt;
&lt;br /&gt;
===Code Examples===&lt;br /&gt;
&lt;br /&gt;
For example, in Java, this would be:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 ch.setDeviceSerialNumber(12345);  // match device 12345&lt;br /&gt;
 ch.setHubPort(4);                 // match hub port 4&lt;br /&gt;
 ch.setChannel(1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 ch.open();                        // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_setDeviceSerialNumber((PhidgetHandle)ch, 12345);  // match device 12345&lt;br /&gt;
 Phidget_setHubPort((PhidgetHandle)ch, 4);                 // match hub port 4&lt;br /&gt;
 Phidget_setChannel((PhidgetHandle)ch, 1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 Phidget_open((PhidgetHandle)ch);                         // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Distinguishing Events===&lt;br /&gt;
&lt;br /&gt;
When using [[Polling vs. Events|events]], it is recommended that a different event handler be used for each channel to simplify the code and prevent programming errors.  In the case where it is not practical to write an individual handler for each channel, the matching properties of the channel can be read to determine what device channel the event is from.&lt;br /&gt;
&lt;br /&gt;
Prior to the channel being attached to a device channel, the matching properties will return the values that will be used to perform the match; however, once the channel is attached, the matching properties will return the physical values from the device channel.  For example, if &#039;&#039;&#039;serial number&#039;&#039;&#039; has not been specified, {{Code|DeviceSerialNumber}} would be {{Code|PHIDGET_SERIALNUMBER_ANY}}, but once attached {{Code|DeviceSerialNumber}} would be the serial number of the attached device.&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28257</id>
		<title>Using Multiple Phidgets</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28257"/>
		<updated>2017-07-11T18:42:09Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Distinguishing Events */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
&lt;br /&gt;
Before opening a channel, it is important to set enough of the matching properties to ensure the desired device channel is matched.  By default, the matching code in the Phidget library will match the first available device channel that is of the correct class.  For example, if two temperature sensor devices are connected to a computer, it is undefined which will attach when the device serial number is not specified before the channel is opened.&lt;br /&gt;
&lt;br /&gt;
===Using the Channel ID===&lt;br /&gt;
&lt;br /&gt;
Each channel exported by a Phidget device has an id, normally starting at 0. The {{Code|channel}} property must be set to ensure the device channel the Phidget software library matches is right one.  If a 4-channel temperature sensor is connected, and the {{Code|channel}} property is not specified, the matching code will attach to channel 0 if available, and the next available channel if not.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;channel id&#039;&#039;&#039; with the {{Code|Channel}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Hub Port===&lt;br /&gt;
&lt;br /&gt;
VINT hubs have a number of ports that VINT devices can be connected to.  To ensure the correct VINT device is attached, the hub port must be specified.  If two temperature sensors are attached to the same hub, and the hub port is not specified prior to opening a channel, it is undefined which temperature sensor will be attached.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;hub port&#039;&#039;&#039; with the {{Code|HubPort}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Serial Number===&lt;br /&gt;
&lt;br /&gt;
Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub). When there is more than one device that exports the same channel class, the device serial number must be specified to ensure the channel on the desired device is matched.  The device serial number can be found on a label on the bottom of the Phidget, or determined by reading the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device serial number&#039;&#039;&#039; with the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Label===&lt;br /&gt;
&lt;br /&gt;
Most Phidgets can be assigned a label that may be used as a human-readable way to reference them (VINT devices inherit the serial number of the hub).  In software that will be distributed, or cannot easily be modified it is useful to attach to channels based on the label of the device.  This way, the device can be labelled prior to running the software, and the new device will be matched&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device label&#039;&#039;&#039; with the {{Code|DeviceLabel}} property.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some limitations are:&lt;br /&gt;
* In [[OS - Windows|Windows]], label can be read on any Phidget that has a serial number, but label can only be written for Phidgets that support firmware upgrading.&lt;br /&gt;
* Some programming languages do not support writing to labels. See the {{Phidget22API}}. &lt;br /&gt;
&lt;br /&gt;
===Code Examples===&lt;br /&gt;
&lt;br /&gt;
For example, in Java, this would be:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 ch.setDeviceSerialNumber(12345);  // match device 12345&lt;br /&gt;
 ch.setHubPort(4);                 // match hub port 4&lt;br /&gt;
 ch.setChannel(1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 ch.open();                        // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_setDeviceSerialNumber((PhidgetHandle)ch, 12345);  // match device 12345&lt;br /&gt;
 Phidget_setHubPort((PhidgetHandle)ch, 4);                 // match hub port 4&lt;br /&gt;
 Phidget_setChannel((PhidgetHandle)ch, 1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 Phidget_open((CPhidgetHandle)ch);                         // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Distinguishing Events===&lt;br /&gt;
&lt;br /&gt;
When using [[Polling vs. Events|events]], it is recommended that a different event handler be used for each channel to simplify the code and prevent programming errors.  In the case where it is not practical to write an individual handler for each channel, the matching properties of the channel can be read to determine what device channel the event is from.&lt;br /&gt;
&lt;br /&gt;
Prior to the channel being attached to a device channel, the matching properties will return the values that will be used to perform the match; however, once the channel is attached, the matching properties will return the physical values from the device channel.  For example, if &#039;&#039;&#039;serial number&#039;&#039;&#039; has not been specified, {{Code|DeviceSerialNumber}} would be {{Code|PHIDGET_SERIALNUMBER_ANY}}, but once attached {{Code|DeviceSerialNumber}} would be the serial number of the attached device.&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28256</id>
		<title>Using Multiple Phidgets</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Using_Multiple_Phidgets&amp;diff=28256"/>
		<updated>2017-07-11T18:32:27Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
&lt;br /&gt;
Before opening a channel, it is important to set enough of the matching properties to ensure the desired device channel is matched.  By default, the matching code in the Phidget library will match the first available device channel that is of the correct class.  For example, if two temperature sensor devices are connected to a computer, it is undefined which will attach when the device serial number is not specified before the channel is opened.&lt;br /&gt;
&lt;br /&gt;
===Using the Channel ID===&lt;br /&gt;
&lt;br /&gt;
Each channel exported by a Phidget device has an id, normally starting at 0. The {{Code|channel}} property must be set to ensure the device channel the Phidget software library matches is right one.  If a 4-channel temperature sensor is connected, and the {{Code|channel}} property is not specified, the matching code will attach to channel 0 if available, and the next available channel if not.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;channel id&#039;&#039;&#039; with the {{Code|Channel}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Hub Port===&lt;br /&gt;
&lt;br /&gt;
VINT hubs have a number of ports that VINT devices can be connected to.  To ensure the correct VINT device is attached, the hub port must be specified.  If two temperature sensors are attached to the same hub, and the hub port is not specified prior to opening a channel, it is undefined which temperature sensor will be attached.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;hub port&#039;&#039;&#039; with the {{Code|HubPort}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Serial Number===&lt;br /&gt;
&lt;br /&gt;
Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub). When there is more than one device that exports the same channel class, the device serial number must be specified to ensure the channel on the desired device is matched.  The device serial number can be found on a label on the bottom of the Phidget, or determined by reading the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device serial number&#039;&#039;&#039; with the {{Code|DeviceSerialNumber}} property.&lt;br /&gt;
&lt;br /&gt;
===Using the Label===&lt;br /&gt;
&lt;br /&gt;
Most Phidgets can be assigned a label that may be used as a human-readable way to reference them (VINT devices inherit the serial number of the hub).  In software that will be distributed, or cannot easily be modified it is useful to attach to channels based on the label of the device.  This way, the device can be labelled prior to running the software, and the new device will be matched&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Set the &#039;&#039;&#039;device label&#039;&#039;&#039; with the {{Code|DeviceLabel}} property.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some limitations are:&lt;br /&gt;
* In [[OS - Windows|Windows]], label can be read on any Phidget that has a serial number, but label can only be written for Phidgets that support firmware upgrading.&lt;br /&gt;
* Some programming languages do not support writing to labels. See the {{Phidget22API}}. &lt;br /&gt;
&lt;br /&gt;
===Code Examples===&lt;br /&gt;
&lt;br /&gt;
For example, in Java, this would be:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 ch.setDeviceSerialNumber(12345);  // match device 12345&lt;br /&gt;
 ch.setHubPort(4);                 // match hub port 4&lt;br /&gt;
 ch.setChannel(1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 ch.open();                        // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_setDeviceSerialNumber((PhidgetHandle)ch, 12345);  // match device 12345&lt;br /&gt;
 Phidget_setHubPort((PhidgetHandle)ch, 4);                 // match hub port 4&lt;br /&gt;
 Phidget_setChannel((PhidgetHandle)ch, 1);                 // match channel 1 port 4 dev 12345&lt;br /&gt;
 Phidget_open((CPhidgetHandle)ch);                         // start matching&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Distinguishing Events===&lt;br /&gt;
&lt;br /&gt;
If you are using [[Polling vs. Events|event-driven code]], once you have correctly opened multiple Phidgets of different types, they will have different event handlers and hence you will know what Phidget triggered which event.  &lt;br /&gt;
If you are using multiple Phidgets of the same type, or you are trying to determine within general events (such as Attach Events) which Phidget triggered the event, you can then check the serial number (or device type) of the triggering device and act accordingly.  &lt;br /&gt;
&lt;br /&gt;
For example, in Java, your [[Phidget_Programming_Basics#Attaching_the_Phidget|attach event handler]] might look like this:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    detachHandler = new DetachListener() { &lt;br /&gt;
        public void detached(DetachEvent event) {&lt;br /&gt;
            int serialNumber = ((Phidget)event.getSource()).getSerialNumber();&lt;br /&gt;
            // Do something according to serialNumber&lt;br /&gt;
    }    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
    int AttachHandler(CPhidgetHandle device, void *userptr) {&lt;br /&gt;
	int serialNo;&lt;br /&gt;
	CPhidget_getSerialNumber(device, &amp;amp;serialNo);&lt;br /&gt;
         // Do something according to serialNumber&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28254</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28254"/>
		<updated>2017-07-11T17:26:08Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Troubleshooting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Phidget Network Server:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The OS pages have examples of how to set up the Phidget Network Server and how to use it to remotely control and gather data from Phidgets. The OS pages also describe how to start and stop the Phidget Network Server, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to access to a Phidget Network Server.  First, if &#039;&#039;&#039;publish&#039;&#039;&#039; is enabled in a server, the server will broadcast its existence, and software can enable server discovery to dynamically discover and connect to the server.  Second, software can connect directly to a server.  To enable server discovery the {{Code|enableServerDiscovery()}} method is used.  To connect to a specific server the {{Code|addServer()}} method is used.  A list of networking methods can be found in the &amp;quot;Network API&amp;quot; section of the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, the SBC makes all of these channels available to any computer connected to the network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside of an assembly. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICE);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
Net.enableServerDiscovery(ServerType.DEVICE);&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICE);&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, please refer to the {{Phidget22API}}, and the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Phidget software library installed.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28253</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28253"/>
		<updated>2017-07-11T17:24:54Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Phidget Network Server:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The OS pages have examples of how to set up the Phidget Network Server and how to use it to remotely control and gather data from Phidgets. The OS pages also describe how to start and stop the Phidget Network Server, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to access to a Phidget Network Server.  First, if &#039;&#039;&#039;publish&#039;&#039;&#039; is enabled in a server, the server will broadcast its existence, and software can enable server discovery to dynamically discover and connect to the server.  Second, software can connect directly to a server.  To enable server discovery the {{Code|enableServerDiscovery()}} method is used.  To connect to a specific server the {{Code|addServer()}} method is used.  A list of networking methods can be found in the &amp;quot;Network API&amp;quot; section of the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, the SBC makes all of these channels available to any computer connected to the network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside of an assembly. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICE);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
Net.enableServerDiscovery(ServerType.DEVICE);&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICE);&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, please refer to the {{Phidget22API}}, and the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28252</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28252"/>
		<updated>2017-07-11T17:17:05Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Network Server on a Phidget Single Board Computer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Phidget Network Server:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The OS pages have examples of how to set up the Phidget Network Server and how to use it to remotely control and gather data from Phidgets. The OS pages also describe how to start and stop the Phidget Network Server, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to access to a Phidget Network Server.  First, if &#039;&#039;&#039;publish&#039;&#039;&#039; is enabled in a server, the server will broadcast its existence, and software can enable server discovery to dynamically discover and connect to the server.  Second, software can connect directly to a server.  To enable server discovery the {{Code|enableServerDiscovery()}} method is used.  To connect to a specific server the {{Code|addServer()}} method is used.  A list of networking methods can be found in the &amp;quot;Network API&amp;quot; section of the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, the SBC makes all of these channels available to any computer connected to the network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside of an assembly. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28251</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28251"/>
		<updated>2017-07-11T17:12:38Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Connecting to a Network Server */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Phidget Network Server:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The OS pages have examples of how to set up the Phidget Network Server and how to use it to remotely control and gather data from Phidgets. The OS pages also describe how to start and stop the Phidget Network Server, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to access to a Phidget Network Server.  First, if &#039;&#039;&#039;publish&#039;&#039;&#039; is enabled in a server, the server will broadcast its existence, and software can enable server discovery to dynamically discover and connect to the server.  Second, software can connect directly to a server.  To enable server discovery the {{Code|enableServerDiscovery()}} method is used.  To connect to a specific server the {{Code|addServer()}} method is used.  A list of networking methods can be found in the &amp;quot;Network API&amp;quot; section of the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, it makes all of these channels available to any device connected to the same wireless network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside some kind of assembly, rather than sitting on your computer desk. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC in a different location. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28250</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28250"/>
		<updated>2017-07-11T17:00:14Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using The Network Server */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Phidget Network Server:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The OS pages have examples of how to set up the Phidget Network Server and how to use it to remotely control and gather data from Phidgets. The OS pages also describe how to start and stop the Phidget Network Server, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to gain access to a Phidget server that&#039;s being hosted on your network. If the server is broadcasting itself on mDNS, you can simply enable automatic server discovery in your program. In C#, for example, you would call the {{Code|Net.EnableServerDiscovery(ServerType.Device)}} method in your program. &#039;&#039;&#039;Net&#039;&#039;&#039; is the object that is used for Phidget Networking. You can find a full list of methods and properties in the {{Phidget22API}} by selecting &amp;quot;Networking API&amp;quot; in the drop-down menu. &lt;br /&gt;
&lt;br /&gt;
If the Phidget server is not broadcasting itself on mDNS, you can connect to it by adding it specifically. In C#, for example, you would do this by calling the {{Code|Net.AddServer()}} method. AddServer takes a number of parameters that help specify the server to connect to (e.g. IP address, port, password). For more details on how to use AddServer, take a look at the &amp;quot;Networking API&amp;quot; in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, it makes all of these channels available to any device connected to the same wireless network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside some kind of assembly, rather than sitting on your computer desk. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC in a different location. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28249</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28249"/>
		<updated>2017-07-11T16:59:17Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Using The Network Server */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Phidget Network Server:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The OS pages have examples of how to set up the Phidget Network Server and how to using it to remotely control and gather data from Phidgets. The pages also tell you how to start and stop the Phidget Network Server on your computer, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to gain access to a Phidget server that&#039;s being hosted on your network. If the server is broadcasting itself on mDNS, you can simply enable automatic server discovery in your program. In C#, for example, you would call the {{Code|Net.EnableServerDiscovery(ServerType.Device)}} method in your program. &#039;&#039;&#039;Net&#039;&#039;&#039; is the object that is used for Phidget Networking. You can find a full list of methods and properties in the {{Phidget22API}} by selecting &amp;quot;Networking API&amp;quot; in the drop-down menu. &lt;br /&gt;
&lt;br /&gt;
If the Phidget server is not broadcasting itself on mDNS, you can connect to it by adding it specifically. In C#, for example, you would do this by calling the {{Code|Net.AddServer()}} method. AddServer takes a number of parameters that help specify the server to connect to (e.g. IP address, port, password). For more details on how to use AddServer, take a look at the &amp;quot;Networking API&amp;quot; in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, it makes all of these channels available to any device connected to the same wireless network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside some kind of assembly, rather than sitting on your computer desk. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC in a different location. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28248</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28248"/>
		<updated>2017-07-11T16:56:08Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Network Server on that operating system:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The operating systems pages have complete examples on how to set up a network server process and using it to remotely control or gather data from Phidgets. The pages also tell you how to start and stop the Network Server on your computer, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to gain access to a Phidget server that&#039;s being hosted on your network. If the server is broadcasting itself on mDNS, you can simply enable automatic server discovery in your program. In C#, for example, you would call the {{Code|Net.EnableServerDiscovery(ServerType.Device)}} method in your program. &#039;&#039;&#039;Net&#039;&#039;&#039; is the object that is used for Phidget Networking. You can find a full list of methods and properties in the {{Phidget22API}} by selecting &amp;quot;Networking API&amp;quot; in the drop-down menu. &lt;br /&gt;
&lt;br /&gt;
If the Phidget server is not broadcasting itself on mDNS, you can connect to it by adding it specifically. In C#, for example, you would do this by calling the {{Code|Net.AddServer()}} method. AddServer takes a number of parameters that help specify the server to connect to (e.g. IP address, port, password). For more details on how to use AddServer, take a look at the &amp;quot;Networking API&amp;quot; in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, it makes all of these channels available to any device connected to the same wireless network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside some kind of assembly, rather than sitting on your computer desk. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC in a different location. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28247</id>
		<title>Phidget Network Server</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Network_Server&amp;diff=28247"/>
		<updated>2017-07-11T16:53:24Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* General Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==General Overview==&lt;br /&gt;
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let&#039;s first take a look at what a system looks like without the Network Server enabled:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br clear=&amp;quot;all&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsLocal&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets physically connected to the local computer.&lt;br /&gt;
&lt;br /&gt;
Specifying &#039;&#039;&#039;IsRemote&#039;&#039;&#039; would instruct the Phidget software to only match Phidgets exported by a remote computer.&lt;br /&gt;
&lt;br /&gt;
Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.&lt;br /&gt;
&lt;br /&gt;
[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]&lt;br /&gt;
&lt;br /&gt;
As can be see in this example, there is a Phidget with four channels connected to a local computer. When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn&#039;t been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.&lt;br /&gt;
&lt;br /&gt;
==Using The Network Server==&lt;br /&gt;
&lt;br /&gt;
Each Operating System page has a section on how to use the Network Server on that operating system:&lt;br /&gt;
&lt;br /&gt;
* [[OS - Windows#Phidget Network Server|Windows]]&lt;br /&gt;
* [[OS - OS X#Phidget Network Server|Mac OS]]&lt;br /&gt;
* [[OS - Linux#Phidget Network Server|Linux]]&lt;br /&gt;
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]&lt;br /&gt;
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]&lt;br /&gt;
&lt;br /&gt;
The operating systems pages have complete examples on how to set up a network server process and using it to remotely control or gather data from Phidgets. The pages also tell you how to start and stop the Network Server on your computer, and how to run it with or without mDNS (Bonjour, avahi, etc).&lt;br /&gt;
&lt;br /&gt;
== Connecting to a Network Server ==&lt;br /&gt;
&lt;br /&gt;
There are two ways to gain access to a Phidget server that&#039;s being hosted on your network. If the server is broadcasting itself on mDNS, you can simply enable automatic server discovery in your program. In C#, for example, you would call the {{Code|Net.EnableServerDiscovery(ServerType.Device)}} method in your program. &#039;&#039;&#039;Net&#039;&#039;&#039; is the object that is used for Phidget Networking. You can find a full list of methods and properties in the {{Phidget22API}} by selecting &amp;quot;Networking API&amp;quot; in the drop-down menu. &lt;br /&gt;
&lt;br /&gt;
If the Phidget server is not broadcasting itself on mDNS, you can connect to it by adding it specifically. In C#, for example, you would do this by calling the {{Code|Net.AddServer()}} method. AddServer takes a number of parameters that help specify the server to connect to (e.g. IP address, port, password). For more details on how to use AddServer, take a look at the &amp;quot;Networking API&amp;quot; in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
==Network Server on a Phidget Single Board Computer==&lt;br /&gt;
&lt;br /&gt;
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server.  It runs the Network Server in the background automatically from the moment you turn it on, and allows you to read from and control all Phidgets attached to it:&lt;br /&gt;
&lt;br /&gt;
[[Image:network_server_sbc.jpg|link=|900px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, it makes all of these channels available to any device connected to the same wireless network. &lt;br /&gt;
&lt;br /&gt;
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside some kind of assembly, rather than sitting on your computer desk. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC in a different location. &lt;br /&gt;
&lt;br /&gt;
The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.&lt;br /&gt;
&lt;br /&gt;
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C/C++}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
PhidgetLightSensorHandle ch;&lt;br /&gt;
PhidgetLightSensor_create(&amp;amp;ch);&lt;br /&gt;
&lt;br /&gt;
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);&lt;br /&gt;
Phidget_setHubPort((PhidgetHandle) ch, 0);&lt;br /&gt;
Phidget_setIsRemote((PhidgetHandle) ch, 1);&lt;br /&gt;
&lt;br /&gt;
Phidget_open((PhidgetHandle) ch);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|C#}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;CSharp&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.DeviceSerialNumber = 37299;&lt;br /&gt;
ch.HubPort = 0;&lt;br /&gt;
ch.IsRemote = true;&lt;br /&gt;
&lt;br /&gt;
ch.Open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Java}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;Java&#039;&amp;gt;&lt;br /&gt;
LightSensor ch = new LightSensor();&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299);&lt;br /&gt;
ch.setHubPort(0);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{hiddenh3|Python}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;python&#039;&amp;gt;&lt;br /&gt;
ch = LightSensor()&lt;br /&gt;
&lt;br /&gt;
ch.setDeviceSerialNumber(37299)&lt;br /&gt;
ch.setHubPort(0)&lt;br /&gt;
ch.setIsRemote(1)&lt;br /&gt;
&lt;br /&gt;
ch.open();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
When using the Network Server, both the &#039;&#039;&#039;client and server should have the &#039;&#039;same version&#039;&#039;&#039;&#039;&#039; of the Network Server installed.  The easiest way to ensure this is to update your libraries on both ends.&lt;br /&gt;
&lt;br /&gt;
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].&lt;br /&gt;
&lt;br /&gt;
=== Further Reading ===&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=What_is_a_Phidget%3F&amp;diff=28246</id>
		<title>What is a Phidget?</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=What_is_a_Phidget%3F&amp;diff=28246"/>
		<updated>2017-07-11T16:29:03Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Network Server */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Phidgets are building-blocks for sensing and control using a computer, tablet, or phone.  Users create applications that use Phidgets to interact with the physical world. Phidgets connect via a USB port:&lt;br /&gt;
[[Image:wiap-image1.jpg|500px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Some Phidgets are a complete, self-contained sensing package. An example is then [{{SERVER}}/products.php?product_id=1042 1042]&lt;br /&gt;
, which measures motion:&lt;br /&gt;
[[Image:wiap-image2-spatial.jpg|500px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Other Phidgets are a &#039;building block&#039; to use other sensors. An example is the [{{SERVER}}/products.php?product_id=1048 1048] which allows the use of wire thermocouples:&lt;br /&gt;
[[Image:wiap-image2-temp.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Another Phidget may be a flexible I/O board which can receive analog sensor data and control digital inputs and outputs. An example is the [{{SERVER}}/products.php?product_id=1018 1018], with eight ports of each type:&lt;br /&gt;
[[Image:wiap-image2-ifkt.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, a Phidget may be a [{{SERVER}}/products.php?product_id=HUB0000 VINT Hub], made up of versatile ports that can be used as inputs or outputs, and also connect to smart [[What_is_VINT?|VINT]] devices.&lt;br /&gt;
[[Image:wiap-vint.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
==Data Flow==&lt;br /&gt;
&lt;br /&gt;
Data and control flows up and down the USB connection:&lt;br /&gt;
[[Image:wiap-dataflow.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
== Software Channels ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exports one or more channels.  Each device channel implements a channel class where that class defines the interface used to access the features of the device in a generic fashion.  For example, a Phidget device that supports sensing voltage exports a channel that implements the VoltageInput class.  The VoltageInput class defines the methods, properties, events and errors relevant to receiving voltage data.&lt;br /&gt;
&lt;br /&gt;
[[Image:voltageInput_object.jpg|300px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check the {{Phidget22API}} for details about channel classes. For example, the [{{SERVER}}/products.php?product_id=1018 1018] has five channels (with five distinct channel classes):&lt;br /&gt;
[[Image:1018_objects.jpg|600px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
Each Phidget channel class inherits common functionality from the base Phidget class.  The Phidget class defines methods like {{Code|open()}} and {{Code|close()}}, and properties like {{Code|DeviceSerialNumber}}. &lt;br /&gt;
&lt;br /&gt;
To help understand the concepts above, we have included a simple C# program that can be used with the [{{SERVER}}/products.php?product_id=1018 1018].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
using Phidget22;&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    VoltageInput vin;            // A user channel of the class VoltageInput&lt;br /&gt;
&lt;br /&gt;
    vin = new VoltageInput();    // Create an instance of the class    &lt;br /&gt;
    vin.Channel = 0;             // Specify the index of the device channel to attach to&lt;br /&gt;
    vin.Open(5000);              // Begin attempting to attach to the device channel&lt;br /&gt;
&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;My channel 0 sensor reads &amp;quot; + vin.Voltage); // Get the voltage value&lt;br /&gt;
&lt;br /&gt;
    vin.Close();                 // Close the channel, which detached it from the device channel&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Programming==&lt;br /&gt;
&lt;br /&gt;
For the most part, using Phidgets requires writing software. The {{Phidget22API}} is available in many different programming languages:&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;border:1px solid darkgray;&amp;quot; cellpadding=&amp;quot;5px;&amp;quot;&lt;br /&gt;
|-style=&amp;quot;background: #f0f0f0&amp;quot; align=center &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&#039;&#039;&#039;Core Languages&#039;&#039;&#039; || |&#039;&#039;&#039;Mobile Languages&#039;&#039;&#039; || |&#039;&#039;&#039;Other Languages&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-CSharp.png|alt=C Sharp|24x24px|link=Language - C Sharp]] [[Language - C Sharp|C#]] || [[Image:Icon-ObjC.png|24x24px|alt=Objective C|link=Language - Objective C]] [[Language - Objective C|Objective C]] || [[Image:Icon-LabVIEW.png|alt=LabVIEW|24x24px|link=Language - LabVIEW]] [[Language - LabVIEW|LabVIEW]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-C++.png|alt=C/C++|24x24px|link=Language - C/C++]] [[Language - C/C++|C/C++]] || |[[Image:Icon-Swift.png|alt=Swift|24x24px|link=Language - Swift]] [[Language - Swift|Swift]] || |[[Image:Icon-MaxMSP.png|24x24px|alt=Max/MSP|link=Language - Max/MSP]] [[Language - Max/MSP|Max/MSP]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Python.png|alt=Python|24x24px|link=Language - Python]] [[Language - Python|Python]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Java.png|alt=Java|24x24px|link=Language - Java]] [[Language - Java|Java]] &lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Visual Basic Net.png|alt=Visual Basic .NET|24x24px|link=Language - Visual Basic .NET]] [[Language - Visual Basic .NET|Visual Basic .NET]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Javascript.png|alt=JavaScript|24x24px|link=Language - JavaScript]] [[Language - JavaScript|JavaScript]] &lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software libraries are supported on a number of operating systems:&lt;br /&gt;
{| style=&amp;quot;border:1px solid darkgray;&amp;quot; cellpadding=&amp;quot;7px;&amp;quot;&lt;br /&gt;
|-style=&amp;quot;background: #f0f0f0&amp;quot; align=center &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&#039;&#039;&#039;Desktop OSes&#039;&#039;&#039; || |&#039;&#039;&#039;Mobile/Wireless OSes&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Windows.png|alt=OS - Windows|24x24px|link=OS - Windows]][[OS - Windows|Windows]] || |[[Image:Icon-Linux.png|alt=OS - Phidget SBC|24x24px|link=OS_-_Phidget_SBC]][[OS_-_Phidget_SBC|Phidget SBC]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Linux.png|alt=OS - Linux|24x24px|link=OS - Linux]][[OS - Linux|Linux]] || [[Image:Icon-iOS.png|alt=OS - iOS|link=OS - iOS|24x24px|link=OS - iOS]][[OS - iOS|iOS]]  &lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Mac-OS.png|alt=OS - OS X|24x24px|link=OS - OS X]][[OS - OS X|OS X]] &amp;lt;!--|| [[Image:Icon-Android.png|alt=OS - Android|link=OS - Android|24x24px|link=OS - Android]][[OS - Android|Android]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Network Server ==&lt;br /&gt;
&lt;br /&gt;
Phidget devices can be accessed remotely by using the [[Phidget Network Server]].  The Network Server exports the channels of each attached Phidget over the network:&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px|center]]&lt;br /&gt;
&lt;br /&gt;
Using the {{Phidget22API}}, clients on remote computers, phones, and [[OS_-_Phidget_SBC|Single Board Computers]] can attach to Phidget devices as if they were local.  This includes WWW based applications using the Phidget JavaScript library.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
With the combination of events, modular sensors, and network support, your system can range from simple to incredibly complex.&lt;br /&gt;
&lt;br /&gt;
We encourage customers to not only build projects for themselves, but also to design and build real-world products using Phidgets.  Our libraries can be distributed with your code to your customers.&lt;br /&gt;
&lt;br /&gt;
And this can all occur with the same devices, and the same flexible software API.&lt;br /&gt;
&lt;br /&gt;
Want to learn more?  Check out our:&lt;br /&gt;
* [{{SERVER}} Products on our main website]&lt;br /&gt;
* [[Software Overview | Languages and Operating Systems]]&lt;br /&gt;
* [{{SERVER}}/?view=articles Example Projects and Articles]&lt;br /&gt;
* [[:Category:Primer|In-depth hardware information]]&lt;br /&gt;
&lt;br /&gt;
Questions?  Please {{ContactUs|contact us}}.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=What_is_a_Phidget%3F&amp;diff=28245</id>
		<title>What is a Phidget?</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=What_is_a_Phidget%3F&amp;diff=28245"/>
		<updated>2017-07-11T16:22:56Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Overview]]&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Phidgets are building-blocks for sensing and control using a computer, tablet, or phone.  Users create applications that use Phidgets to interact with the physical world. Phidgets connect via a USB port:&lt;br /&gt;
[[Image:wiap-image1.jpg|500px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Some Phidgets are a complete, self-contained sensing package. An example is then [{{SERVER}}/products.php?product_id=1042 1042]&lt;br /&gt;
, which measures motion:&lt;br /&gt;
[[Image:wiap-image2-spatial.jpg|500px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Other Phidgets are a &#039;building block&#039; to use other sensors. An example is the [{{SERVER}}/products.php?product_id=1048 1048] which allows the use of wire thermocouples:&lt;br /&gt;
[[Image:wiap-image2-temp.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Another Phidget may be a flexible I/O board which can receive analog sensor data and control digital inputs and outputs. An example is the [{{SERVER}}/products.php?product_id=1018 1018], with eight ports of each type:&lt;br /&gt;
[[Image:wiap-image2-ifkt.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, a Phidget may be a [{{SERVER}}/products.php?product_id=HUB0000 VINT Hub], made up of versatile ports that can be used as inputs or outputs, and also connect to smart [[What_is_VINT?|VINT]] devices.&lt;br /&gt;
[[Image:wiap-vint.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
==Data Flow==&lt;br /&gt;
&lt;br /&gt;
Data and control flows up and down the USB connection:&lt;br /&gt;
[[Image:wiap-dataflow.jpg|700px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
== Software Channels ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exports one or more channels.  Each device channel implements a channel class where that class defines the interface used to access the features of the device in a generic fashion.  For example, a Phidget device that supports sensing voltage exports a channel that implements the VoltageInput class.  The VoltageInput class defines the methods, properties, events and errors relevant to receiving voltage data.&lt;br /&gt;
&lt;br /&gt;
[[Image:voltageInput_object.jpg|300px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can check the {{Phidget22API}} for details about channel classes. For example, the [{{SERVER}}/products.php?product_id=1018 1018] has five channels (with five distinct channel classes):&lt;br /&gt;
[[Image:1018_objects.jpg|600px|link=|alt=|center]]&lt;br /&gt;
&lt;br /&gt;
Each Phidget channel class inherits common functionality from the base Phidget class.  The Phidget class defines methods like {{Code|open()}} and {{Code|close()}}, and properties like {{Code|DeviceSerialNumber}}. &lt;br /&gt;
&lt;br /&gt;
To help understand the concepts above, we have included a simple C# program that can be used with the [{{SERVER}}/products.php?product_id=1018 1018].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
using Phidget22;&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    VoltageInput vin;            // A user channel of the class VoltageInput&lt;br /&gt;
&lt;br /&gt;
    vin = new VoltageInput();    // Create an instance of the class    &lt;br /&gt;
    vin.Channel = 0;             // Specify the index of the device channel to attach to&lt;br /&gt;
    vin.Open(5000);              // Begin attempting to attach to the device channel&lt;br /&gt;
&lt;br /&gt;
    System.Console.WriteLine(&amp;quot;My channel 0 sensor reads &amp;quot; + vin.Voltage); // Get the voltage value&lt;br /&gt;
&lt;br /&gt;
    vin.Close();                 // Close the channel, which detached it from the device channel&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Programming==&lt;br /&gt;
&lt;br /&gt;
For the most part, using Phidgets requires writing software. The {{Phidget22API}} is available in many different programming languages:&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;border:1px solid darkgray;&amp;quot; cellpadding=&amp;quot;5px;&amp;quot;&lt;br /&gt;
|-style=&amp;quot;background: #f0f0f0&amp;quot; align=center &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&#039;&#039;&#039;Core Languages&#039;&#039;&#039; || |&#039;&#039;&#039;Mobile Languages&#039;&#039;&#039; || |&#039;&#039;&#039;Other Languages&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-CSharp.png|alt=C Sharp|24x24px|link=Language - C Sharp]] [[Language - C Sharp|C#]] || [[Image:Icon-ObjC.png|24x24px|alt=Objective C|link=Language - Objective C]] [[Language - Objective C|Objective C]] || [[Image:Icon-LabVIEW.png|alt=LabVIEW|24x24px|link=Language - LabVIEW]] [[Language - LabVIEW|LabVIEW]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-C++.png|alt=C/C++|24x24px|link=Language - C/C++]] [[Language - C/C++|C/C++]] || |[[Image:Icon-Swift.png|alt=Swift|24x24px|link=Language - Swift]] [[Language - Swift|Swift]] || |[[Image:Icon-MaxMSP.png|24x24px|alt=Max/MSP|link=Language - Max/MSP]] [[Language - Max/MSP|Max/MSP]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Python.png|alt=Python|24x24px|link=Language - Python]] [[Language - Python|Python]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Java.png|alt=Java|24x24px|link=Language - Java]] [[Language - Java|Java]] &lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Visual Basic Net.png|alt=Visual Basic .NET|24x24px|link=Language - Visual Basic .NET]] [[Language - Visual Basic .NET|Visual Basic .NET]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Javascript.png|alt=JavaScript|24x24px|link=Language - JavaScript]] [[Language - JavaScript|JavaScript]] &lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software libraries are supported on a number of operating systems:&lt;br /&gt;
{| style=&amp;quot;border:1px solid darkgray;&amp;quot; cellpadding=&amp;quot;7px;&amp;quot;&lt;br /&gt;
|-style=&amp;quot;background: #f0f0f0&amp;quot; align=center &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|&#039;&#039;&#039;Desktop OSes&#039;&#039;&#039; || |&#039;&#039;&#039;Mobile/Wireless OSes&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Windows.png|alt=OS - Windows|24x24px|link=OS - Windows]][[OS - Windows|Windows]] || |[[Image:Icon-Linux.png|alt=OS - Phidget SBC|24x24px|link=OS_-_Phidget_SBC]][[OS_-_Phidget_SBC|Phidget SBC]]&lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Linux.png|alt=OS - Linux|24x24px|link=OS - Linux]][[OS - Linux|Linux]] || [[Image:Icon-iOS.png|alt=OS - iOS|link=OS - iOS|24x24px|link=OS - iOS]][[OS - iOS|iOS]]  &lt;br /&gt;
|-&lt;br /&gt;
|[[Image:Icon-Mac-OS.png|alt=OS - OS X|24x24px|link=OS - OS X]][[OS - OS X|OS X]] &amp;lt;!--|| [[Image:Icon-Android.png|alt=OS - Android|link=OS - Android|24x24px|link=OS - Android]][[OS - Android|Android]]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Network Server ==&lt;br /&gt;
Not only can you control a Phidget locally, but we also provide a tool called the [[Phidget Network Server]]. The Network Server exports your Phidget&#039;s channels over your local network:&lt;br /&gt;
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This allows other computers on your network to control the Phidget or read data from it. The network server isn&#039;t limited to desktop computers- it can also be used with phones or [[OS_-_Phidget_SBC|Single Board Computers]] that are running Phidgets code. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Network Server also includes the [[Phidget Dictionary]], which is a central place to store your data in key-value pairs.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
With the combination of events, modular sensors, and network support, your system can range from simple to incredibly complex.&lt;br /&gt;
&lt;br /&gt;
We encourage customers to not only build projects for themselves, but also to design and build real-world products using Phidgets.  Our libraries can be distributed with your code to your customers.&lt;br /&gt;
&lt;br /&gt;
And this can all occur with the same devices, and the same flexible software API.&lt;br /&gt;
&lt;br /&gt;
Want to learn more?  Check out our:&lt;br /&gt;
* [{{SERVER}} Products on our main website]&lt;br /&gt;
* [[Software Overview | Languages and Operating Systems]]&lt;br /&gt;
* [{{SERVER}}/?view=articles Example Projects and Articles]&lt;br /&gt;
* [[:Category:Primer|In-depth hardware information]]&lt;br /&gt;
&lt;br /&gt;
Questions?  Please {{ContactUs|contact us}}.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28244</id>
		<title>Phidget Programming Basics</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28244"/>
		<updated>2017-07-11T15:45:10Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Sensors, Input, and Output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
It is important to understand what a Phidget is, and how the hardware and software relate to one another before writing software using the {{Phidget22API}}.  Please refer to [[What is a Phidget?]] for an introduction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software library matches a Phidget device channel with a software channel that is created by a user.&lt;br /&gt;
Software channels are each of a class, where that class defines a set of methods, properties, events and errors.  Phidget devices are aware of the channel classes, and expose access to features of the device through the class interface.&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, you&#039;ll want to:&lt;br /&gt;
# &#039;&#039;&#039;[[#Creating a Channel|Create]]&#039;&#039;&#039; an instance of the channel class that provides access to the device you want to control.&lt;br /&gt;
# &#039;&#039;&#039;[[#Opening a Channel|Open]]&#039;&#039;&#039; the channel.&lt;br /&gt;
# Detect when the channel is &#039;&#039;&#039;[[#Attaching a Channel|attached]]&#039;&#039;&#039; to the Phidget.&lt;br /&gt;
# Use &#039;&#039;&#039;[[#Do Things with a Channel|methods and properties]]&#039;&#039;&#039; of the attached channel.&lt;br /&gt;
# &#039;&#039;&#039;[[#Close a Channel|Close]]&#039;&#039;&#039; the channel.&lt;br /&gt;
&lt;br /&gt;
Small code snippets are provided for each step in the sections below.  [[Language - Java|Java]] and [[Language - C/C++|C]] were selected because Java is a high-level language and C is a low level language.  The best reference to the &#039;&#039;features&#039;&#039; each channel class is the {{Phidget22API}} for your specific language.  This page is intended to be a high-level introduction.&lt;br /&gt;
&lt;br /&gt;
== Creating a Channel ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exposes one or more device channels where each channel is an interface to a feature of the hardware.  A channel class defines an interface to control and query a channel of the Phidget device.  For example, a 1018 - Phidget InterfaceKit device includes support&lt;br /&gt;
for digital input and digital out, and therefore exports &amp;lt;code&amp;gt;DigitalInput&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DigitalOutput&amp;lt;/code&amp;gt; channels.&lt;br /&gt;
&lt;br /&gt;
Phidget devices are controlled by creating an instance of a Phidget channel class.  Each channel class has a function that allows you to &#039;&#039;&#039;create&#039;&#039;&#039; an instance, and a function to later &#039;&#039;&#039;delete&#039;&#039;&#039; the instance.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 Accelerometer accel = new Accelerometer();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 RFID rfid = new RFID();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 PhidgetAccelerometerHandle accel;&lt;br /&gt;
 PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 PhidgetRFIDHandle rfid;&lt;br /&gt;
 PhidgetRFID_create(&amp;amp;rfid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A channel class will have properties and methods specific to the feature they belong to. For example, the &amp;lt;code&amp;gt;VoltageInput&amp;lt;/code&amp;gt; class has a &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property that lets you access the current voltage measured by the device, the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; class has properties to set the sensitivity on each axis and the &amp;lt;code&amp;gt;RFID&amp;lt;/code&amp;gt; has a method that writes to a writable RFID tag.&lt;br /&gt;
&lt;br /&gt;
== Opening a Channel ==&lt;br /&gt;
&lt;br /&gt;
After you have created a [[#Creating a Channel| channel instance]], you can call the &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; method to begin the process of matching the channel to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
For example, with the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channel in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 accel.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_open((PhidgetHandle) accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To see how to use &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; in other languages, please refer to the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function begins the process of matching the channel handle you created to a channel of a Phidget device, and does not actually &#039;&#039;open&#039;&#039; the Phidget itself.  An open channel may match a new Phidget device when it is plugged in if that channel has not already found a Phidget device channel that meets its criteria.&lt;br /&gt;
&lt;br /&gt;
===Details for Open()===&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; simply enables the process of matching a channel to a Phidget device channel, &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; returns immediately and does not wait for the channel to attach to a Phidget device channel. The channel changes state to &#039;&#039;open&#039;&#039;, but is not necessarily &#039;&#039;attached&#039;&#039;.  Until the channel actually becomes &#039;&#039;attached&#039;&#039; to a Phidget device channel, most of the channel class interface will not be available.&lt;br /&gt;
&lt;br /&gt;
As long as the channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel that has not already been matched with a channel.  The channel can only be matched once, and the Phidget device channel can only be matched once.  For example, if you connected a single Phidget Accelerometer to a computer, and you created and opened two &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channels, only one of the channels would attach while the other would remain &#039;&#039;open&#039;&#039; but not &#039;&#039;attached&#039;&#039;. If the &#039;&#039;attached&#039;&#039; channel were to be closed, the other channel would then attach to the Accelerometer.&lt;br /&gt;
&lt;br /&gt;
When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Once a Phidget device channel is matched to a channel, it will not be available to match another channel until the first channel is closed.  The one exception is if the Phidget device channel is &#039;&#039;attached&#039;&#039; over the network through the [[Phidget Network Server]].  In that case, multiple channels are allowed to match and attach to the Phidget device channel. Some Phidgets, such as motor controllers will never match more than one channel at a time.&lt;br /&gt;
&lt;br /&gt;
===Channel Matching===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; can be used without specifying any matching properties.  In this case the system will match a channel with the first Phidget device channel that matches the channel class.  The channel class itself is an implied matching parameter.&lt;br /&gt;
&lt;br /&gt;
If there is more than one Phidget connected to the computer that supports the channel class, the &amp;lt;code&amp;gt;DeviceSerialNumber&amp;lt;/code&amp;gt; property must be set determine which Phidget will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget connected to the computer has more than one channel of the same classes, the &amp;lt;code&amp;gt;Channel&amp;lt;/code&amp;gt; property must be set to determine which Phidget device channel will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget Network Server is enabled on the computer, the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; properties can be used to determine if the channel will match the Phidget device channel or the network channel.&lt;br /&gt;
&lt;br /&gt;
Please refer to the {{Phidget22API}}, and [[Best Phidgets Practices|Best Practices]] for more information on matching channels to Phidget device channels.&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a USB Phidget ====&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-1.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a [{{SERVER}}/products.php?product_id=1018 1018 PhidgetInterfaceKit 8/8/8] is connected to a computer. A [{{SERVER}}/products.php?product_id=1108 1108 Magnetic Sensor] is connected to analog port 3 on the 1018. Here&#039;s how you would open the channel for the magnetic sensor in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageRatioInput ch = new VoltageRatioInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(3);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to open digital input 5 in the same example, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(5);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a VINT Hub Port as a Channel ====&lt;br /&gt;
&lt;br /&gt;
The ports on a [[What is VINT?|VINT]] Hub can be opened as Digital Inputs, Digital Outputs, Voltage Inputs, or Voltage Ratio Inputs. Suppose you had a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub] with a [{{SERVER}}/products.php?product_id=1133 1133 Sound Sensor] connected to its sixth port. &lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-2.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how you would open it in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageInput ch = new VoltageInput();&lt;br /&gt;
ch.setDeviceSerialNumber(370181);&lt;br /&gt;
ch.setIsHubPortDevice(true);&lt;br /&gt;
ch.setHubPort(6);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a VINT Device ====&lt;br /&gt;
&lt;br /&gt;
In this example, we have a [{{SERVER}}/products.php?product_id=TMP1101 TMP1101 4x Thermocouple Phidget] and a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub].&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-3.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
If we wanted to open both the thermocouple connected to port 0 and the integrated temperature sensor on the board, we first need to figure out which channels those are. Go to the [{{SERVER}}/products.php?product_id=TMP1101 product page for the TMP1101] and click on the API tab. From the table at the top of the tab, we can see that the integrated temperature sensor is TemperatureSensor channel 4. In Java, opening these channels would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
TemperatureSensor tc = new TemperatureSensor(); // Handle for the thermocouple&lt;br /&gt;
TemperatureSensor ic = new TemperatureSensor(); // Handle for the integrated temperature chip&lt;br /&gt;
&lt;br /&gt;
tc.setDeviceSerialNumber();&lt;br /&gt;
ic.setDeviceSerialNumber();&lt;br /&gt;
tc.setHubPort(2);&lt;br /&gt;
ic.setHubPort(2);&lt;br /&gt;
tc.setChannel(0);&lt;br /&gt;
ic.setChannel(4);&lt;br /&gt;
&lt;br /&gt;
tc.open();&lt;br /&gt;
ic.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a Remote Phidget ====&lt;br /&gt;
&lt;br /&gt;
Suppose you wanted to open a locally connected Phidget remotely over the Network Service, so that you could have multiple programs connecting to it at the same time. In this example, we have a [{{SERVER}}/products.php?product_id=1024 1024 PhidgetRFID] connected to a computer that has the Phidget Network Server enabled.&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-4.jpg|link=|600px]]&lt;br /&gt;
&lt;br /&gt;
You could open the RFID reader channel remotely with the following code in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
RFID ch = new RFID();&lt;br /&gt;
ch.setDeviceSerialNumber(388624);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Attaching a Channel ==&lt;br /&gt;
&lt;br /&gt;
When a channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel until either a match is found, or the channel is closed.  When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
An event handler can be registered to catch the &#039;&#039;attach event&#039;&#039; through the channel&#039;s &amp;lt;code&amp;gt;Attach&amp;lt;/code&amp;gt; event.  Each channel also has a &amp;lt;code&amp;gt;Attached&amp;lt;/code&amp;gt; property that can be read to determine if the class has&lt;br /&gt;
been matched to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
To set an event handler to detect the attach in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  //create a Phidget accelerometer channel:&lt;br /&gt;
  Accelerometer accel = new Accelerometer();&lt;br /&gt;
  accel.addAttachListener((AttachEvent ae) -&amp;gt; {&lt;br /&gt;
      Accelerometer accel = (Accelerometer) ae.getSource();&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
      }&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  // Define the attach handler function&lt;br /&gt;
  void CCONV onAttachedEventHandler(PhidgetHandle channel, void *userPtr) {&lt;br /&gt;
      printf(&amp;quot;A channel has been attached\n&amp;quot;);&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int main() {&lt;br /&gt;
    // .....Then, in the main code create the channel and set the attach handler:&lt;br /&gt;
    PhidgetAccelerometerHandle accel;&lt;br /&gt;
    PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
    Phidget_setOnAttachHandler((PhidgetHandle)accel, onAttachedEventHandler, NULL);&lt;br /&gt;
 &lt;br /&gt;
    // other stuff in main&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should set the attach event handler &#039;&#039;&#039;before&#039;&#039;&#039; you &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; the channel; otherwise, you may miss attach event if it occurs between opening the channel and setting the handler.&lt;br /&gt;
&lt;br /&gt;
== Do Things with a Channel ==&lt;br /&gt;
&lt;br /&gt;
After a channel has been &#039;&#039;opened&#039;&#039; and &#039;&#039;attached&#039;&#039;, software can control the Phidget device via the channel class interface.&lt;br /&gt;
&lt;br /&gt;
Like setting an event handler that executes [[#Attaching a Channel|when the channels is attached]], handlers can be set to receive events when a state change occurs, or sensor data is received.&lt;br /&gt;
&lt;br /&gt;
A Voltage Input channel for example, like one supported by the  [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit], can set an event handler that gets called as the voltage is processed.&lt;br /&gt;
&lt;br /&gt;
In Java, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  sensorIn.addVoltageChangeListener((VoltageInputVoltageChangeEvent de) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Voltage: &amp;quot; + de.getVoltage());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onVoltageChangeHandler(PhidgetVoltageInputHandle voltageInput, void *ctx, double voltage) {				&lt;br /&gt;
	printf(&amp;quot;Voltage: %lf V\n&amp;quot;, voltage);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // .....Then, in the main code:&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  PhidgetVoltageInput_setOnVoltageChangeHandler(sensorIn, onVoltageChangeHandler, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The voltage value delivered in the above snippets could also be accessed via the &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property of the channel.  Properties that have related events are automatically updated each time the event is received by the channel, and calls between events always return the value set by the last event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    double val = sensorIn.getVoltage();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
    double val;&lt;br /&gt;
    PhidgetVoltageInput_getVoltage(sensorIn, &amp;amp;val);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sensors, Input, and Output===&lt;br /&gt;
&lt;br /&gt;
Often, your Phidget will support more than one channel class.  For example a [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit] has voltage inputs (black sensor ports), digital inputs and digital outputs (green terminal blocks). You can determine the channel classes supported by your Phidget from the product page and the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
For InterfaceKits like the 1018:&lt;br /&gt;
&lt;br /&gt;
* To the voltage inputs, you can attach various sensors (temperature, humidity, light, sound)   &lt;br /&gt;
* To the digital inputs, you can attach various input devices (switches)&lt;br /&gt;
* To the digital outputs, you can attach simple indicators (LEDs, buzzers, relays)&lt;br /&gt;
&lt;br /&gt;
The features and channels supported by each Phidget are many and varied, and we only include general concepts on this page. You can view the complete list of channels supported by a Phidget in the {{Phidget22API}}. Select a device at the top of the screen, select your preferred programming language, and then select which channel class you want to view the documentation for.&lt;br /&gt;
&lt;br /&gt;
== Close a Channel ==&lt;br /&gt;
&lt;br /&gt;
When you are finished with a channel, you should close and (in some languages) delete it.  Once closed, the Phidget device channel the channel was matched to will be released and made available for another channel to match.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  device.close();&lt;br /&gt;
  device = null;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_close((PhidgetHandle)channel);&lt;br /&gt;
 Phidget_release((PhidgetHandle)&amp;amp;channel); // will NULL the pointer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - How to know you are attaching to the Phidget you want.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28243</id>
		<title>Phidget Programming Basics</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28243"/>
		<updated>2017-07-11T15:43:43Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Do Things with a Channel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
It is important to understand what a Phidget is, and how the hardware and software relate to one another before writing software using the {{Phidget22API}}.  Please refer to [[What is a Phidget?]] for an introduction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software library matches a Phidget device channel with a software channel that is created by a user.&lt;br /&gt;
Software channels are each of a class, where that class defines a set of methods, properties, events and errors.  Phidget devices are aware of the channel classes, and expose access to features of the device through the class interface.&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, you&#039;ll want to:&lt;br /&gt;
# &#039;&#039;&#039;[[#Creating a Channel|Create]]&#039;&#039;&#039; an instance of the channel class that provides access to the device you want to control.&lt;br /&gt;
# &#039;&#039;&#039;[[#Opening a Channel|Open]]&#039;&#039;&#039; the channel.&lt;br /&gt;
# Detect when the channel is &#039;&#039;&#039;[[#Attaching a Channel|attached]]&#039;&#039;&#039; to the Phidget.&lt;br /&gt;
# Use &#039;&#039;&#039;[[#Do Things with a Channel|methods and properties]]&#039;&#039;&#039; of the attached channel.&lt;br /&gt;
# &#039;&#039;&#039;[[#Close a Channel|Close]]&#039;&#039;&#039; the channel.&lt;br /&gt;
&lt;br /&gt;
Small code snippets are provided for each step in the sections below.  [[Language - Java|Java]] and [[Language - C/C++|C]] were selected because Java is a high-level language and C is a low level language.  The best reference to the &#039;&#039;features&#039;&#039; each channel class is the {{Phidget22API}} for your specific language.  This page is intended to be a high-level introduction.&lt;br /&gt;
&lt;br /&gt;
== Creating a Channel ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exposes one or more device channels where each channel is an interface to a feature of the hardware.  A channel class defines an interface to control and query a channel of the Phidget device.  For example, a 1018 - Phidget InterfaceKit device includes support&lt;br /&gt;
for digital input and digital out, and therefore exports &amp;lt;code&amp;gt;DigitalInput&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DigitalOutput&amp;lt;/code&amp;gt; channels.&lt;br /&gt;
&lt;br /&gt;
Phidget devices are controlled by creating an instance of a Phidget channel class.  Each channel class has a function that allows you to &#039;&#039;&#039;create&#039;&#039;&#039; an instance, and a function to later &#039;&#039;&#039;delete&#039;&#039;&#039; the instance.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 Accelerometer accel = new Accelerometer();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 RFID rfid = new RFID();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 PhidgetAccelerometerHandle accel;&lt;br /&gt;
 PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 PhidgetRFIDHandle rfid;&lt;br /&gt;
 PhidgetRFID_create(&amp;amp;rfid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A channel class will have properties and methods specific to the feature they belong to. For example, the &amp;lt;code&amp;gt;VoltageInput&amp;lt;/code&amp;gt; class has a &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property that lets you access the current voltage measured by the device, the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; class has properties to set the sensitivity on each axis and the &amp;lt;code&amp;gt;RFID&amp;lt;/code&amp;gt; has a method that writes to a writable RFID tag.&lt;br /&gt;
&lt;br /&gt;
== Opening a Channel ==&lt;br /&gt;
&lt;br /&gt;
After you have created a [[#Creating a Channel| channel instance]], you can call the &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; method to begin the process of matching the channel to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
For example, with the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channel in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 accel.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_open((PhidgetHandle) accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To see how to use &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; in other languages, please refer to the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function begins the process of matching the channel handle you created to a channel of a Phidget device, and does not actually &#039;&#039;open&#039;&#039; the Phidget itself.  An open channel may match a new Phidget device when it is plugged in if that channel has not already found a Phidget device channel that meets its criteria.&lt;br /&gt;
&lt;br /&gt;
===Details for Open()===&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; simply enables the process of matching a channel to a Phidget device channel, &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; returns immediately and does not wait for the channel to attach to a Phidget device channel. The channel changes state to &#039;&#039;open&#039;&#039;, but is not necessarily &#039;&#039;attached&#039;&#039;.  Until the channel actually becomes &#039;&#039;attached&#039;&#039; to a Phidget device channel, most of the channel class interface will not be available.&lt;br /&gt;
&lt;br /&gt;
As long as the channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel that has not already been matched with a channel.  The channel can only be matched once, and the Phidget device channel can only be matched once.  For example, if you connected a single Phidget Accelerometer to a computer, and you created and opened two &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channels, only one of the channels would attach while the other would remain &#039;&#039;open&#039;&#039; but not &#039;&#039;attached&#039;&#039;. If the &#039;&#039;attached&#039;&#039; channel were to be closed, the other channel would then attach to the Accelerometer.&lt;br /&gt;
&lt;br /&gt;
When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Once a Phidget device channel is matched to a channel, it will not be available to match another channel until the first channel is closed.  The one exception is if the Phidget device channel is &#039;&#039;attached&#039;&#039; over the network through the [[Phidget Network Server]].  In that case, multiple channels are allowed to match and attach to the Phidget device channel. Some Phidgets, such as motor controllers will never match more than one channel at a time.&lt;br /&gt;
&lt;br /&gt;
===Channel Matching===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; can be used without specifying any matching properties.  In this case the system will match a channel with the first Phidget device channel that matches the channel class.  The channel class itself is an implied matching parameter.&lt;br /&gt;
&lt;br /&gt;
If there is more than one Phidget connected to the computer that supports the channel class, the &amp;lt;code&amp;gt;DeviceSerialNumber&amp;lt;/code&amp;gt; property must be set determine which Phidget will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget connected to the computer has more than one channel of the same classes, the &amp;lt;code&amp;gt;Channel&amp;lt;/code&amp;gt; property must be set to determine which Phidget device channel will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget Network Server is enabled on the computer, the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; properties can be used to determine if the channel will match the Phidget device channel or the network channel.&lt;br /&gt;
&lt;br /&gt;
Please refer to the {{Phidget22API}}, and [[Best Phidgets Practices|Best Practices]] for more information on matching channels to Phidget device channels.&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a USB Phidget ====&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-1.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a [{{SERVER}}/products.php?product_id=1018 1018 PhidgetInterfaceKit 8/8/8] is connected to a computer. A [{{SERVER}}/products.php?product_id=1108 1108 Magnetic Sensor] is connected to analog port 3 on the 1018. Here&#039;s how you would open the channel for the magnetic sensor in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageRatioInput ch = new VoltageRatioInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(3);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to open digital input 5 in the same example, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(5);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a VINT Hub Port as a Channel ====&lt;br /&gt;
&lt;br /&gt;
The ports on a [[What is VINT?|VINT]] Hub can be opened as Digital Inputs, Digital Outputs, Voltage Inputs, or Voltage Ratio Inputs. Suppose you had a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub] with a [{{SERVER}}/products.php?product_id=1133 1133 Sound Sensor] connected to its sixth port. &lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-2.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how you would open it in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageInput ch = new VoltageInput();&lt;br /&gt;
ch.setDeviceSerialNumber(370181);&lt;br /&gt;
ch.setIsHubPortDevice(true);&lt;br /&gt;
ch.setHubPort(6);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a VINT Device ====&lt;br /&gt;
&lt;br /&gt;
In this example, we have a [{{SERVER}}/products.php?product_id=TMP1101 TMP1101 4x Thermocouple Phidget] and a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub].&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-3.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
If we wanted to open both the thermocouple connected to port 0 and the integrated temperature sensor on the board, we first need to figure out which channels those are. Go to the [{{SERVER}}/products.php?product_id=TMP1101 product page for the TMP1101] and click on the API tab. From the table at the top of the tab, we can see that the integrated temperature sensor is TemperatureSensor channel 4. In Java, opening these channels would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
TemperatureSensor tc = new TemperatureSensor(); // Handle for the thermocouple&lt;br /&gt;
TemperatureSensor ic = new TemperatureSensor(); // Handle for the integrated temperature chip&lt;br /&gt;
&lt;br /&gt;
tc.setDeviceSerialNumber();&lt;br /&gt;
ic.setDeviceSerialNumber();&lt;br /&gt;
tc.setHubPort(2);&lt;br /&gt;
ic.setHubPort(2);&lt;br /&gt;
tc.setChannel(0);&lt;br /&gt;
ic.setChannel(4);&lt;br /&gt;
&lt;br /&gt;
tc.open();&lt;br /&gt;
ic.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a Remote Phidget ====&lt;br /&gt;
&lt;br /&gt;
Suppose you wanted to open a locally connected Phidget remotely over the Network Service, so that you could have multiple programs connecting to it at the same time. In this example, we have a [{{SERVER}}/products.php?product_id=1024 1024 PhidgetRFID] connected to a computer that has the Phidget Network Server enabled.&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-4.jpg|link=|600px]]&lt;br /&gt;
&lt;br /&gt;
You could open the RFID reader channel remotely with the following code in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
RFID ch = new RFID();&lt;br /&gt;
ch.setDeviceSerialNumber(388624);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Attaching a Channel ==&lt;br /&gt;
&lt;br /&gt;
When a channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel until either a match is found, or the channel is closed.  When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
An event handler can be registered to catch the &#039;&#039;attach event&#039;&#039; through the channel&#039;s &amp;lt;code&amp;gt;Attach&amp;lt;/code&amp;gt; event.  Each channel also has a &amp;lt;code&amp;gt;Attached&amp;lt;/code&amp;gt; property that can be read to determine if the class has&lt;br /&gt;
been matched to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
To set an event handler to detect the attach in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  //create a Phidget accelerometer channel:&lt;br /&gt;
  Accelerometer accel = new Accelerometer();&lt;br /&gt;
  accel.addAttachListener((AttachEvent ae) -&amp;gt; {&lt;br /&gt;
      Accelerometer accel = (Accelerometer) ae.getSource();&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
      }&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  // Define the attach handler function&lt;br /&gt;
  void CCONV onAttachedEventHandler(PhidgetHandle channel, void *userPtr) {&lt;br /&gt;
      printf(&amp;quot;A channel has been attached\n&amp;quot;);&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int main() {&lt;br /&gt;
    // .....Then, in the main code create the channel and set the attach handler:&lt;br /&gt;
    PhidgetAccelerometerHandle accel;&lt;br /&gt;
    PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
    Phidget_setOnAttachHandler((PhidgetHandle)accel, onAttachedEventHandler, NULL);&lt;br /&gt;
 &lt;br /&gt;
    // other stuff in main&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should set the attach event handler &#039;&#039;&#039;before&#039;&#039;&#039; you &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; the channel; otherwise, you may miss attach event if it occurs between opening the channel and setting the handler.&lt;br /&gt;
&lt;br /&gt;
== Do Things with a Channel ==&lt;br /&gt;
&lt;br /&gt;
After a channel has been &#039;&#039;opened&#039;&#039; and &#039;&#039;attached&#039;&#039;, software can control the Phidget device via the channel class interface.&lt;br /&gt;
&lt;br /&gt;
Like setting an event handler that executes [[#Attaching a Channel|when the channels is attached]], handlers can be set to receive events when a state change occurs, or sensor data is received.&lt;br /&gt;
&lt;br /&gt;
A Voltage Input channel for example, like one supported by the  [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit], can set an event handler that gets called as the voltage is processed.&lt;br /&gt;
&lt;br /&gt;
In Java, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  sensorIn.addVoltageChangeListener((VoltageInputVoltageChangeEvent de) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Voltage: &amp;quot; + de.getVoltage());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV&lt;br /&gt;
  onVoltageChangeHandler(PhidgetVoltageInputHandle voltageInput, void *ctx, double voltage) {				&lt;br /&gt;
	printf(&amp;quot;Voltage: %lf V\n&amp;quot;, voltage);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // .....Then, in the main code:&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  PhidgetVoltageInput_setOnVoltageChangeHandler(sensorIn, onVoltageChangeHandler, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The voltage value delivered in the above snippets could also be accessed via the &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property of the channel.  Properties that have related events are automatically updated each time the event is received by the channel, and calls between events always return the value set by the last event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    double val = sensorIn.getVoltage();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
    double val;&lt;br /&gt;
    PhidgetVoltageInput_getVoltage(sensorIn, &amp;amp;val);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sensors, Input, and Output===&lt;br /&gt;
&lt;br /&gt;
Often, your Phidget support more than one channel class.  For example a [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit] has voltage inputs (black sensor ports), digital inputs and digital outputs (green terminal blocks). You can determine the channel classes supported by your Phidget from the product page and the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
For InterfaceKits like the 1018:&lt;br /&gt;
&lt;br /&gt;
* To the voltage inputs, you can attach various sensors (temperature, humidity, light, sound)   &lt;br /&gt;
* To the digital inputs, you can attach various input devices (switches)&lt;br /&gt;
* To the digital outputs, you can attach simple indicators (LEDs, buzzers, relays)&lt;br /&gt;
&lt;br /&gt;
The features and channels supported by each Phidget are many and varied, and we only include general concepts on this page. You can view the complete list of channels supported by a Phidget in the {{Phidget22API}}. Select a device at the top of the screen, select your preferred programming language, and then select which channel class you want to view the documentation for.&lt;br /&gt;
&lt;br /&gt;
== Close a Channel ==&lt;br /&gt;
&lt;br /&gt;
When you are finished with a channel, you should close and (in some languages) delete it.  Once closed, the Phidget device channel the channel was matched to will be released and made available for another channel to match.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  device.close();&lt;br /&gt;
  device = null;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_close((PhidgetHandle)channel);&lt;br /&gt;
 Phidget_release((PhidgetHandle)&amp;amp;channel); // will NULL the pointer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - How to know you are attaching to the Phidget you want.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28242</id>
		<title>Phidget Programming Basics</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Phidget_Programming_Basics&amp;diff=28242"/>
		<updated>2017-07-11T15:42:06Z</updated>

		<summary type="html">&lt;p&gt;Chad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
It is important to understand what a Phidget is, and how the hardware and software relate to one another before writing software using the {{Phidget22API}}.  Please refer to [[What is a Phidget?]] for an introduction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Phidget software library matches a Phidget device channel with a software channel that is created by a user.&lt;br /&gt;
Software channels are each of a class, where that class defines a set of methods, properties, events and errors.  Phidget devices are aware of the channel classes, and expose access to features of the device through the class interface.&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, you&#039;ll want to:&lt;br /&gt;
# &#039;&#039;&#039;[[#Creating a Channel|Create]]&#039;&#039;&#039; an instance of the channel class that provides access to the device you want to control.&lt;br /&gt;
# &#039;&#039;&#039;[[#Opening a Channel|Open]]&#039;&#039;&#039; the channel.&lt;br /&gt;
# Detect when the channel is &#039;&#039;&#039;[[#Attaching a Channel|attached]]&#039;&#039;&#039; to the Phidget.&lt;br /&gt;
# Use &#039;&#039;&#039;[[#Do Things with a Channel|methods and properties]]&#039;&#039;&#039; of the attached channel.&lt;br /&gt;
# &#039;&#039;&#039;[[#Close a Channel|Close]]&#039;&#039;&#039; the channel.&lt;br /&gt;
&lt;br /&gt;
Small code snippets are provided for each step in the sections below.  [[Language - Java|Java]] and [[Language - C/C++|C]] were selected because Java is a high-level language and C is a low level language.  The best reference to the &#039;&#039;features&#039;&#039; each channel class is the {{Phidget22API}} for your specific language.  This page is intended to be a high-level introduction.&lt;br /&gt;
&lt;br /&gt;
== Creating a Channel ==&lt;br /&gt;
&lt;br /&gt;
A Phidget device exposes one or more device channels where each channel is an interface to a feature of the hardware.  A channel class defines an interface to control and query a channel of the Phidget device.  For example, a 1018 - Phidget InterfaceKit device includes support&lt;br /&gt;
for digital input and digital out, and therefore exports &amp;lt;code&amp;gt;DigitalInput&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;DigitalOutput&amp;lt;/code&amp;gt; channels.&lt;br /&gt;
&lt;br /&gt;
Phidget devices are controlled by creating an instance of a Phidget channel class.  Each channel class has a function that allows you to &#039;&#039;&#039;create&#039;&#039;&#039; an instance, and a function to later &#039;&#039;&#039;delete&#039;&#039;&#039; the instance.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 Accelerometer accel = new Accelerometer();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 RFID rfid = new RFID();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new Accelerometer channel&lt;br /&gt;
 PhidgetAccelerometerHandle accel;&lt;br /&gt;
 PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 // Create a new RFID channel&lt;br /&gt;
 PhidgetRFIDHandle rfid;&lt;br /&gt;
 PhidgetRFID_create(&amp;amp;rfid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A channel class will have properties and methods specific to the feature they belong to. For example, the &amp;lt;code&amp;gt;VoltageInput&amp;lt;/code&amp;gt; class has a &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property that lets you access the current voltage measured by the device, the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; class has properties to set the sensitivity on each axis and the &amp;lt;code&amp;gt;RFID&amp;lt;/code&amp;gt; has a method that writes to a writable RFID tag.&lt;br /&gt;
&lt;br /&gt;
== Opening a Channel ==&lt;br /&gt;
&lt;br /&gt;
After you have created a [[#Creating a Channel| channel instance]], you can call the &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; method to begin the process of matching the channel to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
For example, with the &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channel in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
 accel.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_open((PhidgetHandle) accel);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To see how to use &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; in other languages, please refer to the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; function begins the process of matching the channel handle you created to a channel of a Phidget device, and does not actually &#039;&#039;open&#039;&#039; the Phidget itself.  An open channel may match a new Phidget device when it is plugged in if that channel has not already found a Phidget device channel that meets its criteria.&lt;br /&gt;
&lt;br /&gt;
===Details for Open()===&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; simply enables the process of matching a channel to a Phidget device channel, &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; returns immediately and does not wait for the channel to attach to a Phidget device channel. The channel changes state to &#039;&#039;open&#039;&#039;, but is not necessarily &#039;&#039;attached&#039;&#039;.  Until the channel actually becomes &#039;&#039;attached&#039;&#039; to a Phidget device channel, most of the channel class interface will not be available.&lt;br /&gt;
&lt;br /&gt;
As long as the channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel that has not already been matched with a channel.  The channel can only be matched once, and the Phidget device channel can only be matched once.  For example, if you connected a single Phidget Accelerometer to a computer, and you created and opened two &amp;lt;code&amp;gt;Accelerometer&amp;lt;/code&amp;gt; channels, only one of the channels would attach while the other would remain &#039;&#039;open&#039;&#039; but not &#039;&#039;attached&#039;&#039;. If the &#039;&#039;attached&#039;&#039; channel were to be closed, the other channel would then attach to the Accelerometer.&lt;br /&gt;
&lt;br /&gt;
When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note:&#039;&#039;&#039; Once a Phidget device channel is matched to a channel, it will not be available to match another channel until the first channel is closed.  The one exception is if the Phidget device channel is &#039;&#039;attached&#039;&#039; over the network through the [[Phidget Network Server]].  In that case, multiple channels are allowed to match and attach to the Phidget device channel. Some Phidgets, such as motor controllers will never match more than one channel at a time.&lt;br /&gt;
&lt;br /&gt;
===Channel Matching===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; can be used without specifying any matching properties.  In this case the system will match a channel with the first Phidget device channel that matches the channel class.  The channel class itself is an implied matching parameter.&lt;br /&gt;
&lt;br /&gt;
If there is more than one Phidget connected to the computer that supports the channel class, the &amp;lt;code&amp;gt;DeviceSerialNumber&amp;lt;/code&amp;gt; property must be set determine which Phidget will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget connected to the computer has more than one channel of the same classes, the &amp;lt;code&amp;gt;Channel&amp;lt;/code&amp;gt; property must be set to determine which Phidget device channel will match.&lt;br /&gt;
&lt;br /&gt;
If the Phidget Network Server is enabled on the computer, the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; properties can be used to determine if the channel will match the Phidget device channel or the network channel.&lt;br /&gt;
&lt;br /&gt;
Please refer to the {{Phidget22API}}, and [[Best Phidgets Practices|Best Practices]] for more information on matching channels to Phidget device channels.&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a USB Phidget ====&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-1.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
In this example, a [{{SERVER}}/products.php?product_id=1018 1018 PhidgetInterfaceKit 8/8/8] is connected to a computer. A [{{SERVER}}/products.php?product_id=1108 1108 Magnetic Sensor] is connected to analog port 3 on the 1018. Here&#039;s how you would open the channel for the magnetic sensor in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageRatioInput ch = new VoltageRatioInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(3);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to open digital input 5 in the same example, it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
ch.setDeviceSerialNumber(324781);&lt;br /&gt;
ch.setChannel(5);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a VINT Hub Port as a Channel ====&lt;br /&gt;
&lt;br /&gt;
The ports on a [[What is VINT?|VINT]] Hub can be opened as Digital Inputs, Digital Outputs, Voltage Inputs, or Voltage Ratio Inputs. Suppose you had a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub] with a [{{SERVER}}/products.php?product_id=1133 1133 Sound Sensor] connected to its sixth port. &lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-2.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
Here&#039;s how you would open it in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
VoltageInput ch = new VoltageInput();&lt;br /&gt;
ch.setDeviceSerialNumber(370181);&lt;br /&gt;
ch.setIsHubPortDevice(true);&lt;br /&gt;
ch.setHubPort(6);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a VINT Device ====&lt;br /&gt;
&lt;br /&gt;
In this example, we have a [{{SERVER}}/products.php?product_id=TMP1101 TMP1101 4x Thermocouple Phidget] and a [{{SERVER}}/products.php?product_id=HUB0000 HUB0000 VINT Hub].&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-3.jpg|link=|500px]]&lt;br /&gt;
&lt;br /&gt;
If we wanted to open both the thermocouple connected to port 0 and the integrated temperature sensor on the board, we first need to figure out which channels those are. Go to the [{{SERVER}}/products.php?product_id=TMP1101 product page for the TMP1101] and click on the API tab. From the table at the top of the tab, we can see that the integrated temperature sensor is TemperatureSensor channel 4. In Java, opening these channels would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
TemperatureSensor tc = new TemperatureSensor(); // Handle for the thermocouple&lt;br /&gt;
TemperatureSensor ic = new TemperatureSensor(); // Handle for the integrated temperature chip&lt;br /&gt;
&lt;br /&gt;
tc.setDeviceSerialNumber();&lt;br /&gt;
ic.setDeviceSerialNumber();&lt;br /&gt;
tc.setHubPort(2);&lt;br /&gt;
ic.setHubPort(2);&lt;br /&gt;
tc.setChannel(0);&lt;br /&gt;
ic.setChannel(4);&lt;br /&gt;
&lt;br /&gt;
tc.open();&lt;br /&gt;
ic.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Opening a Channel on a Remote Phidget ====&lt;br /&gt;
&lt;br /&gt;
Suppose you wanted to open a locally connected Phidget remotely over the Network Service, so that you could have multiple programs connecting to it at the same time. In this example, we have a [{{SERVER}}/products.php?product_id=1024 1024 PhidgetRFID] connected to a computer that has the Phidget Network Server enabled.&lt;br /&gt;
&lt;br /&gt;
[[Image:channel-matching-4.jpg|link=|600px]]&lt;br /&gt;
&lt;br /&gt;
You could open the RFID reader channel remotely with the following code in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
RFID ch = new RFID();&lt;br /&gt;
ch.setDeviceSerialNumber(388624);&lt;br /&gt;
ch.setIsRemote(true);&lt;br /&gt;
ch.open();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Attaching a Channel ==&lt;br /&gt;
&lt;br /&gt;
When a channel is open (and not &#039;&#039;attached&#039;&#039;), the system will continue to try to match it with a&lt;br /&gt;
Phidget device channel until either a match is found, or the channel is closed.  When the channels is matched with a Phidget device channel, the channel state is changed to &#039;&#039;attached&#039;&#039; and an event is fired.&lt;br /&gt;
&lt;br /&gt;
An event handler can be registered to catch the &#039;&#039;attach event&#039;&#039; through the channel&#039;s &amp;lt;code&amp;gt;Attach&amp;lt;/code&amp;gt; event.  Each channel also has a &amp;lt;code&amp;gt;Attached&amp;lt;/code&amp;gt; property that can be read to determine if the class has&lt;br /&gt;
been matched to a Phidget device channel.&lt;br /&gt;
&lt;br /&gt;
To set an event handler to detect the attach in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  //create a Phidget accelerometer channel:&lt;br /&gt;
  Accelerometer accel = new Accelerometer();&lt;br /&gt;
  accel.addAttachListener((AttachEvent ae) -&amp;gt; {&lt;br /&gt;
      Accelerometer accel = (Accelerometer) ae.getSource();&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
      }&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  // Define the attach handler function&lt;br /&gt;
  void CCONV onAttachedEventHandler(PhidgetHandle channel, void *userPtr) {&lt;br /&gt;
      printf(&amp;quot;A channel has been attached\n&amp;quot;);&lt;br /&gt;
      // Do things after attachment (i.e. read data, control the device)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  int main() {&lt;br /&gt;
    // .....Then, in the main code create the channel and set the attach handler:&lt;br /&gt;
    PhidgetAccelerometerHandle accel;&lt;br /&gt;
    PhidgetAccelerometer_create(&amp;amp;accel);&lt;br /&gt;
    Phidget_setOnAttachHandler((PhidgetHandle)accel, onAttachedEventHandler, NULL);&lt;br /&gt;
 &lt;br /&gt;
    // other stuff in main&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should set the attach event handler &#039;&#039;&#039;before&#039;&#039;&#039; you &amp;lt;code&amp;gt;open()&amp;lt;/code&amp;gt; the channel; otherwise, you may miss attach event if it occurs between opening the channel and setting the handler.&lt;br /&gt;
&lt;br /&gt;
== Do Things with a Channel ==&lt;br /&gt;
&lt;br /&gt;
After a channel has been &#039;&#039;opened&#039;&#039; and &#039;&#039;attached&#039;&#039;, software can control the Phidget device via the channel class interface.&lt;br /&gt;
&lt;br /&gt;
Like setting an event handler that executes [[#Attaching a Channel|when the channels is attached]], handlers can be set to receive events when a state change occurs, or sensor data is received.&lt;br /&gt;
&lt;br /&gt;
A Voltage Input channel for example, like one supported by the  [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit], can set an event handler that gets called as the voltage is processed.&lt;br /&gt;
&lt;br /&gt;
In Java, this would look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  sensorIn.addVoltageChangeListener((VoltageInputVoltageChangeEvent de) -&amp;gt; {&lt;br /&gt;
            System.out.println(&amp;quot;Voltage: &amp;quot; + de.getVoltage());&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
  void CCONV onVoltageChangeHandler(PhidgetVoltageInputHandle voltageInput, void *userPtr, double voltage) {				&lt;br /&gt;
	printf(&amp;quot;Voltage: %lf V\n&amp;quot;, voltage);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // .....Then, in the main code:&lt;br /&gt;
  // After creating and opening a VoltageInput channel called &amp;quot;sensorIn&amp;quot;&lt;br /&gt;
  PhidgetVoltageInput_setOnVoltageChangeHandler(sensorIn, onVoltageChangeHandler, NULL);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The voltage value delivered in the above snippets could also be accessed via the &amp;lt;code&amp;gt;Voltage&amp;lt;/code&amp;gt; property of the channel.  Properties that have related events are automatically updated each time the event is received by the channel, and calls between events always return the value set by the last event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
    double val = sensorIn.getVoltage();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
    double val;&lt;br /&gt;
    PhidgetVoltageInput_getVoltage(sensorIn, &amp;amp;val);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Sensors, Input, and Output===&lt;br /&gt;
&lt;br /&gt;
Often, your Phidget support more than one channel class.  For example a [{{SERVER}}/products.php?product_id=1018 1018 Phidget Interface Kit] has voltage inputs (black sensor ports), digital inputs and digital outputs (green terminal blocks). You can determine the channel classes supported by your Phidget from the product page and the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
For InterfaceKits like the 1018:&lt;br /&gt;
&lt;br /&gt;
* To the voltage inputs, you can attach various sensors (temperature, humidity, light, sound)   &lt;br /&gt;
* To the digital inputs, you can attach various input devices (switches)&lt;br /&gt;
* To the digital outputs, you can attach simple indicators (LEDs, buzzers, relays)&lt;br /&gt;
&lt;br /&gt;
The features and channels supported by each Phidget are many and varied, and we only include general concepts on this page. You can view the complete list of channels supported by a Phidget in the {{Phidget22API}}. Select a device at the top of the screen, select your preferred programming language, and then select which channel class you want to view the documentation for. &lt;br /&gt;
&lt;br /&gt;
== Close a Channel ==&lt;br /&gt;
&lt;br /&gt;
When you are finished with a channel, you should close and (in some languages) delete it.  Once closed, the Phidget device channel the channel was matched to will be released and made available for another channel to match.&lt;br /&gt;
&lt;br /&gt;
For example, in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
  device.close();&lt;br /&gt;
  device = null;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
 Phidget_close((PhidgetHandle)channel);&lt;br /&gt;
 Phidget_release((PhidgetHandle)&amp;amp;channel); // will NULL the pointer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - How to know you are attaching to the Phidget you want.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;br /&gt;
&lt;br /&gt;
[[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28240</id>
		<title>Best Phidgets Practices</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Best_Phidgets_Practices&amp;diff=28240"/>
		<updated>2017-07-10T18:21:46Z</updated>

		<summary type="html">&lt;p&gt;Chad: /* Don&amp;#039;t Forget to Close() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Programming]]&lt;br /&gt;
The purpose of this page is to provide you with a list of &amp;quot;Best Practices&amp;quot; to follow when writing code that uses Phidgets. These guidelines are especially important if your program is going to be used by people other than you, or on many different systems. If you follow these guidelines, you&#039;ll probably save yourself a few headaches in the future.&lt;br /&gt;
&lt;br /&gt;
== Keep Event Handlers Short ==&lt;br /&gt;
&lt;br /&gt;
User events handlers are called from special dispatch threads within the Phidget library.  Each channel maintains its own queue of events, and these events are processed in order and one at a time.  If a user event handler blocks, or takes longer than the &amp;lt;code&amp;gt;DataInterval&amp;lt;/code&amp;gt; of the channel, events will become &#039;stale&#039;, and eventually the system will begin throwing away events.&lt;br /&gt;
&lt;br /&gt;
For example, if an Accelerometer channel has been configured to receive data every 20 milliseconds and the event handler averages 21ms to execute, the next change event will be ready before the previous one is finished being processed.  Eventually the channel&#039;s event queue will fill and the library will begin throwing away events. &lt;br /&gt;
&lt;br /&gt;
A common mistake is to update a GUI, or wait for user input from within an event handler.  Most GUI APIs have support for dispatching updates to the GUI thread, and those should be used.  Waiting for user input will block the event handler, and should never be done.  Instead, a flag should be set and a loop in the main of the program (or another thread) should wait for the input.&lt;br /&gt;
&lt;br /&gt;
== Be Specific With Open() ==&lt;br /&gt;
&lt;br /&gt;
When you use {{Code|Open()}} to access a Phidget device channel, the library will first try to match a channel of the class that you&#039;re using. There are a number of other matching properties can be set to further filter which device channel is matched. For example:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;serial number&#039;&#039;&#039; of a device or VINT Hub&lt;br /&gt;
* The &#039;&#039;&#039;port&#039;&#039;&#039; a VINT device is plugged into&lt;br /&gt;
* The &#039;&#039;&#039;channel&#039;&#039;&#039; number of a device channel, when a device has multiple channels of the same class&lt;br /&gt;
* If a channel &#039;&#039;&#039;is remote&#039;&#039;&#039; or &#039;&#039;&#039;is local&#039;&#039;&#039;&lt;br /&gt;
* The &#039;&#039;&#039;server name&#039;&#039;&#039;, if a device channel is being accessed over the [[Phidget Network Server]]&lt;br /&gt;
&lt;br /&gt;
If none of the matching properties are set, the Phidget library will match the first channel of the same class. In simple cases this will be fine. For example, if a Phidget temperature sensor is the only device connected to a computer, opening a {{Code|TemperatureSensor}} channel without setting any matching properties will match that temperature sensor; however, if a DC motor controller is connected to the computer, the on-board temperature sensor on the motor control may match.  Setting the {{Code|DeviceSerialNumber}} will force the channel to match the desired device channel.&lt;br /&gt;
&lt;br /&gt;
Since there&#039;s no way to predict what other Phidgets may be plugged into a computer (or become available over the network), you should be as specific as possible before opening a channel.&lt;br /&gt;
&lt;br /&gt;
== When In Doubt, Open Remotely == &lt;br /&gt;
When the [[Phidget Network Server|Network Server]] is enabled, a channel can match both a local device channel and a remote channel.  The end result would be the same as the same actual device channel would be attached; however, most remote channels support being attached to multiple clients, while a local device channel can only be attached once.  Unless the desire is to only allow the device channel to be attached once, or performance is critical, it is more flexible to match the network channel.&lt;br /&gt;
&lt;br /&gt;
See the &amp;lt;code&amp;gt;IsRemote&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;IsLocal&amp;lt;/code&amp;gt; Phidget properties in the {{Phidget22API}}.&lt;br /&gt;
&lt;br /&gt;
== Set Properties in the Attach Handler ==&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;attach&#039;&#039;&#039; event will occur each time a channel is matched. This makes the attach handler the perfect place to initialize properties such as {{Code|DataInterval}}, {{Code|ChangeTrigger}}, gain values, and ranges. If properties are initialize elsewhere, in a situation where the channel detaches those properties will revert to defaults.  Setting properties within the &#039;&#039;&#039;attach&#039;&#039;&#039; handler ensures they are always the expected values.&lt;br /&gt;
&lt;br /&gt;
== Don&#039;t Forget to Close() ==&lt;br /&gt;
&lt;br /&gt;
Calling {{Code|Close()}} on a channel releases the channel so that it can be matched again. In most cases, it&#039;s not technically necessary to call {{Code|Close()}} because when your program exits, the channel will be released; however, it is good practice to close a channel when you are finished with it as some environments will not release the channel automatically. For example in [[Language_-_LabVIEW|LabVIEW]], the runtime environment merely interprets your program, so if you do not call {{Code|Close()}}, the LabVIEW environment will continue to tie up the channel despite having stopped the program you&#039;re working on.&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.&lt;br /&gt;
&lt;br /&gt;
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.&lt;br /&gt;
&lt;br /&gt;
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.&lt;br /&gt;
&lt;br /&gt;
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.&lt;br /&gt;
&lt;br /&gt;
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.&lt;br /&gt;
&lt;br /&gt;
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.&lt;/div&gt;</summary>
		<author><name>Chad</name></author>
	</entry>
</feed>