rcxcomm: A JavaTM communication API for leJOS

As a part of the project: "Advanced programming of the LEGO RCX for education" a communication package for leJOS has been developed (rcxcomm). This page describes the reason for making rcxcomm, and provides API documentation for it. The software can be downloaded here.

This is the first "release" of this page and the rcxcomm package. Please help us improve the page and the software, by sending any comments and bug reports to: LEGO3 Team at DTU-IAU

Why

Our reason for making this package is that we need a simple way to transfer data to and from a RCX running leJOS. The communication package is written in Java, because we wanted to make some HTTP Servlets which could be used to control the RCX via the Internet.

Documentation

The communication package is using the "JavaTM Communications API" to communicate with the tower (see links). This must be installed correctly before using rcxcomm.
rcxcomm has only been tested with leJOS, but it might work with TinyVM as well.

The software is far from stable.
Here are some of the most important bugs:

The API documentation generated by JavaDoc can be found here.

Download

The rcxcomm package is included in the following JAR file: rcxcomm.jar.
The API documentation for rcxcomm: javadoc.zip

A small sample

Below is the source files of a small sample program. Both files have a .txt file type, which must be removed when downloaded. To run the sample programs, rcxcomm must be available from both the leJOS-environment and the environment where the servlets are running. The easiest way is to extract the files from the jar into your working directory.

SensorReader.java

Compile and download the SensorReader to the RCX. When pushing run, the RCX will start waiting for a byte from the PC. The byte sent from the PC indicates which sensor to read from. When a byte arrive the RCX reads the raw value of the given sensor, and sends the value back to the PC.
import java.io.*;
import rcxcomm.*;
import josx.platform.rcx.*;

/**
 * @author LEGO3 Team at DTU-IAU
 */
public class SensorReader {
  
  public static void main(String args[]) {
    RCXDataPort port = new RCXDataPort();
    int sensorID, sensorValue;
    try {
      while (true) {
        sensorID = port.getInputStream().read();
        sensorValue = Sensor.readSensorValue(sensorID, 0);
        try { // We have to wait because of a bug in the communication.
          Thread.sleep(100);
        } catch (InterruptedException iE) { }
        LCD.showNumber(sensorValue);
        port.getOutputStream().write(sensorValue/256);
        port.getOutputStream().write(sensorValue%256);
        port.getOutputStream().flush();
      }
    } catch (IOException ioE) {
      LCD.showNumber(1111);
    } finally {
      port.close();
      try {
        Thread.sleep(1000);
      } catch (InterruptedException iE) { }
    }
  }

}

read_sensor.jsp

Copy this file to a place below the webroot of your servlet runner (if you have not installed a servlet runner, see links).
Alter the comPort value on the third line, to match your configuration, e.g. "COM1" or "/dev/ttyS0". Start your servlet runner and open the JSP page in your browser. Submit the form, and with a little luck, you should get a result back from the RCX.

Links