Class PureJavaSerialPort
- java.lang.Object
-
- purejavacomm.CommPort
-
- purejavacomm.SerialPort
-
- purejavacomm.PureJavaSerialPort
-
public class PureJavaSerialPort extends SerialPort
-
-
Field Summary
-
Fields inherited from class purejavacomm.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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEventListener(SerialPortEventListener eventListener)Registers aSerialPortEventListenerobject to listen forSerialEvents.voidclose()Closes the communications port.voiddisableReceiveFraming()Disables receive framing.voiddisableReceiveThreshold()Disables receive threshold.voiddisableReceiveTimeout()Disables receive timeout.voidenableReceiveFraming(int arg0)Enables receive framing.voidenableReceiveThreshold(int value)Enables receive threshold.voidenableReceiveTimeout(int value)Enables receive timeout.intgetBaudRate()Returns the currently configured baud rate.intgetDataBits()Returns the currently configured number of data bits.intgetFlowControlMode()Returns the currently configured flow control mode.intgetInputBufferSize()Returns the input buffer size in bytes.InputStreamgetInputStream()Returns an input stream.intgetNativeFileDescriptor()Gets the native file descriptor used by this port.intgetOutputBufferSize()Returns the output buffer size in bytes.OutputStreamgetOutputStream()Returns an output stream.intgetParity()Returns the currently configured parity setting.intgetReceiveFramingByte()Returns the current byte used for receive framing.intgetReceiveThreshold()Returns the integer value of the receive threshold.intgetReceiveTimeout()Returns the integer value of the receive timeout.intgetStopBits()Returns the currently defined stop bits.booleanisCD()Returns the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.booleanisCTS()Returns the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.booleanisDSR()Returns the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.booleanisDTR()Returns the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.booleanisInternalThreadRunning()This is not part of the PureJavaComm API, this is purely for testing, do not depend on thisbooleanisReceiveFramingEnabled()Returnstrueif receive framing is enabled.booleanisReceiveThresholdEnabled()Returnstrueif receive threshold is enabled.booleanisReceiveTimeoutEnabled()Returnstrueif receive timeout is enabled.booleanisRI()Returns the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.booleanisRTS()Returns the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.voidnotifyOnBreakInterrupt(boolean x)Expresses interest in receiving notification when there is a break interrupt on the line.voidnotifyOnCarrierDetect(boolean x)Expresses interest in receiving notification when the CD (Carrier Detect) bit changes.voidnotifyOnCTS(boolean x)Expresses interest in receiving notification when the CTS (Clear To Send) bit changes.voidnotifyOnDataAvailable(boolean x)Expresses interest in receiving notification when input data is available.voidnotifyOnDSR(boolean x)Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes.voidnotifyOnFramingError(boolean x)Expresses interest in receiving notification when there is a framing error.voidnotifyOnOutputEmpty(boolean x)Expresses interest in receiving notification when the output buffer is empty.voidnotifyOnOverrunError(boolean x)Expresses interest in receiving notification when there is an overrun error.voidnotifyOnParityError(boolean x)Expresses interest in receiving notification when there is a parity error.voidnotifyOnRingIndicator(boolean x)Expresses interest in receiving notification when the RI (Ring Indicator) bit changes.voidremoveEventListener()Deregisters event listener registered usingaddEventListener.voidsendBreak(int duration)Sends a break ofdurationmilliseconds duration.voidsetDTR(boolean x)Sets or clears the DTR (Data Terminal Ready) signal, if supported by the underlying implementation.voidsetFlowControlMode(int mode)Sets the flow control mode.voidsetInputBufferSize(int arg0)Sets the input buffer size.voidsetOutputBufferSize(int arg0)Sets the output buffer size.voidsetRTS(boolean x)Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.voidsetSerialPortParams(int baudRate, int dataBits, int stopBits, int parity)Sets the serial port parameters.
-
-
-
Method Detail
-
addEventListener
public void addEventListener(SerialPortEventListener eventListener) throws TooManyListenersException
Description copied from class:SerialPortRegisters aSerialPortEventListenerobject to listen forSerialEvents.- Specified by:
addEventListenerin classSerialPort- Throws:
TooManyListenersException
-
getBaudRate
public int getBaudRate()
Description copied from class:SerialPortReturns the currently configured baud rate.- Specified by:
getBaudRatein classSerialPort- Returns:
- The currently configured baud rate.
-
getDataBits
public int getDataBits()
Description copied from class:SerialPortReturns the currently configured number of data bits.- Specified by:
getDataBitsin classSerialPort- Returns:
- The currently configured number of data bits.
-
getFlowControlMode
public int getFlowControlMode()
Description copied from class:SerialPortReturns the currently configured flow control mode.- Specified by:
getFlowControlModein classSerialPort- Returns:
- The currently configured flow control mode.
-
getParity
public int getParity()
Description copied from class:SerialPortReturns the currently configured parity setting.- Specified by:
getParityin classSerialPort- Returns:
- The currently configured parity setting.
-
getStopBits
public int getStopBits()
Description copied from class:SerialPortReturns the currently defined stop bits.- Specified by:
getStopBitsin classSerialPort- Returns:
- The currently defined stop bits.
-
isCD
public boolean isCD()
Description copied from class:SerialPortReturns the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.- Specified by:
isCDin classSerialPort- Returns:
- The state of the CD (Carrier Detect) bit.
-
isCTS
public boolean isCTS()
Description copied from class:SerialPortReturns the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
isCTSin classSerialPort- Returns:
- The state of the CTS (Clear To Send) bit.
-
isDSR
public boolean isDSR()
Description copied from class:SerialPortReturns the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.- Specified by:
isDSRin classSerialPort- Returns:
- The state of the DSR (Data Set Ready) bit.
-
isDTR
public boolean isDTR()
Description copied from class:SerialPortReturns the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.- Specified by:
isDTRin classSerialPort- Returns:
- The state of the DTR (Data Terminal Ready) bit.
-
isRI
public boolean isRI()
Description copied from class:SerialPortReturns the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.- Specified by:
isRIin classSerialPort- Returns:
- The state of the RI (Ring Indicator) bit.
-
isRTS
public boolean isRTS()
Description copied from class:SerialPortReturns the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
isRTSin classSerialPort- Returns:
- The state of the RTS (Request To Send) bit.
-
notifyOnBreakInterrupt
public void notifyOnBreakInterrupt(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when there is a break interrupt on the line.- Specified by:
notifyOnBreakInterruptin classSerialPort
-
notifyOnCTS
public void notifyOnCTS(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when the CTS (Clear To Send) bit changes.- Specified by:
notifyOnCTSin classSerialPort
-
notifyOnCarrierDetect
public void notifyOnCarrierDetect(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when the CD (Carrier Detect) bit changes.- Specified by:
notifyOnCarrierDetectin classSerialPort
-
notifyOnDSR
public void notifyOnDSR(boolean x)
Description copied from class:SerialPortExpresses 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.
- Specified by:
notifyOnDSRin classSerialPort
-
notifyOnDataAvailable
public void notifyOnDataAvailable(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when input data is available.- Specified by:
notifyOnDataAvailablein classSerialPort
-
notifyOnFramingError
public void notifyOnFramingError(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when there is a framing error.- Specified by:
notifyOnFramingErrorin classSerialPort
-
notifyOnOutputEmpty
public void notifyOnOutputEmpty(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when the output buffer is empty.- Specified by:
notifyOnOutputEmptyin classSerialPort
-
notifyOnOverrunError
public void notifyOnOverrunError(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when there is an overrun error.- Specified by:
notifyOnOverrunErrorin classSerialPort
-
notifyOnParityError
public void notifyOnParityError(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when there is a parity error.- Specified by:
notifyOnParityErrorin classSerialPort
-
notifyOnRingIndicator
public void notifyOnRingIndicator(boolean x)
Description copied from class:SerialPortExpresses interest in receiving notification when the RI (Ring Indicator) bit changes.- Specified by:
notifyOnRingIndicatorin classSerialPort
-
removeEventListener
public void removeEventListener()
Description copied from class:SerialPortDeregisters event listener registered usingaddEventListener.This is done automatically when the port is closed.
- Specified by:
removeEventListenerin classSerialPort
-
sendBreak
public void sendBreak(int duration)
Description copied from class:SerialPortSends a break ofdurationmilliseconds duration.- Specified by:
sendBreakin classSerialPort- Parameters:
duration- The break duration in milliseconds.
-
setDTR
public void setDTR(boolean x)
Description copied from class:SerialPortSets or clears the DTR (Data Terminal Ready) signal, if supported by the underlying implementation.- Specified by:
setDTRin classSerialPort
-
setRTS
public void setRTS(boolean x)
Description copied from class:SerialPortSets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
setRTSin classSerialPort
-
disableReceiveFraming
public void disableReceiveFraming()
Description copied from class:CommPortDisables receive framing.- Specified by:
disableReceiveFramingin classCommPort
-
disableReceiveThreshold
public void disableReceiveThreshold()
Description copied from class:CommPortDisables receive threshold.- Specified by:
disableReceiveThresholdin classCommPort
-
disableReceiveTimeout
public void disableReceiveTimeout()
Description copied from class:CommPortDisables receive timeout.- Specified by:
disableReceiveTimeoutin classCommPort
-
enableReceiveThreshold
public void enableReceiveThreshold(int value) throws UnsupportedCommOperationExceptionDescription copied from class:CommPortEnables receive threshold.- Specified by:
enableReceiveThresholdin classCommPort- Throws:
UnsupportedCommOperationException
-
enableReceiveTimeout
public void enableReceiveTimeout(int value) throws UnsupportedCommOperationExceptionDescription copied from class:CommPortEnables receive timeout.- Specified by:
enableReceiveTimeoutin classCommPort- Parameters:
value- Timeout value in milliseconds- Throws:
UnsupportedCommOperationException
-
enableReceiveFraming
public void enableReceiveFraming(int arg0) throws UnsupportedCommOperationExceptionDescription copied from class:CommPortEnables receive framing.- Specified by:
enableReceiveFramingin classCommPort- Throws:
UnsupportedCommOperationException
-
getInputBufferSize
public int getInputBufferSize()
Description copied from class:CommPortReturns the input buffer size in bytes.- Specified by:
getInputBufferSizein classCommPort- Returns:
- The input buffer size in bytes.
-
getOutputBufferSize
public int getOutputBufferSize()
Description copied from class:CommPortReturns the output buffer size in bytes.- Specified by:
getOutputBufferSizein classCommPort- Returns:
- The output buffer size in bytes.
-
setFlowControlMode
public void setFlowControlMode(int mode) throws UnsupportedCommOperationExceptionDescription copied from class:SerialPortSets the flow control mode.- Specified by:
setFlowControlModein classSerialPort- Throws:
UnsupportedCommOperationException
-
setSerialPortParams
public void setSerialPortParams(int baudRate, int dataBits, int stopBits, int parity) throws UnsupportedCommOperationExceptionDescription copied from class:SerialPortSets the serial port parameters.Default: 9600 baud, 8 data bits, 1 stop bit, no parity.
- Specified by:
setSerialPortParamsin classSerialPort- Throws:
UnsupportedCommOperationException
-
getNativeFileDescriptor
public int getNativeFileDescriptor()
Gets the native file descriptor used by this port.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 ofSerialPort.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();- Returns:
- the native OS file descriptor as int
-
getOutputStream
public OutputStream getOutputStream() throws IOException
Description copied from class:CommPortReturns an output stream.- Specified by:
getOutputStreamin classCommPort- Returns:
- An output stream, or
nullif the port is unidirectional and doesn't support sending data. - Throws:
IOException
-
getInputStream
public InputStream getInputStream() throws IOException
Description copied from class:SerialPortReturns 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 classSerialPort- Returns:
- An input stream, or
nullif the port is unidirectional and doesn't support receiving data. - Throws:
IOException
-
getReceiveFramingByte
public int getReceiveFramingByte()
Description copied from class:CommPortReturns the current byte used for receive framing.- Specified by:
getReceiveFramingBytein classCommPort- Returns:
- The current byte used for receive framing.
-
getReceiveThreshold
public int getReceiveThreshold()
Description copied from class:CommPortReturns the integer value of the receive threshold.- Specified by:
getReceiveThresholdin classCommPort- Returns:
- The integer value of the receive threshold.
-
getReceiveTimeout
public int getReceiveTimeout()
Description copied from class:CommPortReturns the integer value of the receive timeout. If the receive timeout is disabled, then the value returned is meaningless.- Specified by:
getReceiveTimeoutin classCommPort- Returns:
- The integer value of the receive timeout.
-
isReceiveFramingEnabled
public boolean isReceiveFramingEnabled()
Description copied from class:CommPortReturnstrueif receive framing is enabled.- Specified by:
isReceiveFramingEnabledin classCommPort- Returns:
trueif receive framing is enabled.
-
isReceiveThresholdEnabled
public boolean isReceiveThresholdEnabled()
Description copied from class:CommPortReturnstrueif receive threshold is enabled.- Specified by:
isReceiveThresholdEnabledin classCommPort- Returns:
trueif receive threshold is enabled.
-
isReceiveTimeoutEnabled
public boolean isReceiveTimeoutEnabled()
Description copied from class:CommPortReturnstrueif receive timeout is enabled.- Specified by:
isReceiveTimeoutEnabledin classCommPort- Returns:
trueif receive timeout is enabled.
-
setInputBufferSize
public void setInputBufferSize(int arg0)
Description copied from class:CommPortSets the input buffer size.- Specified by:
setInputBufferSizein classCommPort
-
setOutputBufferSize
public void setOutputBufferSize(int arg0)
Description copied from class:CommPortSets the output buffer size.- Specified by:
setOutputBufferSizein classCommPort
-
close
public void close()
Description copied from class:CommPortCloses the communications port.
-
isInternalThreadRunning
public boolean isInternalThreadRunning()
This is not part of the PureJavaComm API, this is purely for testing, do not depend on this
-
-