Class SerialPort
- java.lang.Object
-
- purejavacomm.CommPort
-
- purejavacomm.SerialPort
-
- Direct Known Subclasses:
PureJavaSerialPort
public abstract class SerialPort extends CommPort
-
-
Field Summary
Fields Modifier and Type Field Description static intDATABITS_55 data bit format.static intDATABITS_66 data bit format.static intDATABITS_77 data bit format.static intDATABITS_88 data bit format.static intFLOWCONTROL_NONEFlow control off.static intFLOWCONTROL_RTSCTS_INRTS/CTS flow control on input.static intFLOWCONTROL_RTSCTS_OUTRTS/CTS flow control on output.static intFLOWCONTROL_XONXOFF_INXON/XOFF flow control on input.static intFLOWCONTROL_XONXOFF_OUTXON/XOFF flow control on output.static intPARITY_EVENEven parity scheme.static intPARITY_MARKMark parity scheme.static intPARITY_NONENo parity bit.static intPARITY_ODDOdd parity scheme.static intPARITY_SPACESpace parity scheme.static intSTOPBITS_1One stop bit.static intSTOPBITS_1_5One and 1/2 stop bits.static intSTOPBITS_2Two stop bits.
-
Constructor Summary
Constructors Constructor Description SerialPort()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract voidaddEventListener(SerialPortEventListener listener)Registers aSerialPortEventListenerobject to listen forSerialEvents.abstract intgetBaudRate()Returns the currently configured baud rate.abstract intgetDataBits()Returns the currently configured number of data bits.abstract intgetFlowControlMode()Returns the currently configured flow control mode.abstract InputStreamgetInputStream()Returns an input stream.abstract intgetParity()Returns the currently configured parity setting.abstract intgetStopBits()Returns the currently defined stop bits.abstract booleanisCD()Returns the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.abstract booleanisCTS()Returns the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.abstract booleanisDSR()Returns the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.abstract booleanisDTR()Returns the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.abstract booleanisRI()Returns the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.abstract booleanisRTS()Returns the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.abstract voidnotifyOnBreakInterrupt(boolean enable)Expresses interest in receiving notification when there is a break interrupt on the line.abstract voidnotifyOnCarrierDetect(boolean enable)Expresses interest in receiving notification when the CD (Carrier Detect) bit changes.abstract voidnotifyOnCTS(boolean enable)Expresses interest in receiving notification when the CTS (Clear To Send) bit changes.abstract voidnotifyOnDataAvailable(boolean enable)Expresses interest in receiving notification when input data is available.abstract voidnotifyOnDSR(boolean enable)Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes.abstract voidnotifyOnFramingError(boolean enable)Expresses interest in receiving notification when there is a framing error.abstract voidnotifyOnOutputEmpty(boolean enable)Expresses interest in receiving notification when the output buffer is empty.abstract voidnotifyOnOverrunError(boolean enable)Expresses interest in receiving notification when there is an overrun error.abstract voidnotifyOnParityError(boolean enable)Expresses interest in receiving notification when there is a parity error.abstract voidnotifyOnRingIndicator(boolean enable)Expresses interest in receiving notification when the RI (Ring Indicator) bit changes.abstract voidremoveEventListener()Deregisters event listener registered usingaddEventListener.abstract voidsendBreak(int duration)Sends a break ofdurationmilliseconds duration.abstract voidsetDTR(boolean state)Sets or clears the DTR (Data Terminal Ready) signal, if supported by the underlying implementation.abstract voidsetFlowControlMode(int flowcontrol)Sets the flow control mode.abstract voidsetRTS(boolean state)Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.abstract voidsetSerialPortParams(int baudRate, int dataBits, int stopBits, int parity)Sets the serial port parameters.-
Methods inherited from class purejavacomm.CommPort
close, disableReceiveFraming, disableReceiveThreshold, disableReceiveTimeout, enableReceiveFraming, enableReceiveThreshold, enableReceiveTimeout, getInputBufferSize, getName, getOutputBufferSize, getOutputStream, getReceiveFramingByte, getReceiveThreshold, getReceiveTimeout, isReceiveFramingEnabled, isReceiveThresholdEnabled, isReceiveTimeoutEnabled, setInputBufferSize, setOutputBufferSize
-
-
-
-
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
-
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
-
STOPBITS_1
public static final int STOPBITS_1
One stop bit.- See Also:
- Constant Field Values
-
STOPBITS_2
public static final int STOPBITS_2
Two stop bits.- See Also:
- Constant Field Values
-
STOPBITS_1_5
public static final int STOPBITS_1_5
One and 1/2 stop bits. 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
-
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
-
-
Method Detail
-
addEventListener
public abstract void addEventListener(SerialPortEventListener listener) throws TooManyListenersException
Registers aSerialPortEventListenerobject to listen forSerialEvents.- Parameters:
listener-- Throws:
TooManyListenersException
-
getBaudRate
public abstract int getBaudRate()
Returns the currently configured baud rate.- Returns:
- The currently configured baud rate.
-
getDataBits
public abstract int getDataBits()
Returns the currently configured number of data bits.- Returns:
- The currently configured number of data bits.
-
getFlowControlMode
public abstract int getFlowControlMode()
Returns the currently configured flow control mode.- Returns:
- The currently configured flow control mode.
-
getParity
public abstract int getParity()
Returns the currently configured parity setting.- Returns:
- The currently configured parity setting.
-
getStopBits
public abstract int getStopBits()
Returns the currently defined stop bits.- Returns:
- The currently defined stop bits.
-
isCD
public abstract boolean isCD()
Returns the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.- Returns:
- The state of the CD (Carrier Detect) bit.
-
isCTS
public abstract boolean isCTS()
Returns the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.- Returns:
- The state of the CTS (Clear To Send) bit.
-
isDSR
public abstract boolean isDSR()
Returns the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.- Returns:
- The state of the DSR (Data Set Ready) bit.
-
isDTR
public abstract boolean isDTR()
Returns the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.- Returns:
- The state of the DTR (Data Terminal Ready) bit.
-
isRI
public abstract boolean isRI()
Returns the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.- Returns:
- The state of the RI (Ring Indicator) bit.
-
isRTS
public abstract boolean isRTS()
Returns the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.- Returns:
- The state of the RTS (Request To Send) bit.
-
notifyOnBreakInterrupt
public abstract void notifyOnBreakInterrupt(boolean enable)
Expresses interest in receiving notification when there is a break interrupt on the line.- Parameters:
enable-
-
notifyOnCarrierDetect
public abstract void notifyOnCarrierDetect(boolean enable)
Expresses interest in receiving notification when the CD (Carrier Detect) bit changes.- Parameters:
enable-
-
notifyOnCTS
public abstract void notifyOnCTS(boolean enable)
Expresses interest in receiving notification when the CTS (Clear To Send) bit changes.- Parameters:
enable-
-
notifyOnDataAvailable
public abstract void notifyOnDataAvailable(boolean enable)
Expresses interest in receiving notification when input data is available.- Parameters:
enable-
-
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-
-
notifyOnFramingError
public abstract void notifyOnFramingError(boolean enable)
Expresses interest in receiving notification when there is a framing error.- Parameters:
enable-
-
notifyOnOutputEmpty
public abstract void notifyOnOutputEmpty(boolean enable)
Expresses interest in receiving notification when the output buffer is empty.- Parameters:
enable-
-
notifyOnOverrunError
public abstract void notifyOnOverrunError(boolean enable)
Expresses interest in receiving notification when there is an overrun error.- Parameters:
enable-
-
notifyOnParityError
public abstract void notifyOnParityError(boolean enable)
Expresses interest in receiving notification when there is a parity error.- Parameters:
enable-
-
notifyOnRingIndicator
public abstract void notifyOnRingIndicator(boolean enable)
Expresses interest in receiving notification when the RI (Ring Indicator) bit changes.- Parameters:
enable-
-
removeEventListener
public abstract void removeEventListener()
Deregisters event listener registered usingaddEventListener.This is done automatically when the port is closed.
-
sendBreak
public abstract void sendBreak(int duration)
Sends a break ofdurationmilliseconds duration.- Parameters:
duration- The break duration in milliseconds.
-
setDTR
public abstract void setDTR(boolean state)
Sets or clears the DTR (Data Terminal Ready) signal, if supported by the underlying implementation.- Parameters:
state-
-
setFlowControlMode
public abstract void setFlowControlMode(int flowcontrol) throws UnsupportedCommOperationExceptionSets the flow control mode.- Parameters:
flowcontrol-- Throws:
UnsupportedCommOperationException
-
setRTS
public abstract void setRTS(boolean state)
Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.- Parameters:
state-
-
setSerialPortParams
public abstract void setSerialPortParams(int baudRate, int dataBits, int stopBits, int parity) throws UnsupportedCommOperationExceptionSets the serial port parameters.Default: 9600 baud, 8 data bits, 1 stop bit, no parity.
- Parameters:
baudRate-dataBits-stopBits-parity-- Throws:
UnsupportedCommOperationException
-
getInputStream
public abstract InputStream getInputStream() throws 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. The read behavior of the input stream returned by getInputStream depends on combination of the threshold and timeout values. The behaviors are described in the table below.threshold timeout read buffer size read behaviour state value state value disabled - disabled - n bytes block until minimum one byte of data is available enabled m bytes disabled - n bytes block until min(m,n) bytes are available disabled - enabled x msec n bytes block for x msec or until any data is available enabled m bytes enabled x msec n bytes block for x msec or until min(m,n) bytes are available Framing errors may cause the Timeout and Threshold trigger early and to complete the read prematurely without raising an exception.
Enabling the Timeout OR Threshold with a value a zero is a special case. This causes the underlying driver to poll for incoming data instead being event driven. Otherwise, the behaviour is identical to having both the Timeout and Threshold disabled. Returns: InputStream object that can be used to read from the port Throws: java.io.IOException - if an I/O error occurred.
Timeout is interpreted as inter character timeout, in other words the timeout will not occur as long as the pause before the first character or between characters is shorter that the timeout value.
- Specified by:
getInputStreamin classCommPort- Returns:
- An input stream, or
nullif the port is unidirectional and doesn't support receiving data. - Throws:
IOException
-
-