The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail

Trail: Communication
Lesson: rcxcomm

rcxcomm

by Lawrie Griffiths

Overview

josx.rcxcomm is a package for communication between a PC and the RCX.
It is based on original code created by the LEGO3 Team at DTU-IAU.

It uses standard Java IO Streams to send primitive Java data types from the PC to the RCX, or from the RCX to the PC. Either end can initiate communication, and data can be sent in both directions.

Example

A simple program to send a byte is:


    import josx.rcxcomm.*;
    import java.io.*;

    public class TestRCXComm {

      public static void main(String [] args) {

        try {
          RCXPort port = new RCXPort();

          OutputStream os = port.getOutputStream();

          os.write(123);
          os.flush();
        }
        catch (IOException ioe) {
         ...
        } 
    }

To read a byte back you would add:


      InputStream is = port.getInputStream();
      is.read();

To send other Java primitive types, such as int, you add:


   DataOutputStream dos = new DataOutputStream(os);
      dos.writeInt(123456);
      dos.flush();

Limitations

Not all data types are available in the RCX implementation of DataInputStream and DataOutputStream. Object streams and serialization are not yet supported.

Both serial and USB towers are supported on Windows. Currently only serial towers are supported for Linux and Mac OSX.

Compiling programs that use rcxcomm

To compile the RCX program, you use lejosjc. An extra jar (rcxcomm.jar) is used automatically.

On the PC or Mac you compile with javac and add pcrcxcomm.jar from the lejos lib directory to your CLASSPATH.

Running programs that use rcxcomm

The serial or USB tower is accessed by a dynamically linked library using the Java Native Interface (JNI). On Windows this is irtower.dll and needs to be on you path. As it is in the lejos bin directory, it will normally already be on your PATH. On Linux or Mac OSX, it is a shared library, libirtower.so, which needs to be on your shared library path. This can be achieved by adding the lejos bin directory to LD_LIBRARY_PATH.

The port used for the IR Tower can be set in two ways:

For USB the port name is USB, for a serial tower it is COM1, COM2 etc.

Communicating over the web

There is an extra class available for the PC and Mac - RCXBean. This provides a Java Bean interface for sending and receiving data from the RCX.
It is useful for creating Web sites using Java Server Pages (JSPs) that access the RCX to read sensors, perform actions, etc.

Ignoring messages

For serial towers on the PC or Mac, there is an extra method: setListen.
This is used to keep the serial tower alive for receiving data. Use setListen(true) when you want to receive data and setListen(false) to conserve the serial tower batteries. You don't need to call setListen for USB towers.
setListen is called automatically by the PC version of getInputStream(). You can call setListen(false) if you dont need to listen immediately and you want to conserve batteries.

Going further

With lejos 2.0. low level comms that does not rely on the Lego firmware protocol and opcodes has been added, and RCXPort has been re-implemented using this. This makes it faster and reliable. It will continue to try to send data until it gets through, and ensure hat no data is duplicated. Future versions will be faster still by adding buffering and using larger packet sizes.

With lejos 2.0 there are alternative versions of RCXPort. These are:

You can also write your own protocol stacks and RCX Ports; if you are interested in that, take a look here.

Examples that use rcxcomm

There are several example programs that use RCXPort in the rcxcomm/examples directory. See the browser example for how to browse the Internet from the RCX using a socket proxy on the PC, and servlet for how to turn the RCX into a Web Server using an Http Proxy on the PC.
The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail