rcxport
Class RCXPort

java.lang.Object
  |
  +--rcxport.RCXPort

public class RCXPort
extends Object

Top-level interface to the RCX. This is the top-level interface for interacting with the Lego Mindstorms Robotics Kit RCX. It uses the communication protocol described and documented in the RCX Internals web page. The primary method for sending RCX byte codes to the RCX is the downloadProgram method in this class.

Author:
Scott B. Lewis, slewis@teleport.com

Field Summary
static byte DEFAULTRETRYCOUNT
           
static byte DOWNLOAD_SOUND
           
static byte DOWNLOADCHUNK
           
static String FILE_DELIMITERS
           
static int FILE_RADIX
           
static int INSBUFF
           
static int OUTBUFF
           
static int PORTOPENTIMEOUT
           
static int PORTREADTIMEOUT
           
static String RCXPORTNAME
           
 
Constructor Summary
RCXPort(String port)
          Create an interface to the RCX.
 
Method Summary
 void close()
          Close interaction with this port.
 void deleteSubs()
           
 void deleteTasks()
           
 void downloadFragment(boolean type, byte num, byte[] data)
           
 void downloadProgram(RCXProgram aProg, boolean run)
          Download given program and optionally run it.
 byte[] getByteCodesFromReader(BufferedReader r)
          Get byte codes from a BufferedReader.
 byte[] getByteCodesFromString(String aString, String delimiters, int radix)
          Create byte array from string of byte codes.
 String getPortName()
          Get port name.
protected  String loadStringFromFile(BufferedReader r)
           
static void main(String[] args)
          Test program for Java code to communicate with the Lego Mindstorms RCX.
 void ping()
           
 void playSound(byte sound)
           
 void selectProgram(byte prog)
           
 RCXResult sendData(byte[] data)
          Alternative to sendData/2 that has a retry by default.
 RCXResult sendData(byte[] data, boolean retry)
          Send data to the RCX, and get an RCXResult back.
 void startTask(byte task)
           
 void stopTask(byte task)
           
 void sync()
           
(package private) static void usage()
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSBUFF

public static final int INSBUFF

OUTBUFF

public static final int OUTBUFF

RCXPORTNAME

public static final String RCXPORTNAME

PORTOPENTIMEOUT

public static final int PORTOPENTIMEOUT

PORTREADTIMEOUT

public static final int PORTREADTIMEOUT

FILE_DELIMITERS

public static final String FILE_DELIMITERS

FILE_RADIX

public static final int FILE_RADIX

DOWNLOADCHUNK

public static final byte DOWNLOADCHUNK

DEFAULTRETRYCOUNT

public static final byte DEFAULTRETRYCOUNT

DOWNLOAD_SOUND

public static final byte DOWNLOAD_SOUND
Constructor Detail

RCXPort

public RCXPort(String port)
        throws Exception
Create an interface to the RCX. Opens and prepares comm port for use. Will throw an exception if comm port is not available.
Parameters:
port - the name of the system port to use. This name will be system dependent (e.g. "COM1").
Throws:
Exception - thrown if an identifier cannot be gotten from CommPortIdentifier.getPortIdentifier(), or if other comm port problems occur during initialization.
Method Detail

sendData

public RCXResult sendData(byte[] data,
                          boolean retry)
                   throws IOException
Send data to the RCX, and get an RCXResult back. This is the primary interface for communicating with the RCX.
Parameters:
data - the byte array to send. Must not be null and should have length longer than 0.
Returns:
res a valid RCXResult returned from the RCX.
Throws:
IOException - thrown if port has previously been closed, the packet provided is null, or some problem sending packet or receiving result from RCX

sendData

public RCXResult sendData(byte[] data)
                   throws IOException
Alternative to sendData/2 that has a retry by default.
Parameters:
data - the data to send
Throws:
IOException - thrown if port has previously been closed, the packet provided is null, or some problem sending packet or receiving result from RCX

getPortName

public String getPortName()
Get port name.
Returns:
String name of port being used. System dependent.

close

public void close()
Close interaction with this port.

sync

public void sync()
          throws IOException

ping

public void ping()
          throws IOException

deleteSubs

public void deleteSubs()
                throws IOException

deleteTasks

public void deleteTasks()
                 throws IOException

selectProgram

public void selectProgram(byte prog)
                   throws IOException

playSound

public void playSound(byte sound)
               throws IOException

downloadProgram

public void downloadProgram(RCXProgram aProg,
                            boolean run)
                     throws IOException
Download given program and optionally run it.
Parameters:
aProg - the RCXProgram to download to the RCX.
run - if true, immediately run the downloaded program. If false, just do the download
Throws:
IOException - thrown if some problem communicating with the RCX.

startTask

public void startTask(byte task)
               throws IOException

stopTask

public void stopTask(byte task)
              throws IOException

downloadFragment

public void downloadFragment(boolean type,
                             byte num,
                             byte[] data)
                      throws IOException

getByteCodesFromReader

public byte[] getByteCodesFromReader(BufferedReader r)
                              throws IOException
Get byte codes from a BufferedReader.
Parameters:
r - the BufferedReader to read from
Returns:
the byte codes read from the reader. Uses FILE_DELIMITERS to separate byte codes, and FILE_RADIX to determine the radix used int he file

getByteCodesFromString

public byte[] getByteCodesFromString(String aString,
                                     String delimiters,
                                     int radix)
Create byte array from string of byte codes. Uses second parameter to parse byte codes from String, and uses the radix provided (e.g. hex=16).
Parameters:
aString - the string to read. Must not be null.
delimiters - the delimiters used by the StringTokenizer to parse the given String.
radix - the radix of the numbers given in the first parameter.
Returns:
byte [] that holds the byte codes parsed from the provided String.

loadStringFromFile

protected String loadStringFromFile(BufferedReader r)
                             throws IOException

main

public static void main(String[] args)
                 throws Exception
Test program for Java code to communicate with the Lego Mindstorms RCX. See the RCXPort home page for more information.

Usage: java rcxport.RCXPort -p <comm port> -n <prog num> [-f <filename>] | [-raw <byte codes>]

Options:

-p: serial port (e.g. COM1). Defaults to COM1.

-n: RCX program number (in range 1-5 inclusive). Defaults to 5.

-f: file of byte codes to read. In hex.

-raw: raw byte codes to send to RCX (e.g. 51 3). In hex.

Either the -f param or the -raw param should be provided. If the -f parameter is specified, the file is read for byte codes (in hex). If -raw is specified, byte codes are specified directly on the command line (also in hex):

java rcxport.RCXPort -p COM1 -n 1 -f filename.lis

to read the byte codes from filename.lis, or

java rcxport.RCXPort -p COM1 -n 1 -raw 51 3

to send 51 3 (play sound '3') to the RCX.


usage

static void usage()