lejos.pc.comm
Class NXTCommUSB

java.lang.Object
  extended by lejos.pc.comm.NXTCommUSB
All Implemented Interfaces:
NXTCommRequest, NXTComm
Direct Known Subclasses:
NXTCommFantom, NXTCommLibnxt

public abstract class NXTCommUSB
extends java.lang.Object
implements NXTComm

Base Implementation of NXTComm for USB This module implements two types of I/O over USB. 1. The standard Lego LCP format used for LCP command processing. 2. A Simple packet based protocol that can be used to transport a simple byte stream. Protocol 2 is required (rather then using raw USB operations), to allow the signaling of things like open, and close over the connection. Notes This module assumes that the device read and write functions have a built in timeout period of approx 20 seconds. This module assumes that this timeout exists and uses it to timeout some requests. Should not be used directly - use NXTCommFactory to create an appropriate NXTComm object for your system and the protocol you are using.


Field Summary
 
Fields inherited from interface lejos.pc.comm.NXTComm
LCP, PACKET, RAW
 
Constructor Summary
NXTCommUSB()
           
 
Method Summary
 int available()
          The number of bytes that can be read without blocking.
 void close()
          Close the current device.
protected  void finalize()
           
 java.io.InputStream getInputStream()
          Return an InputStream for reading a stream of data from the NXT over this connection.
 java.io.OutputStream getOutputStream()
          Return an OutputStream for writing a stream of data to the NXT over this connection.
 boolean open(NXTInfo nxt)
          Connect to a NXT found by a search or created from name and address.
 boolean open(NXTInfo nxtInfo, int mode)
          Open a connection to the specified device, and make it available for use.
 byte[] read()
          Read bytes from the device
 NXTInfo[] search(java.lang.String name)
          Locate available nxt devices and return them.
 byte[] sendRequest(byte[] data, int replyLen)
          Send a Lego Command Protocol (LCP) request to the device.
 void write(byte[] data)
          Write bytes to the device.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NXTCommUSB

public NXTCommUSB()
Method Detail

search

public NXTInfo[] search(java.lang.String name)
Locate available nxt devices and return them. Optionally filter the list to those that match name.

Specified by:
search in interface NXTComm
Parameters:
name - The name to search for. If null return all devices.
Returns:
The list of devices.

open

public boolean open(NXTInfo nxtInfo,
                    int mode)
Open a connection to the specified device, and make it available for use.

Specified by:
open in interface NXTComm
Parameters:
nxtInfo - The device to connect to.
mode - the I/O mode to be used on this connection.
Returns:
true if the device is now open, false otherwise.

open

public boolean open(NXTInfo nxt)
             throws NXTCommException
Description copied from interface: NXTComm
Connect to a NXT found by a search or created from name and address.

Specified by:
open in interface NXTComm
Parameters:
nxt - the NXTInfo object for the NXT
Returns:
true if the open succeeded
Throws:
NXTCommException

close

public void close()
           throws java.io.IOException
Close the current device.

Specified by:
close in interface NXTCommRequest
Throws:
java.io.IOException

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

sendRequest

public byte[] sendRequest(byte[] data,
                          int replyLen)
                   throws java.io.IOException
Send a Lego Command Protocol (LCP) request to the device.

Specified by:
sendRequest in interface NXTCommRequest
Parameters:
data - The command to send.
replyLen - How many bytes in the optional reply.
Returns:
The optional reply, or null
Throws:
java.io.IOException - Thrown on errors.

read

public byte[] read()
            throws java.io.IOException
Read bytes from the device

Specified by:
read in interface NXTComm
Returns:
An array of bytes read from the device. null if at EOF
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException
The number of bytes that can be read without blocking.

Specified by:
available in interface NXTComm
Returns:
Bytes available to be read.
Throws:
java.io.IOException

write

public void write(byte[] data)
           throws java.io.IOException
Write bytes to the device.

Specified by:
write in interface NXTComm
Parameters:
data - Data to be written.
Throws:
java.io.IOException

getOutputStream

public java.io.OutputStream getOutputStream()
Description copied from interface: NXTComm
Return an OutputStream for writing a stream of data to the NXT over this connection.

Specified by:
getOutputStream in interface NXTComm
Returns:
the OutputStream object

getInputStream

public java.io.InputStream getInputStream()
Description copied from interface: NXTComm
Return an InputStream for reading a stream of data from the NXT over this connection.

Specified by:
getInputStream in interface NXTComm
Returns:
the InputStream object