gnu.io
Class SerialPort

java.lang.Object
  extended bygnu.io.CommPort
      extended bygnu.io.SerialPort
Direct Known Subclasses:
RXTXPort

public abstract class SerialPort
extends CommPort

Author:
juangui TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates

Field Summary
static int DATABITS_5
          5 data bit format.
static int DATABITS_6
          6 data bit format.
static int DATABITS_7
          7 data bit format.
static int DATABITS_8
          8 data bit format.
static int FLOWCONTROL_NONE
          Flow control off.
static int FLOWCONTROL_RTSCTS_IN
          RTS/CTS flow control on input.
static int FLOWCONTROL_RTSCTS_OUT
          RTS/CTS flow control on output.
static int FLOWCONTROL_XONXOFF_IN
          XON/XOFF flow control on input.
static int FLOWCONTROL_XONXOFF_OUT
          XON/XOFF flow control on output.
static int PARITY_EVEN
          EVEN parity scheme.
static int PARITY_MARK
          MARK parity scheme.
static int PARITY_NONE
          No parity bit.
static int PARITY_ODD
          ODD parity scheme.
static int PARITY_SPACE
          SPACE parity scheme.
static int STOPBITS_1
          Number of STOP bits - 1.
static int STOPBITS_1_5
          Number of STOP bits - 1-1/2.
static int STOPBITS_2
          Number of STOP bits - 2.
 
Constructor Summary
SerialPort()
          Constructor.
 
Method Summary
abstract  void addEventListener(SerialPortEventListener lsnr)
          Registers a SerialPortEventListener object to listen for SerialEvents.
 void disableReceiveFraming()
          Disables receive framing.
 void disableReceiveThreshold()
          Disables receive threshold.
 void disableReceiveTimeout()
          Disables receive timeout.
 void enableReceiveFraming(int framingByte)
          Enables receive framing, if this feature is supported by the driver.
 void enableReceiveThreshold(int thresh)
          Enables receive threshold, if this feature is supported by the driver.
 void enableReceiveTimeout(int rcvTimeout)
          Enables receive timeout, if this feature is supported by the driver.
abstract  int getBaudRate()
          Gets the currently configured baud rate.
abstract  int getDataBits()
          Gets the currently configured number of data bits.
abstract  int getFlowControlMode()
          Gets the currently configured flow control mode.
 int getInputBufferSize()
          Gets the input buffer size.
 java.io.InputStream getInputStream()
          Returns an input stream.
 int getOutputBufferSize()
          Gets the output buffer size.
 java.io.OutputStream getOutputStream()
          Returns an output stream.
abstract  int getParity()
          Get the currently configured parity setting.
 int getReceiveFramingByte()
          Gets the current byte used for receive framing.
 int getReceiveThreshold()
          Gets the integer value of the receive threshold.
 int getReceiveTimeout()
          Gets the integer value of the receive timeout.
abstract  int getStopBits()
          Gets the currently defined stop bits.
abstract  boolean isCD()
          Gets the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.
abstract  boolean isCTS()
          Gets the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.
abstract  boolean isDSR()
          Gets the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.
abstract  boolean isDTR()
          Gets the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
 boolean isReceiveFramingEnabled()
          Checks if receive framing is enabled.
 boolean isReceiveThresholdEnabled()
          Checks if receive threshold is enabled.
 boolean isReceiveTimeoutEnabled()
          Checks if receive timeout is enabled.
abstract  boolean isRI()
          Gets the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.
abstract  boolean isRTS()
          Gets the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
abstract  void notifyOnBreakInterrupt(boolean enable)
          Expresses interest in receiving notification when there is a break interrupt on the line.
abstract  void notifyOnCarrierDetect(boolean enable)
          Expresses interest in receiving notification when the CD (Carrier Detect) bit changes.
abstract  void notifyOnCTS(boolean enable)
          Expresses interest in receiving notification when the CTS (Clear To Send) bit changes.
abstract  void notifyOnDataAvailable(boolean enable)
          Expresses interest in receiving notification when input data is available.
abstract  void notifyOnDSR(boolean enable)
          Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes.
abstract  void notifyOnFramingError(boolean enable)
          Expresses interest in receiving notification when there is a framing error.
abstract  void notifyOnOutputEmpty(boolean enable)
          Expresses interest in receiving notification when the output buffer is empty.
abstract  void notifyOnOverrunError(boolean enable)
          Expresses interest in receiving notification when there is an overrun error.
abstract  void notifyOnParityError(boolean enable)
          Expresses interest in receiving notification when there is a parity error.
abstract  void notifyOnRingIndicator(boolean enable)
          Expresses interest in receiving notification when the RI (Ring Indicator) bit changes.
abstract  void removeEventListener()
          Deregisters event listener registered using addEventListener.
abstract  void sendBreak(int millis)
          Sends a break of millis milliseconds duration.
abstract  void setDTR(boolean dtr)
          Sets or clears the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
abstract  void setFlowControlMode(int flowcontrol)
          Sets the flow control mode.
 void setInputBufferSize(int size)
          Sets the input buffer size.
 void setOutputBufferSize(int size)
          Sets the output buffer size.
 void setRcvFifoTrigger(int trigger)
          Deprecated.
abstract  void setRTS(boolean rts)
          Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
abstract  void setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity)
          Sets serial port parameters.
 
Methods inherited from class gnu.io.CommPort
close, getName, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DATABITS_5

public static final int DATABITS_5
5 data bit format.

See Also:
Constant Field Values

DATABITS_6

public static final int DATABITS_6
6 data bit format.

See Also:
Constant Field Values

DATABITS_7

public static final int DATABITS_7
7 data bit format.

See Also:
Constant Field Values

DATABITS_8

public static final int DATABITS_8
8 data bit format.

See Also:
Constant Field Values

STOPBITS_1

public static final int STOPBITS_1
Number of STOP bits - 1.

See Also:
Constant Field Values

STOPBITS_2

public static final int STOPBITS_2
Number of STOP bits - 2.

See Also:
Constant Field Values

STOPBITS_1_5

public static final int STOPBITS_1_5
Number of STOP bits - 1-1/2. Some UARTs permit 1-1/2 STOP bits only with 5 data bit format, but permit 1 or 2 STOP bits with any format.

See Also:
Constant Field Values

PARITY_NONE

public static final int PARITY_NONE
No parity bit.

See Also:
Constant Field Values

PARITY_ODD

public static final int PARITY_ODD
ODD parity scheme. The parity bit is added so there are an odd number of TRUE bits.

See Also:
Constant Field Values

PARITY_EVEN

public static final int PARITY_EVEN
EVEN parity scheme. The parity bit is added so there are an even number of TRUE bits.

See Also:
Constant Field Values

PARITY_MARK

public static final int PARITY_MARK
MARK parity scheme.

See Also:
Constant Field Values

PARITY_SPACE

public static final int PARITY_SPACE
SPACE parity scheme.

See Also:
Constant Field Values

FLOWCONTROL_NONE

public static final int FLOWCONTROL_NONE
Flow control off.

See Also:
Constant Field Values

FLOWCONTROL_RTSCTS_IN

public static final int FLOWCONTROL_RTSCTS_IN
RTS/CTS flow control on input.

See Also:
Constant Field Values

FLOWCONTROL_RTSCTS_OUT

public static final int FLOWCONTROL_RTSCTS_OUT
RTS/CTS flow control on output.

See Also:
Constant Field Values

FLOWCONTROL_XONXOFF_IN

public static final int FLOWCONTROL_XONXOFF_IN
XON/XOFF flow control on input.

See Also:
Constant Field Values

FLOWCONTROL_XONXOFF_OUT

public static final int FLOWCONTROL_XONXOFF_OUT
XON/XOFF flow control on output.

See Also:
Constant Field Values
Constructor Detail

SerialPort

public SerialPort()
Constructor. SerialPort is an abstract class so it can't be instantiated Instead of that you should use the open() method from CommPortIdentifier class

Method Detail

getBaudRate

public abstract int getBaudRate()
Gets the currently configured baud rate.

Returns:
integer value indicating the baud rate

getDataBits

public abstract int getDataBits()
Gets the currently configured number of data bits.

Returns:
integer that can be equal to DATABITS_5, DATABITS_6, DATABITS_7, or DATABITS_8

getStopBits

public abstract int getStopBits()
Gets the currently defined stop bits.

Returns:
integer that can be equal to STOPBITS_1, STOPBITS_2, or STOPBITS_1_5

getParity

public abstract int getParity()
Get the currently configured parity setting.

Returns:
integer that can be equal to PARITY_NONE, PARITY_ODD, PARITY_EVEN, PARITY_MARK or PARITY_SPACE.

sendBreak

public abstract void sendBreak(int millis)
Sends a break of millis milliseconds duration. Note that it may not be possible to time the duration of the break under certain Operating Systems. Hence this parameter is advisory.

Parameters:
millis - - duration of break to send

setFlowControlMode

public abstract void setFlowControlMode(int flowcontrol)
                                 throws UnsupportedCommOperationException
Sets the flow control mode.

Parameters:
flowcontrol - - control Can be a bitmask combination of - FLOWCONTROL_NONE: no flow control - FLOWCONTROL_RTSCTS_IN: RTS/CTS (hardware) flow control for input - FLOWCONTROL_RTSCTS_OUT: RTS/CTS (hardware) flow control for output - FLOWCONTROL_XONXOFF_IN: XON/XOFF (software) flow control for input - FLOWCONTROL_XONXOFF_OUT: XON/XOFF (software) flow control for output
Throws:
UnsupportedCommOperationException - - if any of the flow control mode was not supported by the underline OS, or if input and output flow control are set to different values, i.e. one hardware and one software. The flow control mode will revert to the value before the call was made.

getFlowControlMode

public abstract int getFlowControlMode()
Gets the currently configured flow control mode.

Returns:
an integer bitmask of the modes FLOWCONTROL_NONE, FLOWCONTROL_RTSCTS_IN, FLOWCONTROL_RTSCTS_OUT, FLOWCONTROL_XONXOFF_IN, and FLOWCONTROL_XONXOFF_OUT.

setRcvFifoTrigger

public void setRcvFifoTrigger(int trigger)
Deprecated. This was advisory only. Set the Receive Fifo trigger level If the uart has a FIFO and if it can have programmable trigger levels, then this method will cause the uart to raise an interrupt after trigger bytes have been received.

Parameters:
trigger - - level

setSerialPortParams

public abstract void setSerialPortParams(int baudrate,
                                         int dataBits,
                                         int stopBits,
                                         int parity)
                                  throws UnsupportedCommOperationException
Sets serial port parameters.

Parameters:
baudrate - - If the baudrate passed in by the application is unsupported by the driver, the driver will throw an UnsupportedCommOperationException
dataBits - - One of the following constants: - DATABITS_5: 5 bits - DATABITS_6: 6 bits - DATABITS_7: 7 bits - DATABITS_8: 8 bits
stopBits - - One of the following constants: - STOPBITS_1: 1 stop bit - STOPBITS_2: 2 stop bits - STOPBITS_1_5: 1.5 stop bits
parity - - One of the following constants: - PARITY_NONE: no parity - PARITY_ODD: odd parity - PARITY_EVEN: even parity - PARITY_MARK: mark parity - PARITY_SPACE: space parity
Throws:
UnsupportedCommOperationException - - if any of the above parameters are specified incorrectly. All four of the parameters will revert to the values before the call was made. DEFAULT: 9600 baud, 8 data bits, 1 stop bit, no parity

setDTR

public abstract void setDTR(boolean dtr)
Sets or clears the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.

Parameters:
dtr - - true: set DTR - false: clear DTR

isDTR

public abstract boolean isDTR()
Gets the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.

Returns:

setRTS

public abstract void setRTS(boolean rts)
Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.

Parameters:
rts - - true: set RTS - false: clear RTS

isRTS

public abstract boolean isRTS()
Gets the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.

Returns:

isCTS

public abstract boolean isCTS()
Gets the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.

Returns:

isDSR

public abstract boolean isDSR()
Gets the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.

Returns:

isRI

public abstract boolean isRI()
Gets the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.

Returns:

isCD

public abstract boolean isCD()
Gets the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.

Returns:

addEventListener

public abstract void addEventListener(SerialPortEventListener lsnr)
                               throws java.util.TooManyListenersException
Registers a SerialPortEventListener object to listen for SerialEvents. Interest in specific events may be expressed using the notifyOnXXX calls. The serialEvent method of SerialPortEventListener will be called with a SerialEvent object describing the event. The current implementation only allows one listener per SerialPort. Once a listener is registered, subsequent call attempts to addEventListener will throw a TooManyListenersException without effecting the listener already registered. All the events received by this listener are generated by one dedicated thread that belongs to the SerialPort object. After the port is closed, no more event will be generated. Another call to open() of the port's CommPortIdentifier object will return a new CommPort object, and the lsnr has to be added again to the new CommPort object to receive event from this port.

Parameters:
lsnr - - The SerialPortEventListener object whose serialEvent method will be called with a SerialEvent describing the event.
Throws:
java.util.TooManyListenersException - - If an initial attempt to attach a listener succeeds, subsequent attempts will throw TooManyListenersException without effecting the first listener.

removeEventListener

public abstract void removeEventListener()
Deregisters event listener registered using addEventListener. This is done automatically at port close.


notifyOnDataAvailable

public abstract void notifyOnDataAvailable(boolean enable)
Expresses interest in receiving notification when input data is available. This may be used to drive asynchronous input. When data is available in the input buffer, this event is propagated to the listener registered using addEventListener.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnOutputEmpty

public abstract void notifyOnOutputEmpty(boolean enable)
Expresses interest in receiving notification when the output buffer is empty. This may be used to drive asynchronous output. When the output buffer becomes empty, this event is propagated to the listener registered using addEventListener. The event will be generated after a write is completed, when the system buffer becomes empty again. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnCTS

public abstract void notifyOnCTS(boolean enable)
Expresses interest in receiving notification when the CTS (Clear To Send) bit changes. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnDSR

public abstract void notifyOnDSR(boolean enable)
Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnRingIndicator

public abstract void notifyOnRingIndicator(boolean enable)
Expresses interest in receiving notification when the RI (Ring Indicator) bit changes. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnCarrierDetect

public abstract void notifyOnCarrierDetect(boolean enable)
Expresses interest in receiving notification when the CD (Carrier Detect) bit changes. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnOverrunError

public abstract void notifyOnOverrunError(boolean enable)
Expresses interest in receiving notification when there is an overrun error. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnParityError

public abstract void notifyOnParityError(boolean enable)
Expresses interest in receiving notification when there is a parity error. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnFramingError

public abstract void notifyOnFramingError(boolean enable)
Expresses interest in receiving notification when there is a framing error. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

notifyOnBreakInterrupt

public abstract void notifyOnBreakInterrupt(boolean enable)
Expresses interest in receiving notification when there is a break interrupt on the line. This notification is hardware dependent and may not be supported by all implementations.

Parameters:
enable - - true: enable notification - false: disable notification

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an input stream. This is the only way to receive data from the communications port. If the port is unidirectional and doesn't support receiving data, then getInputStream returns null.

Specified by:
getInputStream in class CommPort
Returns:
InputStream object that can be used to read from the port
Throws:
java.io.IOException - - if an I/O error occurred

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Returns an output stream. This is the only way to send data to the c ommunications port. If the port is unidirectional and doesn't support sending data, then getOutputStream returns null.

Specified by:
getOutputStream in class CommPort
Returns:
OutputStream object that can be used to write to the port
Throws:
java.io.IOException - - if an I/O error occurred

enableReceiveThreshold

public void enableReceiveThreshold(int thresh)
                            throws UnsupportedCommOperationException
Description copied from class: CommPort
Enables receive threshold, if this feature is supported by the driver. When the receive threshold condition becomes true, a read from the input stream for this port will return immediately. An application can determine whether the driver supports this feature by first calling the enableReceiveThreshold method and then calling the isReceiveThresholdEnabled method. If isReceiveThresholdEnabled still returns false, then receive threshold is not supported by the driver. If the driver does not implement this feature, it will return from blocking reads at an appropriate time. See getInputStream for description of exact behaviour.

Specified by:
enableReceiveThreshold in class CommPort
Parameters:
thresh - - when this many bytes are in the input buffer, return immediately from read.
Throws:
UnsupportedCommOperationException - - is thrown if receive threshold is not supported by the underlying driver.

disableReceiveThreshold

public void disableReceiveThreshold()
Description copied from class: CommPort
Disables receive threshold.

Specified by:
disableReceiveThreshold in class CommPort

isReceiveThresholdEnabled

public boolean isReceiveThresholdEnabled()
Description copied from class: CommPort
Checks if receive threshold is enabled.

Specified by:
isReceiveThresholdEnabled in class CommPort
Returns:
boolean true if the driver supports receive threshold.

getReceiveThreshold

public int getReceiveThreshold()
Description copied from class: CommPort
Gets the integer value of the receive threshold. If the receive threshold is disabled or not supported by the driver, then the value returned is meaningless.

Specified by:
getReceiveThreshold in class CommPort
Returns:
number of bytes for receive threshold

enableReceiveTimeout

public void enableReceiveTimeout(int rcvTimeout)
                          throws UnsupportedCommOperationException
Description copied from class: CommPort
Enables receive timeout, if this feature is supported by the driver. When the receive timeout condition becomes true, a read from the input stream for this port will return immediately. An application can determine whether the driver supports this feature by first calling the enableReceiveTimeout method and then calling the isReceiveTimeout method. If isReceiveTimeout still returns false, then receive timeout is not supported by the driver. See getInputStream for description of exact behaviour.

Specified by:
enableReceiveTimeout in class CommPort
Parameters:
rcvTimeout - - when this many milliseconds have elapsed, return immediately from read, regardless of bytes in input buffer.
Throws:
UnsupportedCommOperationException - - is thrown if receive timeout is not supported by the underlying driver.

disableReceiveTimeout

public void disableReceiveTimeout()
Description copied from class: CommPort
Disables receive timeout.

Specified by:
disableReceiveTimeout in class CommPort

isReceiveTimeoutEnabled

public boolean isReceiveTimeoutEnabled()
Description copied from class: CommPort
Checks if receive timeout is enabled.

Specified by:
isReceiveTimeoutEnabled in class CommPort
Returns:
boolean true if the driver supports receive timeout.

getReceiveTimeout

public int getReceiveTimeout()
Description copied from class: CommPort
Gets the integer value of the receive timeout. If the receive timeout is disabled or not supported by the driver, then the value returned is meaningless.

Specified by:
getReceiveTimeout in class CommPort
Returns:
number of milliseconds in receive timeout

enableReceiveFraming

public void enableReceiveFraming(int framingByte)
                          throws UnsupportedCommOperationException
Description copied from class: CommPort
Enables receive framing, if this feature is supported by the driver. When the receive framing condition becomes true, a read from the input stream for this port will return immediately. enableReceiveFraming is an advisory method which the driver may not implement. By default, receive framing is not enabled. An application can determine whether the driver supports this feature by first calling the enableReceiveFraming method and then calling the isReceiveFramingEnabled method. If isReceiveFramingEnabled still returns false, then receive framing is not supported by the driver. Note: As implemented in this method, framing is not related to bit-level framing at the hardware level, and is not associated with data errors.

Specified by:
enableReceiveFraming in class CommPort
Parameters:
framingByte - - this byte in the input stream suggests the end of the received frame. Blocked reads will return immediately. Only the low 8 bits of framingByte are used while the upper 24 bits are masked off. A value outside the range of 0-255 will be converted to the value of its lowest 8 bits.
Throws:
UnsupportedCommOperationException - - is thrown if receive timeout is not supported by the underlying driver.

disableReceiveFraming

public void disableReceiveFraming()
Description copied from class: CommPort
Disables receive framing.

Specified by:
disableReceiveFraming in class CommPort

isReceiveFramingEnabled

public boolean isReceiveFramingEnabled()
Description copied from class: CommPort
Checks if receive framing is enabled.

Specified by:
isReceiveFramingEnabled in class CommPort
Returns:
boolean true if the driver supports receive framing.

getReceiveFramingByte

public int getReceiveFramingByte()
Description copied from class: CommPort
Gets the current byte used for receive framing. If the receive framing is disabled or not supported by the driver, then the value returned is meaningless. The return value of getReceiveFramingByte is an integer, the low 8 bits of which represent the current byte used for receive framing. Note: As implemented in this method, framing is not related to bit-level framing at the hardware level, and is not associated with data errors.

Specified by:
getReceiveFramingByte in class CommPort
Returns:
integer current byte used for receive framing

setInputBufferSize

public void setInputBufferSize(int size)
Description copied from class: CommPort
Sets the input buffer size. Note that this is advisory and memory availability may determine the ultimate buffer size used by the driver.

Specified by:
setInputBufferSize in class CommPort
Parameters:
size - - size of the input buffer

getInputBufferSize

public int getInputBufferSize()
Description copied from class: CommPort
Gets the input buffer size. Note that this method is advisory and the underlying OS may choose not to report correct values for the buffer size.

Specified by:
getInputBufferSize in class CommPort
Returns:
input buffer size currently in use

setOutputBufferSize

public void setOutputBufferSize(int size)
Description copied from class: CommPort
Sets the output buffer size. Note that this is advisory and memory availability may determine the ultimate buffer size used by the driver.

Specified by:
setOutputBufferSize in class CommPort
Parameters:
size - - size of the output buffer

getOutputBufferSize

public int getOutputBufferSize()
Description copied from class: CommPort
Gets the output buffer size. Note that this method is advisory and the underlying OS may choose not to report correct values for the buffer size.

Specified by:
getOutputBufferSize in class CommPort
Returns:
output buffer size currently in use