Language - Java Windows Javac: Difference between revisions

From Phidgets Support
No edit summary
Line 27: Line 27:




Place both the example file and the phidget22.jar file in the same location. Your folder should now look something like this:
Place the example files and the phidget22.jar file in the same location. Your folder should now look something like this:
[[Image:Java_javac_folder.png|link= | center]]
[[Image:Java_javac_folder.png|link= | center]]


Line 43: Line 43:




[[Image:Java_javac.PNG|link= | center]]
[[Image:Java_javac.PNG|link= | center | 850px]]




Line 50: Line 50:
==Editing the Examples==
==Editing the Examples==
{{Language_-_Java_Editing_the_Examples}}
{{Language_-_Java_Editing_the_Examples}}


==Write Code==
==Write Code==
Line 57: Line 58:
When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Java library.
When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Java library.


First, copy phidget22.jar from the following location:
*C:\Program Files\Phidgets\Phidget22\phidget22.jar


To include the Phidget Java library, add the following import to your code:
Create your new project folder and java file, and place phidget22.jar in the same folder as your new project file. Your folder should look somehting like this:
[[Image:Java_javac_folder_new_project.png|link= | center]]
 
To include the Phidget Java library, add the following import to the top of your code:
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
import com.phidget22.*;
import com.phidget22.*;
</syntaxhighlight>
</syntaxhighlight>


You can now compile the file as shown in the [[#Use Our Examples|Use Our Examples]] section.
Once you are ready to run your program, open the command prompt at the folder location. Next, enter the following command in the command prompt:
<syntaxhighlight lang="java">
javac -classpath .;phidget22.jar example.java
</syntaxhighlight>
 
Finally, enter the following command to run the program:
<syntaxhighlight lang="java">
java -classpath .;phidget22.jar example
</syntaxhighlight>
 
[[Image:Java_javac_new_project.PNG|link= | center]]


The project now has access to Phidgets.
The project now has access to Phidgets.

Revision as of 21:34, 20 August 2018

Java Development Environments
OS - Windows Windows

JAVA JC WIN.png JAVA JC WIN on.png

JAVA NETBEANS.png JAVA NETBEANS on.png

JAVA ECLIPSE.png JAVA ECLIPSE on.png

OS - macOS macOS

JAVA JC TRM.png JAVA JC TRM on.png

JAVA NETBEANS.png JAVA NETBEANS on.png

JAVA ECLIPSE.png JAVA ECLIPSE on.png

OS - Linux Linux

JAVA JC TRM.png JAVA JC TRM on.png

JAVA NETBEANS.png JAVA NETBEANS on.png

JAVA ECLIPSE.png JAVA ECLIPSE on.png

OS - Linux Phidget SBC Linux

JAVA JC SBC.png JAVA JC SBC on.png

OS - Android Android

JAVA AS ANDROID.png JAVA AS ANDROID on.png

Language - Java

Windows with javac

Welcome to using Phidgets with Java! By using Java, you will have access to the complete Phidget22 API, including events.

Javac is a command line-based compiler for java programs that compiles java code into bytecode class files.

Install Phidget Drivers for Windows

Before getting started with the guides below, ensure you have the following components installed on your machine:

  1. You will need the Phidgets Windows Drivers
  2. You will need the Java Development Kit from Oracle

Use Our Examples

One of the best ways to start programming with Phidgets is to use our example code as a guide. Before we get started, make sure you have installed the Phidget drivers.


Next, select an example that will work with your Phidget:


Copy phidget22.jar from the following location:

  • C:\Program Files\Phidgets\Phidget22\phidget22.jar


Place the example files and the phidget22.jar file in the same location. Your folder should now look something like this:

Java javac folder.png


Open the command prompt at the folder location. Next, enter the following command in the command prompt:

javac -classpath .;phidget22.jar example.java


Finally, enter the following command to run the program:

java -classpath .;phidget22.jar example


Java javac.PNG


You should now have the example up and running for your device. Your next step is to look at the Editing the Examples section below for information about the example and important concepts for programming Phidgets. This would be a good time to play around with the device and experiment with some of its functionality.

Editing the Examples

To get our example code to run in a custom application, simply remove the calls to AskForDeviceParameters and PrintEventDescriptions, as well as the ChannelInfo object, then hard-code the addressing parameters for your application.

If you are unsure what values to use for the addressing parameters, check the Finding The Addressing Information page.

For instance:

//You may remove these lines and hard-code the addressing parameters to fit your application
ChannelInfo channelInfo = new ChannelInfo();  //Information from AskForDeviceParameters(). May be removed when hard-coding parameters.
PhidgetHelperFunctions.AskForDeviceParameters(channelInfo, ch);

ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber);
ch.setHubPort(channelInfo.hubPort);
ch.setIsHubPortDevice(channelInfo.isHubPortDevice);
ch.setChannel(channelInfo.channel);

if(channelInfo.netInfo.isRemote) {
    ch.setIsRemote(channelInfo.netInfo.isRemote);
    if(channelInfo.netInfo.serverDiscovery) {
        try {
            Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
        }
        catch (PhidgetException e) {
            PhidgetHelperFunctions.PrintEnableServerDiscoveryErrorMessage(e);
            throw new Exception("Program Terminated: EnableServerDiscovery Failed", e);
        }
    }
    else {
        Net.addServer("Server", channelInfo.netInfo.hostname,
            channelInfo.netInfo.port, channelInfo.netInfo.password, 0);
    }
}


//This call may be harmlessly removed
PrintEventDescriptions();

Might become:

ch.setDeviceSerialNumber(370114);
ch.setHubPort(2);
ch.setIsHubPortDevice(true);

Notice that you can leave out any parameter not relevant to your application for simplicity.

You can then manipulate the rest of the code as your application requires. A more in-depth description of programming with Phidgets can be found in our guide on Phidget Programming Basics.

For future Phidgets-based projects, you can leave out the PhidgetHelperFunctions.java and ChannelInfo.java classes entirely.


Write Code

By following the instructions for your operating system and compiler above, you now have working examples and a project that is configured. This teaching section will help you understand how the examples were written so you can start writing your own code.


Remember: your main reference for writing Java code will be the Phidget22 API Manual and the example code.

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 like this:

DigitalInput ch = new DigitalInput();

Next, we can address which Phidget we want to connect to by setting parameters such as DeviceSerialNumber.

ch.setDeviceSerialNumber(496911);

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:

try {
    DigitalInput ch = new DigitalInput();
    ch.setDeviceSerialNumber(496911);
} catch (PhidgetException ex) {
    System.out.println(ex.getDescription());
}

Step Two: Open and Wait for Attachment

After we have specified which Phidget to connect to, we can open the Phidget object like this:

ch.open(5000);

To use a Phidget, it must be plugged in (attached). We can handle this by calling open(timeout), which will block indefinitely until a connection is made, or until the timeout value is exceeded. Simply calling open() does not guarantee you can use the Phidget immediately.

Alternately, you could verify the device is attached by using event driven programming and tracking the attach events.

To use events to handle attachments, we have to modify our code slightly:

ch.addAttachListener(new AttachListener() {
    public void onAttach(AttachEvent ae) {
        printf("Phidget attached!\n");
});

ch.open(5000);

We recommend using this attach listener to set any initialization parameters for the channel such as DataInterval and ChangeTrigger from within the AttachListener, so the parameters are set as soon as the device becomes available.

Step Three: Do Things with the Phidget

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 listener for a state change event:

DigitalInput ch = new DigitalInput();

ch.addAttachListener(new AttachListener() {
    public void onAttach(AttachEvent ae) {
        printf("Phidget attached!\n");
});

ch.addStateChangeListener(new DigitalInputStateChangeListener() {
    public void onStateChange(DigitalInputStateChangeEvent e) {
        System.out.println("State changed: " + e.getState());
    }
});

ch.open(5000);

This code will connect a function to an event. In this case, the onStateChange function will be called when there has been a change to the channel's input.

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.

If events do not suit your needs, you can also poll the device directly for data using code like this:

boolean state = ch.getState();
System.out.println("State: " + state);

Important Note: 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.

Enumerations

Some Phidget devices have functions that deal with specific predefined values called enumerations. Enumerations commonly provide readable names to a set of numbered options.

Enumerations with Phidgets in Java will take the form of com.phidget22.EnumerationType.ENUMERATION_NAME.

For example, specifying a SensorType to use the 1142 for a voltage input would look like:

com.phidget22.VoltageSensorType.PN_1142

and specifying a K-Type thermocouple for a temperature sensor would be:

com.phidget22.ThermocoupleType.K

The Phidget error code for timing out could be specified as:

com.phidget22.ErrorCode.TIMEOUT

You can find the Enumeration Type under the Enumerations section of the Phidget22 API for your device, and the Enumeration Name in the drop-down list within.

Step Four: Close and Delete

At the end of your program, be sure to close your device:

ch.close();

Setting up a New Project

When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Java library.

First, copy phidget22.jar from the following location:

  • C:\Program Files\Phidgets\Phidget22\phidget22.jar

Create your new project folder and java file, and place phidget22.jar in the same folder as your new project file. Your folder should look somehting like this:

Java javac folder new project.png

To include the Phidget Java library, add the following import to the top of your code:

import com.phidget22.*;

Once you are ready to run your program, open the command prompt at the folder location. Next, enter the following command in the command prompt:

javac -classpath .;phidget22.jar example.java

Finally, enter the following command to run the program:

java -classpath .;phidget22.jar example
Java javac new project.PNG

The project now has access to Phidgets.

Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

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.

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.

Logging, Exceptions, and Errors - Learn about all the tools you can use to debug your program.

Phidget Network Server - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.