<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.phidgets.com/docs/index.php?action=history&amp;feed=atom&amp;title=Template%3ALanguage_-_Python_Write_Code</id>
	<title>Template:Language - Python Write Code - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.phidgets.com/docs/index.php?action=history&amp;feed=atom&amp;title=Template%3ALanguage_-_Python_Write_Code"/>
	<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Template:Language_-_Python_Write_Code&amp;action=history"/>
	<updated>2026-05-12T21:52:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.8</generator>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Template:Language_-_Python_Write_Code&amp;diff=30200&amp;oldid=prev</id>
		<title>Jdecoux: /* Step Three: Do Things with the Phidget */</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Template:Language_-_Python_Write_Code&amp;diff=30200&amp;oldid=prev"/>
		<updated>2018-09-05T16:37:12Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Step Three: Do Things with the Phidget&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:37, 5 September 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l68&quot;&gt;Line 68:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 68:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If you are using multiple Phidgets in your program, check out our page on [[&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Using_Multiple_Phidgets&lt;/del&gt;]] for information on how to properly address them and use them in events.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If you are using multiple Phidgets in your program, check out our page on [[&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Using Multiple Phidgets&lt;/ins&gt;]] for information on how to properly address them and use them in events.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If events do not suit your needs, you can also poll the device directly for data using code like this:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;If events do not suit your needs, you can also poll the device directly for data using code like this:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Jdecoux</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs/index.php?title=Template:Language_-_Python_Write_Code&amp;diff=29588&amp;oldid=prev</id>
		<title>Mparadis: Created page with &quot;==Write Code== {{WriteCode_Intro|Python|Python}}  === Step One: Create and Address=== You will need to declare your Phidget object in your code. For example, we can declare a...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Template:Language_-_Python_Write_Code&amp;diff=29588&amp;oldid=prev"/>
		<updated>2018-07-05T19:00:51Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Write Code== {{WriteCode_Intro|Python|Python}}  === Step One: Create and Address=== You will need to declare your Phidget object in your code. For example, we can declare a...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Write Code==&lt;br /&gt;
{{WriteCode_Intro|Python|Python}}&lt;br /&gt;
&lt;br /&gt;
=== Step One: Create and Address===&lt;br /&gt;
You will need to declare your Phidget object in your code. For example, we can declare a digital input object like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ch = DigitalInput()	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, we can address which Phidget we want to connect to by setting parameters such as &amp;#039;&amp;#039;DeviceSerialNumber&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ch.setDeviceSerialNumber(496911)	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although we are not including it on this page, you should handle exceptions of all Phidget functions. Here is an example of the previous code with error handling:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
  ch = DigitalInput()	&lt;br /&gt;
except RuntimeError as e:&lt;br /&gt;
  print(&amp;quot;Runtime Error: %s&amp;quot; % e.message) &lt;br /&gt;
try:&lt;br /&gt;
  ch.setDeviceSerialNumber(496911)&lt;br /&gt;
except PhidgetException as e:&lt;br /&gt;
  print (“Phidget Exception %i: %s” % (e.code, e.details))		&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step Two: Open and Wait for Attachment===&lt;br /&gt;
&lt;br /&gt;
After we have specified which Phidget to connect to, we can open the Phidget object like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ch.openWaitForAttachment(5000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To use a Phidget, it must be plugged in (attached). We can handle this by calling &amp;#039;&amp;#039;openWaitForAttachment&amp;#039;&amp;#039;, which will block indefinitely until a connection is made, or until the timeout value is exceeded. Simply calling &amp;#039;&amp;#039;open&amp;#039;&amp;#039; does not guarantee you can use the Phidget immediately.&lt;br /&gt;
&lt;br /&gt;
Alternately, you could verify the device is attached by using event driven programming and tracking the attach events.&lt;br /&gt;
&lt;br /&gt;
To use events to handle attachments, we have to modify our code slightly:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ch = DigitalInput()	&lt;br /&gt;
ch.setOnAttachHandler(onAttachHandler)&lt;br /&gt;
ch.openWaitForAttachment(5000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, we have to declare the function that will be called when an attach event is fired - in this case the function &amp;#039;&amp;#039;onAttachHandler&amp;#039;&amp;#039; will be called:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
def onAttachHandler(e):&lt;br /&gt;
    print(&amp;quot;Phidget attached!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We recommend using this attach handler to set any initialization parameters for the channel such as DataInterval and ChangeTrigger from within the AttachHandler, so the parameters are set as soon as the device becomes available.&lt;br /&gt;
&lt;br /&gt;
=== Step Three: Do Things with the Phidget ===&lt;br /&gt;
We recommend the use of event driven programming when working with Phidgets. In a similar way to handling an attach event as described above, we can also add an event handler for a state change event:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ch = DigitalInput()	&lt;br /&gt;
ch.setOnAttachHandler(onAttachHandler)&lt;br /&gt;
ch.setOnStateChangeHandler(onStateChangeHandler)&lt;br /&gt;
ch.openWaitForAttachment(5000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code will connect a function to an event. In this case, the &amp;#039;&amp;#039;onStateChangeHandler&amp;#039;&amp;#039; function will be called when there has been a change to the channel&amp;#039;s input. Next, we need to create the &amp;#039;&amp;#039;onStateChangeHandler&amp;#039;&amp;#039; function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
def onStateChangeHandler(self, state):&lt;br /&gt;
    print(&amp;quot;State %f&amp;quot; % state)&lt;br /&gt;
    return 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using multiple Phidgets in your program, check out our page on [[Using_Multiple_Phidgets]] for information on how to properly address them and use them in events.&lt;br /&gt;
&lt;br /&gt;
If events do not suit your needs, you can also poll the device directly for data using code like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
state = ch.getState()&lt;br /&gt;
print(&amp;quot;State %f&amp;quot; % state)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Important Note:&amp;#039;&amp;#039;&amp;#039; There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an exception. See more information on this on our page for [[Unknown Values]].&lt;br /&gt;
&lt;br /&gt;
====Enumerations====&lt;br /&gt;
&lt;br /&gt;
Some Phidget devices have functions that deal with specific predefined values called enumerations. Enumerations commonly provide readable names to a set of numbered options.&lt;br /&gt;
&lt;br /&gt;
Enumerations with Phidgets in Python will take the form of &amp;#039;&amp;#039;&amp;#039;EnumerationType.ENUMERATION_NAME&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, specifying a SensorType to use the 1142 for a voltage input would look like:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
VoltageSensorType.SENSOR_TYPE_1142&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and specifying a K-Type thermocouple for a temperature sensor would be:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ThermocoupleType.THERMOCOUPLE_TYPE_K&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The Phidget error code for timing out could be specified as:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ErrorCode.EPHIDGET_TIMEOUT&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can find the Enumeration Type under the &amp;#039;&amp;#039;Enumerations&amp;#039;&amp;#039; section of the {{Phidget22API}} for your device, and the Enumeration Name in the drop-down list within.&lt;br /&gt;
&lt;br /&gt;
=== Step Four: Close and Delete ===&lt;br /&gt;
At the end of your program, be sure to close and delete your device:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt;&lt;br /&gt;
ch.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mparadis</name></author>
	</entry>
</feed>