josx.rcxcomm
Class Tower

java.lang.Object
  extended by josx.rcxcomm.Tower

public class Tower
extends java.lang.Object

Low-level interface to the Lego IR Tower Used by RCXPort to send and receive messages to and from the RCX. Can be used to send any packet or any sequence of bytes to the RCX. The tower is opened with a call to open() or open(port) and closed by a call to close(). send can be used to send a packet, and receive to receive one. write can read a sequence of bytes, and read can read them. Note that the methods that have returned error codes in former releases are throwing exceptions now (in particular the open() methods). Additionally, the Tower() constructor with no String argument for the port has beeen removed, for it lead to multiple errors. The send(byte[],int) method is deprecated now and no longer public.


Field Summary
static int DEFAULT_READ_TIMEOUT
           
 
Constructor Summary
Tower(java.lang.String tty)
          Create the tower class.
 
Method Summary
protected  int close()
          Close the tower
 void closeTower()
          Close tower.
 int getError()
          Deprecated. use exception handling instead of status codes
 boolean isRCXAlive()
          Check if RCX is alive
 boolean isUSB()
          Is tower an usb tower?.
protected  int open(java.lang.String p, boolean fastMode)
          Open the tower
 void openTower(boolean fastMode)
          Open the tower.
protected  int read(byte[] b, int timeout)
          Deprecated. do not call native methods, use #readBytes instead
 int readBytes(byte[] data)
          Low-level read.
 int readBytes(byte[] data, int timeout)
          Low-level read.
protected  int receive(byte[] b, int timeout)
          Deprecated. do not call native methods, use #receivePacket instead
 int receivePacket(byte[] data)
          Receive a packet.
 int receivePacket(byte[] data, int timeout)
          Receive a packet.
protected  int send(byte[] b, int n)
          Deprecated. do not call native methods, use #sendPacket instead
 void sendPacket(byte[] data)
          Send a packet to the RCX, e.g 0x10 for ping.
 int sendPacketReceivePacket(byte[] data, byte[] response, int retries)
          Send a packet and retrieve answer.
 int sendPacketReceivePacket(byte[] data, byte[] response, int retries, int timeout)
          Send a packet and retrieve answer.
protected  int write(byte[] b, int n)
          Deprecated. do not call native methods, use #writeBytes instead
 void writeBytes(byte[] data)
          Write low-level bytes to the tower, e.g 0xff550010ef10ef for ping.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_READ_TIMEOUT

public static final int DEFAULT_READ_TIMEOUT
See Also:
Constant Field Values
Constructor Detail

Tower

public Tower(java.lang.String tty)
Create the tower class.

Method Detail

openTower

public void openTower(boolean fastMode)
               throws TowerException
Open the tower.

Throws:
TowerException

closeTower

public void closeTower()
                throws TowerException
Close tower.

Throws:
TowerException

getError

public int getError()
Deprecated. use exception handling instead of status codes

Get last status code.


writeBytes

public void writeBytes(byte[] data)
                throws TowerException
Write low-level bytes to the tower, e.g 0xff550010ef10ef for ping.

Parameters:
data - bytes to send
Throws:
TowerException

sendPacket

public void sendPacket(byte[] data)
                throws TowerException
Send a packet to the RCX, e.g 0x10 for ping.

Parameters:
data - packet to send
Throws:
TowerException

readBytes

public int readBytes(byte[] data)
              throws TowerException
Low-level read.

Parameters:
data - buffer to receive bytes
Returns:
number of bytes read
Throws:
TowerException

readBytes

public int readBytes(byte[] data,
                     int timeout)
              throws TowerException
Low-level read.

Parameters:
data - buffer to receive bytes
timeout - read timeout in ms
Returns:
number of bytes read
Throws:
TowerException

receivePacket

public int receivePacket(byte[] data)
                  throws TowerException
Receive a packet.

Parameters:
data - buffer to receive packet
Returns:
number of bytes read
Throws:
TowerException

receivePacket

public int receivePacket(byte[] data,
                         int timeout)
                  throws TowerException
Receive a packet.

Parameters:
data - buffer to receive packet
timeout - read timeout in ms
Returns:
number of bytes read
Throws:
TowerException

sendPacketReceivePacket

public int sendPacketReceivePacket(byte[] data,
                                   byte[] response,
                                   int retries)
                            throws TowerException
Send a packet and retrieve answer.

Parameters:
data - bytes to send
response - buffer to receive packet
Returns:
number of bytes read
Throws:
TowerException

sendPacketReceivePacket

public int sendPacketReceivePacket(byte[] data,
                                   byte[] response,
                                   int retries,
                                   int timeout)
                            throws TowerException
Send a packet and retrieve answer.

Parameters:
data - bytes to send
response - buffer to receive packet
timeout - read timeout in ms
Returns:
number of bytes read
Throws:
TowerException

open

protected final int open(java.lang.String p,
                         boolean fastMode)
Open the tower

Parameters:
p - port to use, e.g. usb or COM1
fastMode - open port for fast mode transmissions?

close

protected final int close()
Close the tower

Returns:
error number or zero for success

isRCXAlive

public final boolean isRCXAlive()
Check if RCX is alive


isUSB

public final boolean isUSB()
Is tower an usb tower?.


write

protected final int write(byte[] b,
                          int n)
Deprecated. do not call native methods, use #writeBytes instead

Write low-level bytes to the tower, e.g 0xff550010ef10ef for ping.

Parameters:
b - bytes to send
n - number of bytes
Returns:
error number

send

protected final int send(byte[] b,
                         int n)
Deprecated. do not call native methods, use #sendPacket instead

Send a packet to the RCX, e.g 0x10 for ping.

Parameters:
b - packet to send
n - number of bytes
Returns:
error number

read

protected final int read(byte[] b,
                         int timeout)
Deprecated. do not call native methods, use #readBytes instead

Low-level read.

Parameters:
b - buffer to receive bytes
timeout - read timeout in ms
Returns:
number of bytes read

receive

protected final int receive(byte[] b,
                            int timeout)
Deprecated. do not call native methods, use #receivePacket instead

Receive a packet.

Parameters:
b - buffer to receive packet
timeout - read timeout in ms
Returns:
number of bytes read