rcx
Class RCXPort

java.lang.Object
  extended by rcx.RCXPort

public class RCXPort
extends java.lang.Object

RCXPort - Encapsulates and manages communications to and from the RCX

Version:
2.1 Copyright 2002 Dario Laverde, under terms of GNU LGPL
Author:
Dario Laverde

Field Summary
static boolean debug
          used to display debugging messages
 boolean isEmulation
          used to indicate emulation mode
static boolean skipalive
          used to skip sending the initial alive message
 
Constructor Summary
RCXPort()
          creates the port and calls init(null)
RCXPort(java.lang.String portname)
          creates the port and calls init(portname)
 
Method Summary
 void addAllMessagesListener(AllMessagesListener rl)
          adds an AllMessagesListener
 void addRCXErrorListener(RCXErrorListener rl)
          adds an RCXErrorListener
 void addRCXListener(RCXListener rl)
          adds an RCXListener
 void alive()
          sends an alive message to the RCX
static java.lang.String ArrayToString(byte[] message, int length)
          utility to return a string representation of an array or first part of an array (given a length)
 void beep()
          play system sound (0)
 void beepSequence()
          plays system sound (2)
 void beepSequenceUp()
          plays system sound (3)
 void buzz()
          plays system sound (4)
 void close()
          closes the streams and the port
 void controlMotor(char id, short mode, short power)
           
 float getBatteryPower()
          gets the battery power level in volts
 java.io.InputStream getInputStream()
          gets the InputStream
 java.lang.String getLastError()
          retrieves the last error that occurred
 int getLastValue()
          returns the return value of the last message processed (if it returned a value)
 java.io.OutputStream getOutputStream()
          gets the OutputStream
 void init(java.lang.String port)
          creates an instance of RCXOpcode for looking up opcodes, creates the static instances of Motor and Sensor, and calls open(port)
 boolean isOpen()
          tests to see if port already opened
static void main(java.lang.String[] args)
           
 boolean open()
          actually opens the port and obtains the output and input streams
 void open(java.lang.String port)
          determines the type of port from the port name
(if null will find first available serial port
 void playTone(int frequency, int duration)
          Plays a tone, given its frequency and duration.
 void processMessage(byte[] message)
          processes messages by converting return values and storing the return value internally until retrieved by getLastValue()
 int readSensorValue(int sensorId, int requestType)
           
 boolean send(java.lang.String packet)
          converts string to byte array and send it to RCX
 void setDebug(boolean set)
          sets debug mode on or off
 void setError(java.lang.String msg)
          sets error message and sends it to the errorListener
 void setOpen(boolean set)
          used to overide the port open status (for example, mark as closed when errors occur)
 void setSensorValue(int sensorId, int val, int requestType)
          sets the sensorID value and type (if in emulation mode, sets the value to be read)
 void setStreams(java.io.InputStream in, java.io.OutputStream out)
          sets the streams to swap and creates an instance (thread) of StreamSwap
 void systemSound(boolean queued, int code)
          plays a system sound (0-5)
0:Blip 1:Beep beep 2:Downward tones 3:Upward tones 4:Low buzz 5:Fast upward tones
(note: queued not applied here - used just for compatiblity with leJOS)
 void twoBeeps()
          plays system sound (1)
 void upwardTones()
          plays system sound (5)
 boolean write(byte[] bArray)
          My new simple write!
 boolean writeOrig(byte[] bArray)
          writes a byte array to the RCX port
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static boolean debug
used to display debugging messages


skipalive

public static boolean skipalive
used to skip sending the initial alive message


isEmulation

public boolean isEmulation
used to indicate emulation mode

Constructor Detail

RCXPort

public RCXPort()
creates the port and calls init(null)


RCXPort

public RCXPort(java.lang.String portname)
creates the port and calls init(portname)

Method Detail

init

public void init(java.lang.String port)
creates an instance of RCXOpcode for looking up opcodes, creates the static instances of Motor and Sensor, and calls open(port)


addRCXListener

public void addRCXListener(RCXListener rl)
adds an RCXListener


addAllMessagesListener

public void addAllMessagesListener(AllMessagesListener rl)
adds an AllMessagesListener


addRCXErrorListener

public void addRCXErrorListener(RCXErrorListener rl)
adds an RCXErrorListener


setDebug

public void setDebug(boolean set)
sets debug mode on or off


open

public void open(java.lang.String port)
determines the type of port from the port name
(if null will find first available serial port


alive

public void alive()
sends an alive message to the RCX


open

public boolean open()
actually opens the port and obtains the output and input streams


close

public void close()
closes the streams and the port


isOpen

public boolean isOpen()
tests to see if port already opened


setOpen

public void setOpen(boolean set)
used to overide the port open status (for example, mark as closed when errors occur)


getOutputStream

public java.io.OutputStream getOutputStream()
gets the OutputStream


getInputStream

public java.io.InputStream getInputStream()
gets the InputStream


setStreams

public void setStreams(java.io.InputStream in,
                       java.io.OutputStream out)
sets the streams to swap and creates an instance (thread) of StreamSwap

See Also:
RCXServer

write

public boolean write(byte[] bArray)
My new simple write!

Parameters:
bArray -
Returns:

writeOrig

public boolean writeOrig(byte[] bArray)
writes a byte array to the RCX port


send

public boolean send(java.lang.String packet)
converts string to byte array and send it to RCX


getLastError

public java.lang.String getLastError()
retrieves the last error that occurred


getLastValue

public int getLastValue()
returns the return value of the last message processed (if it returned a value)


ArrayToString

public static java.lang.String ArrayToString(byte[] message,
                                             int length)
utility to return a string representation of an array or first part of an array (given a length)


processMessage

public void processMessage(byte[] message)
processes messages by converting return values and storing the return value internally until retrieved by getLastValue()


setError

public void setError(java.lang.String msg)
sets error message and sends it to the errorListener

See Also:
RCXErrorListener

beep

public void beep()
play system sound (0)


twoBeeps

public void twoBeeps()
plays system sound (1)


beepSequence

public void beepSequence()
plays system sound (2)


beepSequenceUp

public void beepSequenceUp()
plays system sound (3)


buzz

public void buzz()
plays system sound (4)


upwardTones

public void upwardTones()
plays system sound (5)


systemSound

public void systemSound(boolean queued,
                        int code)
plays a system sound (0-5)
0:Blip 1:Beep beep 2:Downward tones 3:Upward tones 4:Low buzz 5:Fast upward tones
(note: queued not applied here - used just for compatiblity with leJOS)


playTone

public void playTone(int frequency,
                     int duration)
Plays a tone, given its frequency and duration. Frequency is audible from about 31 to 2100 Hertz. The duration argument is in hundreds of a seconds and is truncated at 256, so the maximum duration of a tone is 2.56 seconds.

Parameters:
frequency - The frequency of the tone in Hertz (Hz).
duration - The duration of the tone, in centiseconds. Value is truncated at 256 centiseconds.

controlMotor

public void controlMotor(char id,
                         short mode,
                         short power)
Parameters:
id - 'A', 'B' or 'C'.
mode - 1=forward, 2=backward, 3=stop, 4=float
power - 0-7

readSensorValue

public int readSensorValue(int sensorId,
                           int requestType)
Parameters:
sensorId - Sensor ID (0..2).
requestType - 0 = raw value, 1 = canonical value, 2 = boolean value.

setSensorValue

public void setSensorValue(int sensorId,
                           int val,
                           int requestType)
sets the sensorID value and type (if in emulation mode, sets the value to be read)

See Also:
Sensor, SensorConstants

getBatteryPower

public float getBatteryPower()
gets the battery power level in volts


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        java.lang.InterruptedException
Throws:
java.io.IOException
java.lang.InterruptedException