public class SerialPort
extends java.lang.Object
To obtain an instance of the serial port and open it you have to use one
of the following methods: SerialPortManager.openSerialPort(String)
,
SerialPortManager.openSerialPort(String, int, int, int, int, int, int)
,
or SerialPortManager.openSerialPort(String, SerialPortParameters)
.
Modifier and Type | Field | Description |
---|---|---|
static int |
DATABITS_5 |
5 data bits: .
|
static int |
DATABITS_6 |
6 data bits: .
|
static int |
DATABITS_7 |
7 data bits: .
|
static int |
DATABITS_8 |
8 data bits: .
|
static int |
DEFAULT_READ_TIMEOUT |
Default read timeout: milliseconds.
|
static int |
FLOWCONTROL_NONE |
No flow control: .
|
static int |
FLOWCONTROL_RTSCTS_IN |
Hardware flow control (input): .
|
static int |
FLOWCONTROL_RTSCTS_OUT |
Hardware flow control (output): .
|
static int |
FLOWCONTROL_XONXOFF_IN |
Xon/Xoff flow control (input): .
|
static int |
FLOWCONTROL_XONXOFF_OUT |
Xon/Xoff flow control (output): .
|
static int |
PARITY_EVEN |
Parity even: .
|
static int |
PARITY_MARK |
Parity mark: .
|
static int |
PARITY_NONE |
Parity none: .
|
static int |
PARITY_ODD |
Parity odd: .
|
static int |
PARITY_SPACE |
Parity space: .
|
static int |
STOPBITS_1 |
1 stop bit: .
|
static int |
STOPBITS_1_5 |
1.5 stop bits: .
|
static int |
STOPBITS_2 |
2 stop bits: .
|
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closes the communications port.
|
void |
disableReceiveThreshold() |
Disables receive threshold.
|
void |
disableReceiveTimeout() |
Disables receive timeout.
|
void |
enableReceiveThreshold(int thresh) |
Enables receive threshold, if this feature is supported by the driver.
|
void |
enableReceiveTimeout(int time) |
Enables receive timeout, if this feature is supported by the driver.
|
int |
getBaudBase() |
Returns the baud base value.
|
int |
getBaudRate() |
Returns the configured baud rate.
|
int |
getDataBits() |
Returns the configured data bits.
|
int |
getDivisor() |
Returns the divisor value.
|
byte |
getEndOfInputChar() |
Returns the end of input character.
|
int |
getFlowControl() |
Returns the configured flow control.
|
int |
getInputBufferSize() |
Returns the input buffer size.
|
java.io.InputStream |
getInputStream() |
Returns the connection interface input stream to read data from.
|
java.lang.String |
getName() |
Returns the serial port name.
|
int |
getOutputBufferSize() |
Returns the output buffer size.
|
java.io.OutputStream |
getOutputStream() |
Returns the connection interface output stream to write data to.
|
int |
getParity() |
Returns the configured parity.
|
byte |
getParityErrorChar() |
Returns the parity error character.
|
int |
getReceiveThreshold() |
Returns the integer value of the receive threshold.
|
int |
getReceiveTimeout() |
Returns the integer value of the receive timeout.
|
int |
getStopBits() |
Returns the configured stop bits.
|
boolean |
isCD() |
Returns the state of the CD line.
|
boolean |
isCTS() |
Returns the state of the CTS line.
|
boolean |
isDSR() |
Returns the state of the DSR line.
|
boolean |
isDTR() |
Returns the state of the DTR line.
|
boolean |
isReceiveThresholdEnabled() |
Checks if receive threshold is enabled.
|
boolean |
isReceiveTimeoutEnabled() |
Checks if receive timeout is enabled.
|
boolean |
isRI() |
Returns the state of the RI line.
|
boolean |
isRTS() |
Returns the state of the RTS line.
|
void |
notifyOnBreakInterrupt(boolean enable) |
Sets whether the listener should be notified on break interrupt.
|
void |
notifyOnCarrierDetect(boolean enable) |
Sets whether the listener should be notified on carrier detect.
|
void |
notifyOnCTS(boolean enable) |
Sets whether the listener should be notified on CTS.
|
void |
notifyOnDataAvailable(boolean enable) |
Sets whether the listener should be notified on data available.
|
void |
notifyOnDSR(boolean enable) |
Sets whether the listener should be notified on DSR.
|
void |
notifyOnFramingError(boolean enable) |
Sets whether the listener should be notified on framing error.
|
void |
notifyOnOutputEmpty(boolean enable) |
Sets whether the listener should be notified on output empty.
|
void |
notifyOnOverrunError(boolean enable) |
Sets whether the listener should be notified on overrun error.
|
void |
notifyOnParityError(boolean enable) |
Sets whether the listener should be notified on parity error.
|
void |
notifyOnRingIndicator(boolean enable) |
Sets whether the listener should be notified on ring indicator.
|
int |
readData(byte[] buffer,
int numBytes) |
Attempts to read the given amount of bytes from the serial port.
|
void |
registerEventListener(ISerialPortEventListener listener) |
Registers the given listener to be notified when a serial port event
occurs.
|
void |
sendBreak(int duration) |
Sends a break signal to the serial port with the given duration
(in milliseconds).
|
boolean |
setBaudBase(int baudBase) |
Sets the baud base value.
|
boolean |
setDivisor(int divisor) |
Sets the divisor value.
|
void |
setDTR(boolean state) |
Sets the state of the DTR.
|
boolean |
setEndOfInputChar(byte b) |
Sets the end of input character.
|
void |
setInputBufferSize(int size) |
Sets the input buffer size.
|
void |
setOutputBufferSize(int size) |
Sets the output buffer size.
|
boolean |
setParityErrorChar(byte b) |
Sets the parity error character.
|
void |
setPortParameters(int baudRate,
int dataBits,
int stopBits,
int parity,
int flowControl) |
Sets the new parameters of the serial port.
|
void |
setRTS(boolean state) |
Sets the state of the RTS line.
|
java.lang.String |
toString() |
|
void |
unregisterEventListener() |
Unregisters the configured serial port event listener.
|
void |
writeData(byte[] data,
int numBytes) |
Attempts to write the given amount of bytes to the serial port.
|
public static final int DATABITS_5
public static final int DATABITS_6
public static final int DATABITS_7
public static final int DATABITS_8
public static final int PARITY_NONE
public static final int PARITY_ODD
public static final int PARITY_EVEN
public static final int PARITY_MARK
public static final int PARITY_SPACE
public static final int STOPBITS_1
public static final int STOPBITS_2
public static final int STOPBITS_1_5
public static final int FLOWCONTROL_NONE
public static final int FLOWCONTROL_RTSCTS_IN
public static final int FLOWCONTROL_RTSCTS_OUT
public static final int FLOWCONTROL_XONXOFF_IN
public static final int FLOWCONTROL_XONXOFF_OUT
public static final int DEFAULT_READ_TIMEOUT
public void registerEventListener(ISerialPortEventListener listener) throws java.util.TooManyListenersException
Note that you can only register one listener.
listener
- Serial port event listener.java.util.TooManyListenersException
- If you register more than one listener.unregisterEventListener()
,
ISerialPortEventListener
public void unregisterEventListener()
public void setPortParameters(int baudRate, int dataBits, int stopBits, int parity, int flowControl) throws UnsupportedCommOperationException
baudRate
- Baud rate.dataBits
- Data bits, one of DATABITS_5
, DATABITS_6
,
DATABITS_7
, and DATABITS_8
.stopBits
- Stop bits, one of STOPBITS_1
, STOPBITS_2
,
and STOPBITS_1_5
.parity
- Parity, one of PARITY_NONE
, PARITY_ODD
,
PARITY_EVEN
, PARITY_MARK
, and
PARITY_SPACE
.flowControl
- Flow control, one of FLOWCONTROL_NONE
,
FLOWCONTROL_RTSCTS_IN
,
FLOWCONTROL_RTSCTS_OUT
,
FLOWCONTROL_XONXOFF_IN
, and
FLOWCONTROL_XONXOFF_OUT
. Use an "|" to combine
them, e.g. FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT
.UnsupportedCommOperationException
- If the operation is not
supported by the driver.DATABITS_5
,
DATABITS_6
,
DATABITS_7
,
DATABITS_8
,
FLOWCONTROL_NONE
,
FLOWCONTROL_RTSCTS_IN
,
FLOWCONTROL_RTSCTS_OUT
,
FLOWCONTROL_XONXOFF_IN
,
FLOWCONTROL_XONXOFF_OUT
,
PARITY_EVEN
,
PARITY_MARK
,
PARITY_NONE
,
PARITY_ODD
,
PARITY_SPACE
,
STOPBITS_1
,
STOPBITS_1_5
,
STOPBITS_2
,
getBaudRate()
,
getDataBits()
,
getFlowControl()
,
getParity()
,
getStopBits()
public int getBaudRate()
setPortParameters(int, int, int, int, int)
public int getDataBits()
setPortParameters(int, int, int, int, int)
public int getStopBits()
setPortParameters(int, int, int, int, int)
public int getParity()
setPortParameters(int, int, int, int, int)
public int getFlowControl()
setPortParameters(int, int, int, int, int)
public boolean isDTR()
true
if the line is high, false
otherwise.isCD()
,
isCTS()
,
isDSR()
,
isRI()
,
isRTS()
,
setDTR(boolean)
,
setRTS(boolean)
public void setDTR(boolean state)
public boolean isRTS()
true
if the line is high, false
otherwise.isCD()
,
isCTS()
,
isDSR()
,
isDTR()
,
isRI()
,
setDTR(boolean)
,
setRTS(boolean)
public void setRTS(boolean state)
public boolean isCTS()
true
if the line is high, false
otherwise.isCD()
,
isDSR()
,
isDTR()
,
isRI()
,
isRTS()
,
setDTR(boolean)
,
setRTS(boolean)
public boolean isDSR()
true
if the line is high, false
otherwise.isCD()
,
isCTS()
,
isDTR()
,
isRI()
,
isRTS()
,
setDTR(boolean)
,
setRTS(boolean)
public boolean isCD()
true
if the line is high, false
otherwise.isCTS()
,
isDSR()
,
isDTR()
,
isRI()
,
isRTS()
,
setDTR(boolean)
,
setRTS(boolean)
public boolean isRI()
true
if the line is high, false
otherwise.isCD()
,
isCTS()
,
isDSR()
,
isDTR()
,
isRTS()
,
setDTR(boolean)
,
setRTS(boolean)
public void sendBreak(int duration)
duration
- Duration of the break signal in milliseconds.public void notifyOnDataAvailable(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnOutputEmpty(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnCTS(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnDSR(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnRingIndicator(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnCarrierDetect(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnOverrunError(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnParityError(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnFramingError(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public void notifyOnBreakInterrupt(boolean enable)
enable
- true
to notify.registerEventListener(ISerialPortEventListener)
,
ISerialPortEventListener
,
SerialPortEvent
public byte getParityErrorChar() throws UnsupportedCommOperationException
UnsupportedCommOperationException
- If the operation is not
supported by the driver.setParityErrorChar(byte)
public boolean setParityErrorChar(byte b) throws UnsupportedCommOperationException
b
- Parity Error Character.true
on success.UnsupportedCommOperationException
- If the operation is not
supported by the driver.getParityErrorChar()
public byte getEndOfInputChar() throws UnsupportedCommOperationException
UnsupportedCommOperationException
- If the operation is not
supported by the driver.setEndOfInputChar(byte)
public boolean setEndOfInputChar(byte b) throws UnsupportedCommOperationException
b
- End Of Input Character.true
on success.UnsupportedCommOperationException
- If the operation is not
supported by the driver.getEndOfInputChar()
public boolean setBaudBase(int baudBase) throws UnsupportedCommOperationException, java.io.IOException
baudBase
- The clock frequency divided by 16. Default
BaudBase is 115200.true
on success.java.io.IOException
- If an I/O error occurs.UnsupportedCommOperationException
- If the operation is not
supported by the driver.getBaudBase()
public int getBaudBase() throws UnsupportedCommOperationException, java.io.IOException
java.io.IOException
- If an I/O error occurs.UnsupportedCommOperationException
- If the operation is not
supported by the driver.setBaudBase(int)
public boolean setDivisor(int divisor) throws UnsupportedCommOperationException, java.io.IOException
divisor
- Divisor.true
on success.java.io.IOException
- If an I/O error occurs.UnsupportedCommOperationException
- If the operation is not
supported by the driver.getDivisor()
public int getDivisor() throws UnsupportedCommOperationException, java.io.IOException
java.io.IOException
- If an I/O error occurs.UnsupportedCommOperationException
- If the operation is not
supported by the driver.setDivisor(int)
public void disableReceiveTimeout()
public void enableReceiveTimeout(int time) throws UnsupportedCommOperationException
read
from the
input stream for this port will return immediately.
This is an advisory method which the driver may not implement. By default, receive timeout is not enabled.
An application can determine whether the driver supports this feature
by first calling the enableReceiveTimeout(int)
method and then
calling the isReceiveTimeoutEnabled()
method. If it returns
false
, then receive timeout is not supported by the driver.
time
- When this many milliseconds have elapsed, return immediately
from read
, regardless of bytes in input buffer.UnsupportedCommOperationException
- If the operation is not
supported by the driver.disableReceiveTimeout()
,
getReceiveTimeout()
,
isReceiveTimeoutEnabled()
public boolean isReceiveTimeoutEnabled()
An application can determine whether the driver supports this feature
by first calling the enableReceiveTimeout(int)
method and then
calling the isReceiveTimeoutEnabled()
method. If it returns
false
, then receive timeout is not supported by the driver.
true
if the receive timeout is enabled, false
otherwise.disableReceiveTimeout()
,
enableReceiveTimeout(int)
,
getReceiveTimeout()
public int getReceiveTimeout()
disableReceiveTimeout()
,
enableReceiveTimeout(int)
,
isReceiveTimeoutEnabled()
public void enableReceiveThreshold(int thresh) throws UnsupportedCommOperationException
read
from
the input stream for this port will return immediately.
This is an advisory method which the driver may not implement. By default, receive threshold is not enabled.
An application can determine whether the driver supports this feature
by first calling the enableReceiveThreshold(int)
method and then
calling the isReceiveThresholdEnabled()
method. If it returns
false
, then receive threshold is not supported by the driver.
thresh
- When this many bytes are in the input buffer, return
immediately from read
.UnsupportedCommOperationException
- If the operation is not
supported by the driver.disableReceiveThreshold()
,
getReceiveThreshold()
,
isReceiveThresholdEnabled()
public void disableReceiveThreshold()
public int getReceiveThreshold()
disableReceiveThreshold()
,
enableReceiveThreshold(int)
,
isReceiveThresholdEnabled()
public boolean isReceiveThresholdEnabled()
An application can determine whether the driver supports this feature
by first calling the enableReceiveThreshold(int)
method and then
calling the isReceiveThresholdEnabled()
method. If it returns
false
, then receive threshold is not supported by the driver.
true
if the receive threshold is enabled, false
otherwise.disableReceiveThreshold()
,
enableReceiveThreshold(int)
,
getReceiveThreshold()
public void setInputBufferSize(int size)
size
- Size of the input buffer in bytes.getInputBufferSize()
public int getInputBufferSize()
setInputBufferSize(int)
public void setOutputBufferSize(int size)
size
- Size of the output buffer in bytes.getOutputBufferSize()
public int getOutputBufferSize()
setOutputBufferSize(int)
public void close()
The application must call this method when it is done with the port.
public java.io.InputStream getInputStream() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.getOutputStream()
,
InputStream
public java.io.OutputStream getOutputStream() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.getInputStream()
,
OutputStream
public java.lang.String getName()
public int readData(byte[] buffer, int numBytes) throws java.io.IOException
buffer
- Byte array to store read data.numBytes
- Number of bytes to read.java.lang.IllegalArgumentException
- If numBytes < 1
or
if numBytes > buffer.length
.java.io.IOException
- If an I/O error occurs.writeData(byte[], int)
public void writeData(byte[] data, int numBytes) throws java.io.IOException
data
- Data to write.numBytes
- Number of bytes to write.java.lang.IllegalArgumentException
- If numBytes < 1
or
if numBytes > data.length
.java.io.IOException
- If an I/O error occurs.readData(byte[], int)
public java.lang.String toString()
toString
in class java.lang.Object