SourceForge.net LogoA Guide to JavaHMI

[Previous] | [Table of Contents] | [Next]


2  Developing with JavaHMI

    2.1  Downloading the latest version

You can download the latest JavaHMI version here: http://sourceforge.net/projects/javahmi/

    2.2  Accessing the cvs source tree

JavaHMI's CVS repository can be checked out through anonymous (pserver) CVS with the following instruction set. When prompted for a password for anonymous, simply press the Enter key.

cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/javahmi login
 
cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/javahmi co javahmi

    2.3  Integrated Development Environments (IDEs)

Add content here...

        2.3.1  Eclipse

Add content here...

        2.3.2  NetBeans

Add content here...

    2.4  Development Considerations

Add content here...

    2.5  Writing JNI (Java™ Native Interface) wrappers

        2.5.1  What is JNI

The Java Native Interface enables Java code to be embedded in C code and vice versa.  The advantage of JNI to JavaHMI is that most interface cards and PLC have C interfaces available to them.  To make these resources available to JavaHMI, JNI can be used to "wrap" the C interfaces in Java classes.  These classes are then available to be incorporated with JavaHMI.

        2.5.2  A JNI Primer

Excerpts of the following example were taken from chapter 2 of Sheng Liang's The Java Native Interface.

Step 1:  Declare the native method in a Java™ program.

Code Example 2.1.  HelloWorld.java
class HelloWorld {
	private native void print();
	public static void main(String [] args) {
		new HelloWorld().print();
	}
	static {
		System.loadLibrary("HelloWorld");
	}
}

Compile this program:

%>  javac HelloWorld.java

This creates a file HelloWorld.class in the same directory.

Step 2:  Create the native method header.

%>  javah -jni HelloWorld

This assumes the current directory is included in your class path.  The command above creates the file HelloWorld.h.

Step 3:  Write native method implementation.

Code Example 2.2.  HelloWorld.c
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
}
	printf("Hello World!\n");
	return;
}

%>  gcc -shared \
>  -I$JAVA_HOME/include \
>  -I$JAVA_HOME/include/linux \
>  HelloWorld.c -o libHelloWorld.so

In the command above the "\" are line continuations.  The command above assumes you are running a Linux system.  Please consult your c compiler documentation for variances on your system.  This command also assumes you have an environment variable JAVA_HOME that points to your Java installation.

The command above produces the file libHelloWorld.so which is a shared library file.

Step 4:  Run the program.

%>  java -Djava.library.path=. \
>  -Djava.class.path=.:$CLASSPATH HelloWorld
Hello World!

    2.6  Extending ControlObserver and its subclasses

The following example shows how net.sourceforge.javahmi.mockup.MockDigitalOutput extends net.sourceforge.javahmi.control.ControlDigitalOutput which extends ControlObserver.

Code Example 2.2.  MockDigitalOutput.java
package net.sourceforge.javahmi.mockup;

import net.sourceforge.javahmi.control.ControlDigitalOutput;

public class MockDigitalOutput extends ControlDigitalOutput {

	public final Integer ON = new Integer(1);
	public final Integer OFF = new Integer(0);

	private Integer _value = ON;

	public MockDigitalOutput() {
		start();
	}

	public Number read() {
		return _value;
	}

	public void write(Number value) {
		if (value instanceof Integer) {
			if (value.intValue() != ON.intValue()
				&& value.intValue() != OFF.intValue()) {
				StringBuffer msg = new StringBuffer();
				msg.append("Input parameter must be ");
				msg.append("either MockDigitalObservable.ON ");
				msg.append("or MockDigitalObservable.OFF.");
				throw new IllegalArgumentException(msg.toString());
			}
			_value = (Integer) value;
		} else {
			StringBuffer msg = new StringBuffer();
			msg.append("Input parameter is not of type Integer.");
			throw new IllegalArgumentException(msg.toString());
		}
	}
}
                            

 


[Previous] | [Table of Contents] | [Next]


Please send content suggestions to todd.brunia@xilution.com.