<?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_-_Java_Write_Code</id>
	<title>Template:Language - Java 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_-_Java_Write_Code"/>
	<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Template:Language_-_Java_Write_Code&amp;action=history"/>
	<updated>2026-05-15T12:28:13Z</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_-_Java_Write_Code&amp;diff=29735&amp;oldid=prev</id>
		<title>Jdecoux at 18:49, 10 August 2018</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Template:Language_-_Java_Write_Code&amp;diff=29735&amp;oldid=prev"/>
		<updated>2018-08-10T18:49:32Z</updated>

		<summary type="html">&lt;p&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 18:49, 10 August 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-l44&quot;&gt;Line 44:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 44:&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;We recommend using this attach &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;handler &lt;/del&gt;to set any initialization parameters for the channel such as DataInterval and ChangeTrigger from within the &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;AttachHandler&lt;/del&gt;, so the parameters are set as soon as the device becomes available.&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;We recommend using this attach &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;listener &lt;/ins&gt;to set any initialization parameters for the channel such as DataInterval and ChangeTrigger from within the &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;AttachListener&lt;/ins&gt;, so the parameters are set as soon as the device becomes available.&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;=== Step Three: Do Things with the Phidget ===&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;=== Step Three: Do Things with the Phidget ===&lt;/div&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;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 &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;handler &lt;/del&gt;for a state change event:&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;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 &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;listener &lt;/ins&gt;for a state change event:&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;div&gt;&amp;lt;syntaxhighlight lang=java&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 lang=java&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;div&gt;DigitalInput ch = new DigitalInput();&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;DigitalInput ch = new DigitalInput();&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_-_Java_Write_Code&amp;diff=29696&amp;oldid=prev</id>
		<title>Jdecoux: Created page with &quot;{{WriteCode_Intro|Java|Java}}  === Step One: Create and Address=== You will need to create your Phidget object in your code. For example, we can create a digital input object...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs/index.php?title=Template:Language_-_Java_Write_Code&amp;diff=29696&amp;oldid=prev"/>
		<updated>2018-08-09T20:30:10Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{WriteCode_Intro|Java|Java}}  === Step One: Create and Address=== You will need to create your Phidget object in your code. For example, we can create a digital input object...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{WriteCode_Intro|Java|Java}}&lt;br /&gt;
&lt;br /&gt;
=== Step One: Create and Address===&lt;br /&gt;
You will need to create your Phidget object in your code. For example, we can create a digital input object like this:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new 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=java&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 the return codes of all Phidget functions. Here is an example of the previous code with error handling:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    DigitalInput ch = new DigitalInput();&lt;br /&gt;
    ch.setDeviceSerialNumber(496911);&lt;br /&gt;
} catch (PhidgetException ex) {&lt;br /&gt;
    System.out.println(ex.getDescription());&lt;br /&gt;
}&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=java&amp;gt;&lt;br /&gt;
ch.open(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;&amp;#039;open(&amp;#039;&amp;#039;timeout&amp;#039;&amp;#039;)&amp;#039;&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;&amp;#039;open()&amp;#039;&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=java&amp;gt;&lt;br /&gt;
ch.addAttachListener(new AttachListener() {&lt;br /&gt;
    public void onAttach(AttachEvent ae) {&lt;br /&gt;
        printf(&amp;quot;Phidget attached!\n&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
ch.open(5000);&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=java&amp;gt;&lt;br /&gt;
DigitalInput ch = new DigitalInput();&lt;br /&gt;
&lt;br /&gt;
ch.addAttachListener(new AttachListener() {&lt;br /&gt;
    public void onAttach(AttachEvent ae) {&lt;br /&gt;
        printf(&amp;quot;Phidget attached!\n&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
ch.addStateChangeListener(new DigitalInputStateChangeListener() {&lt;br /&gt;
    public void onStateChange(DigitalInputStateChangeEvent e) {&lt;br /&gt;
        System.out.println(&amp;quot;State changed: &amp;quot; + e.getState());&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
ch.open(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;onStateChange&amp;#039;&amp;#039; function will be called when there has been a change to the channel&amp;#039;s input. &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=java&amp;gt;&lt;br /&gt;
boolean state = ch.getState();&lt;br /&gt;
System.out.println(&amp;quot;State: &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 Java will take the form of &amp;#039;&amp;#039;&amp;#039;com.phidget22.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=java&amp;gt;&lt;br /&gt;
com.phidget22.VoltageSensorType.PN_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=java&amp;gt;&lt;br /&gt;
com.phidget22.ThermocoupleType.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=java&amp;gt;&lt;br /&gt;
com.phidget22.ErrorCode.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 your device:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
ch.close();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jdecoux</name></author>
	</entry>
</feed>