|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object lejos.nxt.comm.NXTConnection
public abstract class NXTConnection
Generic lejos nxt connection class. Provide access to standard read/write methods. This code supports both asynchronous (used for Bluetooth and RS-485 connections) and synchronous (used for USB) I/O operations for the actual reading and writing of the low level buffers.
NOTE: The code in this class makes a number of assumptions:
setIOMode
function must be over-ridden.
PACKET
mode and can be overridden. Currently this is used for USB
devices. It is not used for Bluetooth/RS-485 connections.
PACKET
mode, an entire packet will fit within
a single USB packet. This limits the maximum packet size which can be
used over USB connections to 63 bytes. This code does not currently
enforce this limit.
Field Summary | |
---|---|
static int |
LCP
Lego Communications Protocol ( LCP ) I/O mode. |
static int |
PACKET
PACKET I/O mode. |
static int |
RAW
RAW I/O mode. |
Constructor Summary | |
---|---|
NXTConnection()
|
Method Summary | |
---|---|
int |
available()
Convenience method that calls available(0) |
int |
available(int what)
Indicate the number of bytes available to be read. |
void |
close()
Close the connection. |
String |
getAddress()
Get the device address set by implementation. |
DataInputStream |
openDataInputStream()
Return the DataInputStream for this connect |
DataOutputStream |
openDataOutputStream()
Return the DataOutputStream for this connection. |
InputStream |
openInputStream()
Return the InputStream for this connection. |
OutputStream |
openOutputStream()
Return the OutputStream for this connection |
int |
read(byte[] data,
int len)
Perform a blocking read on the connection |
int |
read(byte[] data,
int outLen,
boolean wait)
Attempt to read data from the connection. |
int |
readPacket(byte[] buf,
int len)
Read a packet from the stream. |
int |
sendPacket(byte[] buf,
int bufLen)
Send a data packet. |
void |
setIOMode(int mode)
Set operating mode. |
int |
write(byte[] data,
int len)
Perform a blocking write on the connection |
int |
write(byte[] data,
int len,
boolean wait)
Attempt to write bytes to the connection. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int LCP
LCP
) I/O mode. The LCP is defined by The Lego Company to allow limited remote
command control of a NXT brick.
See the Lego Mindstorms Site. Look for the Bluetooth Developer Kit in Support |
Files | Advanced
public static final int PACKET
PACKET
I/O mode. This is default and is probably the best mode to use if you are talking to a
NXT using the leJOS classes. Headers are included for each packet of data sent and received.
public static final int RAW
RAW
I/O mode. This mode is just that and omits any headers. It is used normally for connections to non-NXT
devices such as cell phones, etc.
Constructor Detail |
---|
public NXTConnection()
Method Detail |
---|
public String getAddress()
public int write(byte[] data, int len, boolean wait)
PACKET
and
RAW
write operations. If in PACKET
mode, a set of header bytes indicating
the size of the packet will be sent ahead of the data.
NOTE: If in PACKET
mode, the maximum write will be limited to the
underlying maximum packet length. When using PACKET
mode with writes
larger then the I/O buffer, the wait mode
=true
must be used to ensure correct
operation.
data
- The data to be written.len
- The number of bytes to write.wait
- true
if the call should block until all of the data has
been sent.
wait
was false).
public int read(byte[] data, int outLen, boolean wait)
PACKET
and RAW
mode operations. When
in PACKET
mode, the packet length bytes are automatically processed. The
read will return just a single packet. If the packet is larger then the
requested length, then the rest of the packet will be returned in the
following reads.
If wait
is true
then in PACKET
mode, the call will wait
until either the entire packet can be read or outLen
bytes are available.
In stream mode the call will return if at least 1 byte has been read.
data
- Location to return the data. If null the data is discarded.outLen
- Max number of bytes to read.wait
- Should the call block waiting for data.
public int available(int what)
PACKET
mode and stream connections.
what
- 0 (all modes) return the number of bytes that can be
read without blocking.
1 (packet mode) return the number of bytes still to be
read from the current packet.
2 (packet mode) return the length of the current packet.
public int available()
available(0)
available(int)
public void setIOMode(int mode)
PACKET
mode, it defines the header size to be used.
mode
- I/O mode to be used for this connection. RAW
, LCP
, or PACKET
RAW
,
LCP
,
PACKET
public int read(byte[] data, int len)
data
- byte array to store the results.len
- max number of bytes to read
public int write(byte[] data, int len)
data
- byte array to be written.len
- number of bytes to write
public InputStream openInputStream()
openInputStream
in interface InputConnection
NXTInputStream
public OutputStream openOutputStream()
openOutputStream
in interface OutputConnection
NXTOutputStream
public DataInputStream openDataInputStream()
openDataInputStream
in interface InputConnection
public DataOutputStream openDataOutputStream()
openDataOutputStream
in interface OutputConnection
public void close()
close
in interface Connection
public int readPacket(byte[] buf, int len)
bufSz
), do not return a partial packet.
buf
- Buffer to read data into.len
- Number of bytes to read.
read
.read(byte[],int,boolean)
public int sendPacket(byte[] buf, int bufLen)
buf
- the data to sendbufLen
- the number of bytes to send
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |