public class PureJavaSerialPort extends SerialPort
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| Modifier and Type | Method and Description |
|---|---|
void |
addEventListener(SerialPortEventListener eventListener)
Registers a
SerialPortEventListener object to listen for
SerialEvents. |
void |
close()
Closes the communications port.
|
void |
disableReceiveFraming()
Disables receive framing.
|
void |
disableReceiveThreshold()
Disables receive threshold.
|
void |
disableReceiveTimeout()
Disables receive timeout.
|
void |
enableReceiveFraming(int arg0)
Enables receive framing.
|
void |
enableReceiveThreshold(int value)
Enables receive threshold.
|
void |
enableReceiveTimeout(int value)
Enables receive timeout.
|
int |
getBaudRate()
Returns the currently configured baud rate.
|
int |
getDataBits()
Returns the currently configured number of data bits.
|
int |
getFlowControlMode()
Returns the currently configured flow control mode.
|
int |
getInputBufferSize()
Returns the input buffer size in bytes.
|
InputStream |
getInputStream()
Returns an input stream.
|
int |
getNativeFileDescriptor()
Gets the native file descriptor used by this port.
|
int |
getOutputBufferSize()
Returns the output buffer size in bytes.
|
OutputStream |
getOutputStream()
Returns an output stream.
|
int |
getParity()
Returns the currently configured parity setting.
|
int |
getReceiveFramingByte()
Returns the current byte used for receive framing.
|
int |
getReceiveThreshold()
Returns the integer value of the receive threshold.
|
int |
getReceiveTimeout()
Returns the integer value of the receive timeout.
|
int |
getStopBits()
Returns the currently defined stop bits.
|
boolean |
isCD()
Returns the state of the CD (Carrier Detect) bit in the UART, if
supported by the underlying implementation.
|
boolean |
isCTS()
Returns the state of the CTS (Clear To Send) bit in the UART, if
supported by the underlying implementation.
|
boolean |
isDSR()
Returns the state of the DSR (Data Set Ready) bit in the UART, if
supported by the underlying implementation.
|
boolean |
isDTR()
Returns the state of the DTR (Data Terminal Ready) bit in the UART, if
supported by the underlying implementation.
|
boolean |
isInternalThreadRunning()
This is not part of the PureJavaComm API, this is purely for testing, do
not depend on this
|
boolean |
isReceiveFramingEnabled()
Returns
true if receive framing is enabled. |
boolean |
isReceiveThresholdEnabled()
Returns
true if receive threshold is enabled. |
boolean |
isReceiveTimeoutEnabled()
Returns
true if receive timeout is enabled. |
boolean |
isRI()
Returns the state of the RI (Ring Indicator) bit in the UART, if
supported by the underlying implementation.
|
boolean |
isRTS()
Returns the state of the RTS (Request To Send) bit in the UART, if
supported by the underlying implementation.
|
void |
notifyOnBreakInterrupt(boolean x)
Expresses interest in receiving notification when there is a break
interrupt on the line.
|
void |
notifyOnCarrierDetect(boolean x)
Expresses interest in receiving notification when the CD (Carrier Detect)
bit changes.
|
void |
notifyOnCTS(boolean x)
Expresses interest in receiving notification when the CTS (Clear To Send)
bit changes.
|
void |
notifyOnDataAvailable(boolean x)
Expresses interest in receiving notification when input data is
available.
|
void |
notifyOnDSR(boolean x)
Expresses interest in receiving notification when the DSR (Data Set
Ready) bit changes.
|
void |
notifyOnFramingError(boolean x)
Expresses interest in receiving notification when there is a framing
error.
|
void |
notifyOnOutputEmpty(boolean x)
Expresses interest in receiving notification when the output buffer is
empty.
|
void |
notifyOnOverrunError(boolean x)
Expresses interest in receiving notification when there is an overrun
error.
|
void |
notifyOnParityError(boolean x)
Expresses interest in receiving notification when there is a parity
error.
|
void |
notifyOnRingIndicator(boolean x)
Expresses interest in receiving notification when the RI (Ring Indicator)
bit changes.
|
void |
removeEventListener()
Deregisters event listener registered using
addEventListener
. |
void |
sendBreak(int duration)
Sends a break of
duration milliseconds duration. |
void |
setDTR(boolean x)
Sets or clears the DTR (Data Terminal Ready) signal, if supported by the
underlying implementation.
|
void |
setFlowControlMode(int mode)
Sets the flow control mode.
|
void |
setInputBufferSize(int arg0)
Sets the input buffer size.
|
void |
setOutputBufferSize(int arg0)
Sets the output buffer size.
|
void |
setRTS(boolean x)
Sets or clears the RTS (Request To Send) bit in the UART, if supported by
the underlying implementation.
|
void |
setSerialPortParams(int baudRate,
int dataBits,
int stopBits,
int parity)
Sets the serial port parameters.
|
public void addEventListener(SerialPortEventListener eventListener) throws TooManyListenersException
SerialPortSerialPortEventListener object to listen for
SerialEvents.addEventListener in class SerialPorteventListener - the listenerTooManyListenersException - the too many listeners exceptionpublic int getBaudRate()
SerialPortgetBaudRate in class SerialPortpublic int getDataBits()
SerialPortgetDataBits in class SerialPortpublic int getFlowControlMode()
SerialPortgetFlowControlMode in class SerialPortpublic int getParity()
SerialPortgetParity in class SerialPortpublic int getStopBits()
SerialPortgetStopBits in class SerialPortpublic boolean isCD()
SerialPortisCD in class SerialPortpublic boolean isCTS()
SerialPortisCTS in class SerialPortpublic boolean isDSR()
SerialPortisDSR in class SerialPortpublic boolean isDTR()
SerialPortisDTR in class SerialPortpublic boolean isRI()
SerialPortisRI in class SerialPortpublic boolean isRTS()
SerialPortisRTS in class SerialPortpublic void notifyOnBreakInterrupt(boolean x)
SerialPortnotifyOnBreakInterrupt in class SerialPortx - the enablepublic void notifyOnCTS(boolean x)
SerialPortnotifyOnCTS in class SerialPortx - the enablepublic void notifyOnCarrierDetect(boolean x)
SerialPortnotifyOnCarrierDetect in class SerialPortx - the enablepublic void notifyOnDSR(boolean x)
SerialPortThis notification is hardware dependent and may not be supported by all implementations.
notifyOnDSR in class SerialPortx - the enablepublic void notifyOnDataAvailable(boolean x)
SerialPortnotifyOnDataAvailable in class SerialPortx - the enablepublic void notifyOnFramingError(boolean x)
SerialPortnotifyOnFramingError in class SerialPortx - the enablepublic void notifyOnOutputEmpty(boolean x)
SerialPortnotifyOnOutputEmpty in class SerialPortx - the enablepublic void notifyOnOverrunError(boolean x)
SerialPortnotifyOnOverrunError in class SerialPortx - the enablepublic void notifyOnParityError(boolean x)
SerialPortnotifyOnParityError in class SerialPortx - the enablepublic void notifyOnRingIndicator(boolean x)
SerialPortnotifyOnRingIndicator in class SerialPortx - the enablepublic void removeEventListener()
SerialPortaddEventListener
.
This is done automatically when the port is closed.
removeEventListener in class SerialPortpublic void sendBreak(int duration)
SerialPortduration milliseconds duration.sendBreak in class SerialPortduration - The break duration in milliseconds.public void setDTR(boolean x)
SerialPortsetDTR in class SerialPortx - the statepublic void setRTS(boolean x)
SerialPortsetRTS in class SerialPortx - the statepublic void disableReceiveFraming()
CommPortdisableReceiveFraming in class CommPortpublic void disableReceiveThreshold()
CommPortdisableReceiveThreshold in class CommPortpublic void disableReceiveTimeout()
CommPortdisableReceiveTimeout in class CommPortpublic void enableReceiveThreshold(int value)
throws UnsupportedCommOperationException
CommPortenableReceiveThreshold in class CommPortvalue - the thresholdUnsupportedCommOperationException - the unsupported comm operation exceptionpublic void enableReceiveTimeout(int value)
throws UnsupportedCommOperationException
CommPortenableReceiveTimeout in class CommPortvalue - Timeout value in millisecondsUnsupportedCommOperationException - the unsupported comm operation exceptionpublic void enableReceiveFraming(int arg0)
throws UnsupportedCommOperationException
CommPortenableReceiveFraming in class CommPortarg0 - the framing byteUnsupportedCommOperationException - the unsupported comm operation exceptionpublic int getInputBufferSize()
CommPortgetInputBufferSize in class CommPortpublic int getOutputBufferSize()
CommPortgetOutputBufferSize in class CommPortpublic void setFlowControlMode(int mode)
throws UnsupportedCommOperationException
SerialPortsetFlowControlMode in class SerialPortmode - the flowcontrolUnsupportedCommOperationException - the unsupported comm operation exceptionpublic void setSerialPortParams(int baudRate,
int dataBits,
int stopBits,
int parity)
throws UnsupportedCommOperationException
SerialPortsetSerialPortParams in class SerialPortbaudRate - the baud ratedataBits - the data bitsstopBits - the stop bitsparity - the parityUnsupportedCommOperationException - the unsupported comm operation exceptionpublic int getNativeFileDescriptor()
The file descriptor can be used in calls to JTermios functions. This
maybe useful in extreme cases where performance is more important than
convenience, for example using JTermios.read(...) instead of
SerialPort.getInputStream().read(...).
Note that mixing direct JTermios read/write calls with SerialPort stream read/write calls is at best fragile and likely to fail, which also implies that when using JTermios directly then configuring the port, especially termios.cc[VMIN] and termios.cc[VTIME] is the users responsibility.
Below is a sketch of minimum necessary to perform a read using raw JTermios functionality.
// import the JTermios functionality like this
import jtermios.*;
import static jtermios.JTermios.*;
SerialPort port = ...;
// cast the port to PureJavaSerialPort to get access to getNativeFileDescriptor
int FD = ((PureJavaSerialPort) port).getNativeFileDescriptor();
// timeout and threshold values
int messageLength = 25; // bytes
int timeout = 200; // msec
// to initialize timeout and threshold first read current termios
Termios termios = new Termios();
if (0 != tcgetattr(FD, termios))
errorHandling();
// then set VTIME and VMIN, note VTIME in 1/10th of sec and both max 255
termios.c_cc[VTIME] = (byte) ((timeout+99) / 100);
termios.c_cc[VMIN] = (byte) messageLength;
// update termios
if (0 != tcsetattr(FD, TCSANOW, termios))
errorHandling();
...
// allocate read buffer
byte[] readBuffer = new byte[messageLength];
...
// then perform raw read, not this may block indefinitely
int n = read(FD, readBuffer, messageLength);
if (n < 0)
errorHandling();
public OutputStream getOutputStream() throws IOException
CommPortgetOutputStream in class CommPortnull if the port is unidirectional and doesn't support sending data.IOException - the io exceptionpublic InputStream getInputStream() throws IOException
SerialPort| 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.
getInputStream in class SerialPortnull if the port is unidirectional and doesn't support receiving data.IOException - the io exceptionpublic int getReceiveFramingByte()
CommPortgetReceiveFramingByte in class CommPortpublic int getReceiveThreshold()
CommPortgetReceiveThreshold in class CommPortpublic int getReceiveTimeout()
CommPortgetReceiveTimeout in class CommPortpublic boolean isReceiveFramingEnabled()
CommPorttrue if receive framing is enabled.isReceiveFramingEnabled in class CommPorttrue if receive framing is enabled.public boolean isReceiveThresholdEnabled()
CommPorttrue if receive threshold is enabled.isReceiveThresholdEnabled in class CommPorttrue if receive threshold is enabled.public boolean isReceiveTimeoutEnabled()
CommPorttrue if receive timeout is enabled.isReceiveTimeoutEnabled in class CommPorttrue if receive timeout is enabled.public void setInputBufferSize(int arg0)
CommPortsetInputBufferSize in class CommPortarg0 - the sizepublic void setOutputBufferSize(int arg0)
CommPortsetOutputBufferSize in class CommPortarg0 - the sizepublic void close()
CommPortpublic boolean isInternalThreadRunning()
Copyright © 2017. All rights reserved.