Class RxTxSerialPort
- All Implemented Interfaces:
Closeable,AutoCloseable,SerialPort
- Author:
- Markus Rathgeb - Initial contribution, Vita Tucek - added further methods
-
Field Summary
Fields inherited from interface org.openhab.core.io.transport.serial.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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEventListener(SerialPortEventListener listener) Registers aSerialPortEventListenerobject to listen forSerialPortEvents.voidclose()voidDisable receive timeout.voidenableReceiveThreshold(int i) Enable receive threshold with the specified thresh parameter.voidenableReceiveTimeout(int timeout) Enables the receive timeout.intGets port baud rate.intGets number of port data bits.intGets the flow control mode value.@Nullable InputStreamReturns an input stream.getName()Retrieves the name of the serial port.@Nullable OutputStreamReturns an output stream.intGets port parity.intGets number of port stop bits.booleanisCD()Check current state of CD (Carrier Detect).booleanisCTS()Check current state of CTS (Clear To Send).booleanisDSR()Check current state of DSR (Request To Send).booleanisDTR()Check current state of DTR (Data Terminal Ready).booleanisRI()Check current state of RI (Ring Indicator).booleanisRTS()Check current state of RTS (Request To Send).voidnotifyOnBreakInterrupt(boolean enable) Enable / disable the notification on break interrupt.voidnotifyOnCarrierDetect(boolean enable) Enable / disable the notification on carrier detect.voidnotifyOnCTS(boolean enable) Enable / disable the notification on CTS.voidnotifyOnDataAvailable(boolean enable) Enable / disable the notification for 'data available'.voidnotifyOnDSR(boolean enable) Enable / disable the notification on DSR.voidnotifyOnFramingError(boolean enable) Enable / disable the notification on framing error.voidnotifyOnOutputEmpty(boolean enable) Enable / disable the notification on output buffer empty.voidnotifyOnOverrunError(boolean enable) Enable / disable the notification on overrun error.voidnotifyOnParityError(boolean enable) Enable / disable the notification on parity error.voidnotifyOnRingIndicator(boolean enable) Enable / disable the notification on ring indicator.voidRemove the event listener.voidsendBreak(int duration) Send break.voidsetDTR(boolean state) Sets or clears the DTR (Request To Send) bit in the UART, if supported by the underlying implementation.voidsetFlowControlMode(int flowcontrolRtsctsOut) Sets the flow control mode value.voidsetRTS(boolean enable) 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 serial port parameters.
-
Constructor Details
-
RxTxSerialPort
public RxTxSerialPort(gnu.io.SerialPort sp) Constructor.- Parameters:
sp- the underlying serial port implementation
-
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSerialPort
-
setSerialPortParams
public void setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) throws UnsupportedCommOperationException Description copied from interface:SerialPortSets serial port parameters.- Specified by:
setSerialPortParamsin interfaceSerialPort- Parameters:
baudrate- the baud ratedataBits- the number of data bitsstopBits- the number of stop bitsparity- the parity- Throws:
UnsupportedCommOperationException- if the operation is not supported
-
getInputStream
Description copied from interface: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 behaviour of the input stream returned by getInputStream depends on combination of the threshold and timeout values. The possible behaviours are described in the table below: | Threshold .........| Timeout ........ | Read Buffer Size | Read Behaviour | | State ...| Value ..| State ...| Value | | disabled | - ......| disabled | - ....| n bytes .........| block until any data is available | enabled .| m bytes | disabled | - ....| n bytes .........| block until min(m,n) bytes are available | disabled | - ......| enabled .| x ms .| n bytes .........| block for x ms or until any data is available | enabled .| m bytes | enabled .| x ms .| n bytes .........| block for x ms or until min(m,n) bytes are available
Note, however, that framing errors may cause the Timeout and Threshold values to complete 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. *
- Specified by:
getInputStreamin interfaceSerialPort- Returns:
- the input stream or null
- Throws:
IOException- on I/O error
-
getOutputStream
Description copied from interface:SerialPortReturns an output stream.This is the only way to send data to the communications port. If the port is unidirectional and doesn't support sending data, then getOutputStream returns null.
- Specified by:
getOutputStreamin interfaceSerialPort- Returns:
- the output stream or null
- Throws:
IOException- on I/O error
-
addEventListener
Description copied from interface:SerialPortRegisters aSerialPortEventListenerobject to listen forSerialPortEvents.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. Only one listener per SerialPort is allowed. 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.
- Specified by:
addEventListenerin interfaceSerialPort- Parameters:
listener- the listener- Throws:
TooManyListenersException- if too many listeners has been added
-
removeEventListener
public void removeEventListener()Description copied from interface:SerialPortRemove the event listener.- Specified by:
removeEventListenerin interfaceSerialPort
-
notifyOnDataAvailable
public void notifyOnDataAvailable(boolean enable) Description copied from interface:SerialPortEnable / disable the notification for 'data available'.- Specified by:
notifyOnDataAvailablein interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnBreakInterrupt
public void notifyOnBreakInterrupt(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on break interrupt.- Specified by:
notifyOnBreakInterruptin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnFramingError
public void notifyOnFramingError(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on framing error.- Specified by:
notifyOnFramingErrorin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnOverrunError
public void notifyOnOverrunError(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on overrun error.- Specified by:
notifyOnOverrunErrorin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnParityError
public void notifyOnParityError(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on parity error.- Specified by:
notifyOnParityErrorin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
setRTS
public void setRTS(boolean enable) Description copied from interface:SerialPortSets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
setRTSin interfaceSerialPort- Parameters:
enable- true rts is set, false if rts cleared
-
enableReceiveTimeout
Description copied from interface:SerialPortEnables the receive timeout.When the receive timeout condition becomes true, a read from the input stream for this port will return immediately.
- Specified by:
enableReceiveTimeoutin interfaceSerialPort- Parameters:
timeout- the timeout (milliseconds), must be greater or equal to zero- Throws:
UnsupportedCommOperationException- if the operation is not supported
-
disableReceiveTimeout
public void disableReceiveTimeout()Description copied from interface:SerialPortDisable receive timeout.- Specified by:
disableReceiveTimeoutin interfaceSerialPort
-
getName
Description copied from interface:SerialPortRetrieves the name of the serial port.- Specified by:
getNamein interfaceSerialPort- Returns:
- the name of the serial port
-
setFlowControlMode
Description copied from interface:SerialPortSets the flow control mode value.- Specified by:
setFlowControlModein interfaceSerialPort- Parameters:
flowcontrolRtsctsOut- The flowcontrol (int) parameter.- Throws:
UnsupportedCommOperationException- Unsupported Comm Operation Exception.
-
enableReceiveThreshold
Description copied from interface:SerialPortEnable receive threshold with the specified thresh parameter.- Specified by:
enableReceiveThresholdin interfaceSerialPort- Parameters:
i- The thresh (int) parameter.- Throws:
UnsupportedCommOperationException- Unsupported Comm Operation Exception.
-
getBaudRate
public int getBaudRate()Description copied from interface:SerialPortGets port baud rate.- Specified by:
getBaudRatein interfaceSerialPort- Returns:
- baud rate
-
getDataBits
public int getDataBits()Description copied from interface:SerialPortGets number of port data bits.- Specified by:
getDataBitsin interfaceSerialPort- Returns:
- data bits
-
getStopBits
public int getStopBits()Description copied from interface:SerialPortGets number of port stop bits.- Specified by:
getStopBitsin interfaceSerialPort- Returns:
- stop bits count
-
getParity
public int getParity()Description copied from interface:SerialPortGets port parity.- Specified by:
getParityin interfaceSerialPort- Returns:
- parity
-
notifyOnOutputEmpty
public void notifyOnOutputEmpty(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on output buffer empty.- Specified by:
notifyOnOutputEmptyin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnCTS
public void notifyOnCTS(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on CTS.- Specified by:
notifyOnCTSin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnDSR
public void notifyOnDSR(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on DSR.- Specified by:
notifyOnDSRin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnRingIndicator
public void notifyOnRingIndicator(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on ring indicator.- Specified by:
notifyOnRingIndicatorin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
notifyOnCarrierDetect
public void notifyOnCarrierDetect(boolean enable) Description copied from interface:SerialPortEnable / disable the notification on carrier detect.- Specified by:
notifyOnCarrierDetectin interfaceSerialPort- Parameters:
enable- true if the notification should be enabled
-
getFlowControlMode
public int getFlowControlMode()Description copied from interface:SerialPortGets the flow control mode value.- Specified by:
getFlowControlModein interfaceSerialPort- Returns:
- flowcontrol value.
-
isRTS
public boolean isRTS()Description copied from interface:SerialPortCheck current state of RTS (Request To Send).- Specified by:
isRTSin interfaceSerialPort- Returns:
- true if RTS is set, otherwise false
-
setDTR
public void setDTR(boolean state) Description copied from interface:SerialPortSets or clears the DTR (Request To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
setDTRin interfaceSerialPort- Parameters:
state- true DTR is set, false if DTR cleared
-
isDTR
public boolean isDTR()Description copied from interface:SerialPortCheck current state of DTR (Data Terminal Ready).- Specified by:
isDTRin interfaceSerialPort- Returns:
- true if DTR is set, otherwise false
-
isCTS
public boolean isCTS()Description copied from interface:SerialPortCheck current state of CTS (Clear To Send).- Specified by:
isCTSin interfaceSerialPort- Returns:
- true if CTS is set, otherwise false
-
isDSR
public boolean isDSR()Description copied from interface:SerialPortCheck current state of DSR (Request To Send).- Specified by:
isDSRin interfaceSerialPort- Returns:
- true if DSR is set, otherwise false
-
isCD
public boolean isCD()Description copied from interface:SerialPortCheck current state of CD (Carrier Detect).- Specified by:
isCDin interfaceSerialPort- Returns:
- true if CD is set, otherwise false
-
isRI
public boolean isRI()Description copied from interface:SerialPortCheck current state of RI (Ring Indicator).- Specified by:
isRIin interfaceSerialPort- Returns:
- true if RI is set, otherwise false
-
sendBreak
public void sendBreak(int duration) Description copied from interface:SerialPortSend break.- Specified by:
sendBreakin interfaceSerialPort- Parameters:
duration- Break duration parameter
-