You are viewing an older revision! See the latest version

Interfacing with Java

Here is a Java library for interfacing Java programs with mbed using the RPC. This could be used for:

  • Creating a GUI for mbed programs
  • Controlling actuators from and reading data into Java programs

Java is potentially a great choice for interfacing to mbed over a network as it can be run in a browser and across many different platforms. This page shows how to use the library.

Information

To used the mbedRPC library your mbed needs to be running code which can receive and execute RPC commands. Information and examples of this, can be found on the Interfacing-Using-RPC page. Compile and run the program for the type of transport mechanism you wish to use.

The mbedRPC Library

Here is the mbedRPC library as a jar file: mbedRPC.jar

Here is an example of the code required to get started and flash some LEDs. This will run as a Java application.

mbedRPC_HelloWorld

import org.mbed.RPC.*;
public class HelloWorld implements mbedRPC{
	DigitalOut d1;
	DigitalOut d4;
	mbed mbed;
	
	public static void main(String[] args) {
		HelloWorld demo = new HelloWorld();
		demo.flash(10);
	}
	
	public HelloWorld(){	
		System.out.println("mbed RPC Hello World");
		//Create an mbed object for communication over serial
		mbed = new SerialRxTxRPC("COM5", 9600);
		//Or:   mbed = HTTPRPC("http://192.168.2.2")
		
		//Create new Digital Outputs on the mbed
		d1 = new DigitalOut(mbed, LED1);
		d4 = new DigitalOut(mbed, LED4);
		
	}
	public void flash(int n){
		
		for(int i = 0; i< n; i++){
			//Call the DigitalOut objects' methods 
			d1.write(1);
			d4.write(0);
			wait(500);
			d1.write(0);
			d4.write(1);
			wait(500);
		}
		mbed.delete();
		System.out.println("Complete");
		
	}
	//A function to create a wait
	public static void wait (int n){
        long startTime,currentTime;
        startTime =System.currentTimeMillis();
        do{
            currentTime = System.currentTimeMillis();
        }
        while ((currentTime - startTime) < n);
	}
}

And here is this class for download: HelloWorld.java

The Code API

The aim in writing the library has been to mirror the mbed API so that programming in Java is very similar to programming for mbed.

The first thing you have to do is import the library into your program. Note that to do this you need to place the Jar in the Java library folders or if you're using an IDE then add it to the build path for that project. If you want to distribute the project you produce then you'll need to package this jar with it.

import org.mbed.RPC.*

You then need to create your class. To assist in using the mbedRPC library implement mbedRPC in the class declaration

public class HelloWorld implements mbedRPC{....

The next step is to create an mbed object which sets up the communication and knows how to execute RPC methods. You can set this up using different transport mechanisms, currently serial or HTTP.

SerialRPC mbed = new SerialRPC("COM5", 9600);
OR:
SerialRxTx mbed = new SerialRxTx("COM5", 9600);
OR:
HTTPRPC mbed = new HTTPRPC("http://192.168.2.2");

For more information on using serial in Java see below.

You can now create objects on mbed. This is done in almost the same way as if you were programming on mbed but as well as specifying the pins you must also pass in the mbed object for the mbed you want to create the object on.

DigitalOut d1 = new DigitalOut(mbed, LED2);
DigitalOut d2 = new DigitalOut(mbed, p20);
AnalogIn ain = new AnalogIn(mbed, p20);
PwmOut p1 = new PwmOut(mbed, LED3);

Alternatively you can tie to objects already created on mbed and then execute their methods.

DigitalOut d3 = new DigitalOut(mbed, "myled");

Finally you can call methods on the objects you have created or tied to. The methods are those described in the Handbook. However unlike on mbed there are no overloaded operators so you have to call the methods by name.

d1.write(1);
d2.write(0);
float i = ain.read();
p1.period(1);
p1.write(1);

Information

If you want to make an RPC call which isn't wrapped by the classes we have provided then use:

String RPCresponse = mbed.RPC(<String ObjectName>,<String MethodName>,<String[] Arguments>);

Setting Up Serial in Java

To use Java in serial and with the mbedRPC library you need to install either the Java Sun Communications API or use RxTx depending on operating system. You can then choose to create your mbed object according to the type of communication you have installed. RxTx would be the best choice for most users.


All wikipages