|
|||||||||
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 connections) and synchronous (used for USB and RS485) 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: 1. The input and output buffers have been sized to match the underlying device, and the packet header size. In particular the synchronous code assumes that the entire output buffer can be written using a single atomic write operation. 2. Although the code can handle any size packet header the default is assumed to be 2 bytes. If this is not the case (for example USB), then the setIOMode function must be over-ridden. 3. This class allows the use of a "soft" EOF implementation which uses a zero length packet as an EOF marker. This only operates when in packet mode and can be overridden. Currently this is used for USB and RS485 devices. It is not used for Bluetooth connections. 4. Some devices (like USB), have an inherent packet structure. The current PC assumes that when in 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
|
static int |
PACKET
|
static int |
RAW
|
Constructor Summary | |
---|---|
NXTConnection()
|
Method Summary | |
---|---|
int |
available()
|
int |
available(int what)
Indicate the number of bytes available to be read. |
void |
close()
Close the connection. |
String |
getAddress()
|
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 Bluetooth 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
public static final int PACKET
public static final int RAW
Constructor Detail |
---|
public NXTConnection()
Method Detail |
---|
public String getAddress()
public int write(byte[] data, int len, boolean wait)
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.
public int read(byte[] data, int outLen, boolean wait)
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)
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()
public void setIOMode(int mode)
mode
- I/O mode to be used for this connection. NXTConnection.RAW, .LCP, or .PACKETpublic 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
public OutputStream openOutputStream()
openOutputStream
in interface OutputConnection
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)
buf
- Buffer to read data into.len
- Number of bytes to read.
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 |