public interface UART extends Device<UART>, java.nio.channels.ByteChannel, BufferAccess<java.nio.ByteBuffer>
UART
interface provides methods for controlling and accessing a UART (Universal Asynchronous
Receiver/Transmitter).
A UART device may be identified by the numerical ID and by the name (if any defined) that correspond to its
registered configuration. A UART
instance can be opened by a call to one of the
DeviceManager.open(id,...)
methods using its ID or by a call to one of the
DeviceManager.open(name,...)
methods using its name. When a UART
instance is opened with an ad-hoc
UARTConfig
configuration (which includes its hardware addressing information) using one of the
DeviceManager.open(config,...)
it is not
assigned any ID nor name.
Once opened, an application can read the received data bytes and write the data bytes to be transmitted through the
UART using methods of the ByteChannel
interface.
An application can register a UARTEventListener
instance which will get asynchronously notified of input data
availability, input buffer overrun and/or empty output buffer conditions. Note that the input and output buffers for
which these events may be notified may not necessarily correspond to the transmit and receive FIFO buffers of the
UART hardware but may be buffers allocated by the underlying native driver. To register a UARTEventListener
instance, the application must call the setEventListener
method. The registered
listener can later on be removed by calling the same method with a null
listener parameter.
When done, an application should call the UART.close
method to close the UART. Any further attempt
to access or control a UART which has been closed will result in a ClosedDeviceException
been thrown.UARTPermission
,
ClosedDeviceException
BIG_ENDIAN, LITTLE_ENDIAN, MIXED_ENDIAN
Modifier and Type | Method and Description |
---|---|
void |
generateBreak(int duration)
Generates a break condition for the specified duration.
|
int |
getBaudRate()
Gets the current baud rate.
|
int |
getDataBits()
Gets the current number of bits per character.
|
int |
getParity()
Gets the current parity.
|
int |
getReceiveTimeout()
Gets the current receive timeout.
|
int |
getReceiveTriggerLevel()
Gets the current receive trigger level.
|
int |
getStopBits()
Gets the current number of stop bits per character.
|
int |
read(java.nio.ByteBuffer dst)
Reads a sequence of bytes from this UART into the given buffer.
|
void |
setBaudRate(int baudRate)
Sets the baud rate.
|
void |
setDataBits(int dataBits)
Sets the number of bits per character.
|
void |
setEventListener(int eventId,
UARTEventListener listener)
Registers a
UARTEventListener instance to monitor input data availability, input buffer overrun and/or
empty output buffer conditions. |
void |
setParity(int parity)
Sets the parity.
|
void |
setReceiveTimeout(int timeout)
Sets the receive timeout.
|
void |
setReceiveTriggerLevel(int level)
Sets the receive trigger level.
|
void |
setStopBits(int stopBits)
Sets the number of stop bits per character.
|
void |
startReading(java.nio.ByteBuffer dst1,
java.nio.ByteBuffer dst2,
InputRoundListener<UART,java.nio.ByteBuffer> listener)
Starts asynchronous reading in successive rounds.
|
void |
startReading(java.nio.ByteBuffer dst,
InputRoundListener<UART,java.nio.ByteBuffer> listener)
Starts asynchronous reading in successive rounds - reading data into the provided
buffer.
|
void |
startWriting(java.nio.ByteBuffer src1,
java.nio.ByteBuffer src2,
OutputRoundListener<UART,java.nio.ByteBuffer> listener)
Starts asynchronous writing in successive rounds.
|
void |
startWriting(java.nio.ByteBuffer src,
OutputRoundListener<UART,java.nio.ByteBuffer> listener)
Starts asynchronous writing in successive rounds - initially writing the data remaining in the provided
buffer.
|
void |
stopReading()
Stops (cancels) the currently active reading session.
|
void |
stopWriting()
Stops (cancels) the currently active writing session.
|
int |
write(java.nio.ByteBuffer src)
Writes a sequence of bytes to this UART from the given buffer.
|
getInputBuffer, getOutputBuffer
int getBaudRate() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
setBaudRate
the
device configuration-specific default value is returned.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.int getDataBits() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTConfig.DATABITS_5
, UARTConfig.DATABITS_6
,
UARTConfig.DATABITS_7
, UARTConfig.DATABITS_8
or UARTConfig.DATABITS_9
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.int getParity() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTConfig.PARITY_ODD
, UARTConfig.PARITY_EVEN
,
UARTConfig.PARITY_MARK
, UARTConfig.PARITY_SPACE
, or UARTConfig.PARITY_NONE
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.int getStopBits() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTConfig.STOPBITS_1
, UARTConfig.STOPBITS_1_5
, or
UARTConfig.STOPBITS_2
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void setBaudRate(int baudRate) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
baudRate
- the baud rate to set.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested baud rate.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void setDataBits(int dataBits) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
dataBits
- the number bits per character: UARTConfig.DATABITS_5
, UARTConfig.DATABITS_6
,
UARTConfig.DATABITS_7
, UARTConfig.DATABITS_8
or UARTConfig.DATABITS_9
.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested number of bits per character.java.lang.IllegalArgumentException
- if dataBits
is not one of the defined values.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void setEventListener(int eventId, UARTEventListener listener) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTEventListener
instance to monitor input data availability, input buffer overrun and/or
empty output buffer conditions. While the listener can be triggered by hardware interrupts, there are no
real-time guarantees of when the listener will be called.
A list of event type IDs is defined in UARTEvent
.
If this UART
is open in DeviceManager.SHARED
access mode
the listeners registered by all the applications sharing the underlying device will get
notified of the events they registered for.
If listener
is null
then listener previously registered for the specified event type will be
removed.
Only one listener can be registered at a particular time for a particular event type.eventId
- ID of the native event to listen to: UARTEvent.INPUT_DATA_AVAILABLE
,
UARTEvent.INPUT_BUFFER_OVERRUN
,
UARTEvent.OUTPUT_BUFFER_EMPTY
, UARTEvent.BREAK_INTERRUPT
,
UARTEvent.FRAMING_ERROR
or UARTEvent.PARITY_ERROR
.listener
- the UARTEventListener
instance to be notified upon occurrence of the designated event.java.lang.IllegalArgumentException
- if eventId
does not correspond to any of the supported event types.java.lang.IllegalStateException
- if listener
is not null
and a listener is already registered for the specified event
type.ClosedDeviceException
- if the device has been closed.java.lang.UnsupportedOperationException
- if this UART does not support asynchronous event notification of the requested conditions (eg. input data availability, input
buffer overrun, empty output buffer and/or error conditions).java.io.IOException
- if some other I/O error occurs.UnavailableDeviceException
void setParity(int parity) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
parity
- the speed parity: UARTConfig.PARITY_ODD
, UARTConfig.PARITY_EVEN
,
UARTConfig.PARITY_MARK
, UARTConfig.PARITY_SPACE
, or UARTConfig.PARITY_NONE
.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested parity.java.lang.IllegalArgumentException
- if parity
is not one of the defined values.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void setStopBits(int stopBits) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
stopBits
- the number of stop bits per character: UARTConfig.STOPBITS_1
, UARTConfig.STOPBITS_1_5
,
or UARTConfig.STOPBITS_2
.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested number of stop bits per character.java.lang.IllegalArgumentException
- if stopBits
is not one of the defined values.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void startWriting(java.nio.ByteBuffer src, OutputRoundListener<UART,java.nio.ByteBuffer> listener) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
OutputRoundListener
instance once the initial data have been written. The initial data to be written
is retrieved from the provided buffer; the data to write during the subsequent rounds is retrieved
from that very same buffer upon invocation of the provided OutputRoundListener
instance.
Writing can be stopped by a call to stopWriting
.
r bytes will be written to this UART
,
where r is the number of bytes remaining in the buffer (possibly 0
), that is,
src.remaining()
, at the moment this method is initially invoked
and then subsequently when the listener is returning.
Suppose that a byte sequence of length n is written, where
0 <= n <= r
.
This byte sequence will be transferred from the buffer starting at index
p, where p is the buffer's position at the moment this
method is initially invoked
and then subsequently when the listener is returning; the index of the last byte written will be
p + n - 1
.
Upon invocation of the listener for fetching more data to write the buffer's position will be equal to
p + n
; its limit will not have changed.
stopWriting
is not predictable unless called from within the listener.
The data will be written according to the current baud rate as returned by getBaudRate
. The
baud rate and other configuration parameters can be changed by the provided OutputRoundListener
instance
upon notification of each round.
Note that a buffer with 0
bytes remaining to be written (that is a buffer already empty), at the moment this method is initially
invoked or then subsequently when the listener is returning will not stop the asynchronous operation; the listener is
guaranteed to be called back again at the latest as soon as all other events pending at the time of notification have been dispatched.
Interfering with the asynchronous operation by accessing and modifying the provided buffer concurrently
may yield unpredictable results.
Only one write operation (synchronous or asynchronous) can be going on at any time.
UARTEvent.OUTPUT_BUFFER_EMPTY
) may be
notified to the registered UARTEventListener
independently to the invocation of the provided OutputRoundListener
attempting to call
the write
method from within the registered UARTEventListener
will result in an exception.src
- the buffer for the data to be written.listener
- the OutputRoundListener
instance to be notified when the all the data remaining
in the buffer has been written.java.lang.NullPointerException
- If src
or listener
is null
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.lang.IllegalStateException
- if another synchronous or asynchronous output operation is already active.java.io.IOException
- if some other I/O error occurs such as the device is not writable.void startWriting(java.nio.ByteBuffer src1, java.nio.ByteBuffer src2, OutputRoundListener<UART,java.nio.ByteBuffer> listener) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
startWriting(ByteBuffer, OutputRoundListener)
excepts that it
uses double-buffering. Notification will happen when all the data remaining in the current working buffer (initially src1
) has been written
and writing will proceed with the alternate buffer (which will become the
current working buffer). Writing will only be suspended if the previous event has not yet been handled.
Note that a working buffer with 0
bytes remaining to be written (that is a buffer already empty), at the moment this method is initially
invoked or then subsequently when the listener is returning will not stop the asynchronous operation; the listener is
guaranteed to be called back again at the latest as soon as all other events pending at the time of notification have been dispatched.
Only one write operation (synchronous or asynchronous) can be going on at any time.
UARTEvent.OUTPUT_BUFFER_EMPTY
) may be
notified to the registered UARTEventListener
independently from the invocation of the provided OutputRoundListener
attempting to call
the write
method from within the registered UARTEventListener
will result in an exception.src1
- the first buffer for the data to be written.src2
- the second buffer for the data to be written.listener
- the OutputRoundListener
instance to be notified when all
the data remaining in the working buffer has been written.java.lang.NullPointerException
- If src1
, src2
or listener
is null
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.lang.IllegalStateException
- if another synchronous or asynchronous output operation is already active.java.io.IOException
- if some other I/O error occurs such as the device is not writable.void stopWriting() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void startReading(java.nio.ByteBuffer dst, InputRoundListener<UART,java.nio.ByteBuffer> listener) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
InputRoundListener
is cyclicly notified when the provided buffer has been filled
with input data. Reading into the buffer and notification will only resume once the
event has been handled. Reading and notification will immediately start and will repeat until it is stopped by a
call to stopReading
.
r bytes will be read from this UART
,
where r is the number of bytes remaining in the buffer (possibly 0
), that is,
dst.remaining(), at the moment this method is initially invoked
and then subsequently when the listener is returning.
Suppose that a byte sequence of length n is read, where 0 <= n <= r
.
This byte sequence will be transferred into the buffer so that the first
byte in the sequence is at index p and the last byte is at index
p + n - 1
,
where p is the buffer's position at the moment this
method is initially invoked
and then subsequently when the listener is returning.
Upon invocation of the listener for fetching more data to write the buffer's position will be equal to
p + n
; its limit will not have changed.
stopReading
is not predictable unless called from within the listener..
The data will be read according to the current baud rate as returned by getBaudRate
. The
baud rate and other configuration parameters can be changed by the provided InputRoundListener
instance
upon notification of each round.
Note that a buffer with 0
bytes remaining to be read (that is a buffer already full), at the moment this method is initially
invoked or then subsequently when the listener is returning will not stop the asynchronous operation; the listener is
guaranteed to be called back again at the latest as soon as all other events pending at the time of notification have been dispatched.
Interfering with the asynchronous operation by accessing and modifying the provided buffer concurrently
may yield unpredictable results.
Only one read operation (synchronous or asynchronous) can be going on at any time.
UARTEvent.INPUT_DATA_AVAILABLE
) may be
notified to the registered UARTEventListener
independently from the invocation of the provided InputRoundListener
attempting to call
the read
method from within the registered UARTEventListener
will result in an exception.dst
- the buffer for the data to be read.listener
- the InputRoundListener
instance to be notified when the all remaining
space in the buffer has been filled with input data.java.lang.NullPointerException
- If src
or listener
is null
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.lang.IllegalStateException
- if another synchronous or asynchronous input operation is already active.java.io.IOException
- if some other I/O error occurs such as the device is not readable.void startReading(java.nio.ByteBuffer dst1, java.nio.ByteBuffer dst2, InputRoundListener<UART,java.nio.ByteBuffer> listener) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
startReading(ByteBuffer, InputRoundListener)
excepts that it
uses double-buffering. Notification will happen when all the remaining space in the current working buffer (initially dst1
) has been filled
and reading will proceed with the alternate buffer (which will become the
current working buffer). Reading will only be suspended if the previous event has not yet been handled.
Note that a buffer with 0
bytes remaining to be read (that is a buffer already full), at the moment this method is initially
invoked or then subsequently when the listener is returning will not stop the asynchronous operation; the listener is
guaranteed to be called back again at the latest as soon as all other events pending at the time of notification have been dispatched.
Only one read operation (synchronous or asynchronous) can be going on at any time.
UARTEvent.INPUT_DATA_AVAILABLE
) may be
notified to the registered UARTEventListener
independently from the invocation of the provided InputRoundListener
attempting to call
the read
method from within the registered UARTEventListener
will result in an exception.dst1
- the first buffer for the data to be read.dst2
- the second buffer for the data to be read.listener
- the InputRoundListener
instance to be notified when all
the space remaining in the working buffer has been filled with input data.java.lang.NullPointerException
- If dst1
, dst2
or listener
is null
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.lang.IllegalStateException
- if another synchronous or asynchronous input operation is already active.java.io.IOException
- if some other I/O error occurs such as the device is not readable.void stopReading() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void generateBreak(int duration) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
duration
- duration of the break condition to generate.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested receive trigger level.java.io.IOException
- if some other I/O error occurs.void setReceiveTriggerLevel(int level) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTEventListener
instance registered
for UARTEvent.INPUT_DATA_AVAILABLE
events (if any) will get notified after
the specified number of bytes have been received in the input buffer.
If a synchronous read operation is on-going it may then immediately return
with the number of bytes already read.level
- the trigger level, in bytes.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested receive trigger level.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.int getReceiveTriggerLevel() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
0
if this feature is not supported.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.void setReceiveTimeout(int timeout) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTEventListener
instance registered
for UARTEvent.INPUT_DATA_AVAILABLE
events (if any) will get notified if
there is at least one byte in the input buffer and the specified timeout has elapsed.
If a synchronous read operation is on-going it may then immediately return
with the number of bytes already read.timeout
- the timeout, in milliseconds.java.lang.UnsupportedOperationException
- if this UART cannot be configured with the requested receive timeout.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.int getReceiveTimeout() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
0
if this feature is not supported.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if some other I/O error occurs.int read(java.nio.ByteBuffer dst) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTEvent
with ID
UARTEvent.INPUT_DATA_AVAILABLE
according to the receive trigger level or receive timeout (if set).
read
in interface java.nio.channels.ReadableByteChannel
dst
- The buffer into which bytes are to be transferred-1
if the device has reached end-of-streamjava.lang.NullPointerException
- If dst
is null
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if an I/O error occurred such as the device is not readable.setReceiveTriggerLevel
,
setReceiveTimeout
int write(java.nio.ByteBuffer src) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UARTEvent
with ID
UARTEvent.OUTPUT_BUFFER_EMPTY
.
write
in interface java.nio.channels.WritableByteChannel
src
- The buffer from which bytes are to be retrievedjava.lang.NullPointerException
- If src
is null
.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another application.ClosedDeviceException
- if the device has been closed.java.io.IOException
- if an I/O error occurred such as the device is not writable.Copyright © 2012, 2014, Oracle and/or its affiliates. All rights reserved.
Legal Notices