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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addEventListener
(SerialPortEventListener listener) Registers aSerialPortEventListener
object to listen forSerialPortEvent
s.void
close()
void
Disable receive timeout.void
enableReceiveThreshold
(int i) Enable receive threshold with the specified thresh parameter.void
enableReceiveTimeout
(int timeout) Enables the receive timeout.int
Gets port baud rate.int
Gets number of port data bits.int
Gets the flow control mode value.@Nullable InputStream
Returns an input stream.getName()
Retrieves the name of the serial port.@Nullable OutputStream
Returns an output stream.int
Gets port parity.int
Gets number of port stop bits.boolean
isCD()
Check current state of CD (Carrier Detect).boolean
isCTS()
Check current state of CTS (Clear To Send).boolean
isDSR()
Check current state of DSR (Request To Send).boolean
isDTR()
Check current state of DTR (Data Terminal Ready).boolean
isRI()
Check current state of RI (Ring Indicator).boolean
isRTS()
Check current state of RTS (Request To Send).void
notifyOnBreakInterrupt
(boolean enable) Enable / disable the notification on break interrupt.void
notifyOnCarrierDetect
(boolean enable) Enable / disable the notification on carrier detect.void
notifyOnCTS
(boolean enable) Enable / disable the notification on CTS.void
notifyOnDataAvailable
(boolean enable) Enable / disable the notification for 'data available'.void
notifyOnDSR
(boolean enable) Enable / disable the notification on DSR.void
notifyOnFramingError
(boolean enable) Enable / disable the notification on framing error.void
notifyOnOutputEmpty
(boolean enable) Enable / disable the notification on output buffer empty.void
notifyOnOverrunError
(boolean enable) Enable / disable the notification on overrun error.void
notifyOnParityError
(boolean enable) Enable / disable the notification on parity error.void
notifyOnRingIndicator
(boolean enable) Enable / disable the notification on ring indicator.void
Remove the event listener.void
sendBreak
(int duration) Send break.void
setDTR
(boolean state) Sets or clears the DTR (Request To Send) bit in the UART, if supported by the underlying implementation.void
setFlowControlMode
(int flowcontrolRtsctsOut) Sets the flow control mode value.void
setRTS
(boolean enable) 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 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceSerialPort
-
setSerialPortParams
public void setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) throws UnsupportedCommOperationException Description copied from interface:SerialPort
Sets serial port parameters.- Specified by:
setSerialPortParams
in 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:SerialPort
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 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:
getInputStream
in interfaceSerialPort
- Returns:
- the input stream or null
- Throws:
IOException
- on I/O error
-
getOutputStream
Description copied from interface:SerialPort
Returns 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:
getOutputStream
in interfaceSerialPort
- Returns:
- the output stream or null
- Throws:
IOException
- on I/O error
-
addEventListener
Description copied from interface:SerialPort
Registers aSerialPortEventListener
object to listen forSerialPortEvent
s.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:
addEventListener
in interfaceSerialPort
- Parameters:
listener
- the listener- Throws:
TooManyListenersException
- if too many listeners has been added
-
removeEventListener
public void removeEventListener()Description copied from interface:SerialPort
Remove the event listener.- Specified by:
removeEventListener
in interfaceSerialPort
-
notifyOnDataAvailable
public void notifyOnDataAvailable(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification for 'data available'.- Specified by:
notifyOnDataAvailable
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnBreakInterrupt
public void notifyOnBreakInterrupt(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on break interrupt.- Specified by:
notifyOnBreakInterrupt
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnFramingError
public void notifyOnFramingError(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on framing error.- Specified by:
notifyOnFramingError
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnOverrunError
public void notifyOnOverrunError(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on overrun error.- Specified by:
notifyOnOverrunError
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnParityError
public void notifyOnParityError(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on parity error.- Specified by:
notifyOnParityError
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
setRTS
public void setRTS(boolean enable) Description copied from interface:SerialPort
Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
setRTS
in interfaceSerialPort
- Parameters:
enable
- true rts is set, false if rts cleared
-
enableReceiveTimeout
Description copied from interface:SerialPort
Enables the receive timeout.When the receive timeout condition becomes true, a read from the input stream for this port will return immediately.
- Specified by:
enableReceiveTimeout
in 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:SerialPort
Disable receive timeout.- Specified by:
disableReceiveTimeout
in interfaceSerialPort
-
getName
Description copied from interface:SerialPort
Retrieves the name of the serial port.- Specified by:
getName
in interfaceSerialPort
- Returns:
- the name of the serial port
-
setFlowControlMode
Description copied from interface:SerialPort
Sets the flow control mode value.- Specified by:
setFlowControlMode
in interfaceSerialPort
- Parameters:
flowcontrolRtsctsOut
- The flowcontrol (int
) parameter.- Throws:
UnsupportedCommOperationException
- Unsupported Comm Operation Exception.
-
enableReceiveThreshold
Description copied from interface:SerialPort
Enable receive threshold with the specified thresh parameter.- Specified by:
enableReceiveThreshold
in interfaceSerialPort
- Parameters:
i
- The thresh (int
) parameter.- Throws:
UnsupportedCommOperationException
- Unsupported Comm Operation Exception.
-
getBaudRate
public int getBaudRate()Description copied from interface:SerialPort
Gets port baud rate.- Specified by:
getBaudRate
in interfaceSerialPort
- Returns:
- baud rate
-
getDataBits
public int getDataBits()Description copied from interface:SerialPort
Gets number of port data bits.- Specified by:
getDataBits
in interfaceSerialPort
- Returns:
- data bits
-
getStopBits
public int getStopBits()Description copied from interface:SerialPort
Gets number of port stop bits.- Specified by:
getStopBits
in interfaceSerialPort
- Returns:
- stop bits count
-
getParity
public int getParity()Description copied from interface:SerialPort
Gets port parity.- Specified by:
getParity
in interfaceSerialPort
- Returns:
- parity
-
notifyOnOutputEmpty
public void notifyOnOutputEmpty(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on output buffer empty.- Specified by:
notifyOnOutputEmpty
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnCTS
public void notifyOnCTS(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on CTS.- Specified by:
notifyOnCTS
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnDSR
public void notifyOnDSR(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on DSR.- Specified by:
notifyOnDSR
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnRingIndicator
public void notifyOnRingIndicator(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on ring indicator.- Specified by:
notifyOnRingIndicator
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
notifyOnCarrierDetect
public void notifyOnCarrierDetect(boolean enable) Description copied from interface:SerialPort
Enable / disable the notification on carrier detect.- Specified by:
notifyOnCarrierDetect
in interfaceSerialPort
- Parameters:
enable
- true if the notification should be enabled
-
getFlowControlMode
public int getFlowControlMode()Description copied from interface:SerialPort
Gets the flow control mode value.- Specified by:
getFlowControlMode
in interfaceSerialPort
- Returns:
- flowcontrol value.
-
isRTS
public boolean isRTS()Description copied from interface:SerialPort
Check current state of RTS (Request To Send).- Specified by:
isRTS
in interfaceSerialPort
- Returns:
- true if RTS is set, otherwise false
-
setDTR
public void setDTR(boolean state) Description copied from interface:SerialPort
Sets or clears the DTR (Request To Send) bit in the UART, if supported by the underlying implementation.- Specified by:
setDTR
in interfaceSerialPort
- Parameters:
state
- true DTR is set, false if DTR cleared
-
isDTR
public boolean isDTR()Description copied from interface:SerialPort
Check current state of DTR (Data Terminal Ready).- Specified by:
isDTR
in interfaceSerialPort
- Returns:
- true if DTR is set, otherwise false
-
isCTS
public boolean isCTS()Description copied from interface:SerialPort
Check current state of CTS (Clear To Send).- Specified by:
isCTS
in interfaceSerialPort
- Returns:
- true if CTS is set, otherwise false
-
isDSR
public boolean isDSR()Description copied from interface:SerialPort
Check current state of DSR (Request To Send).- Specified by:
isDSR
in interfaceSerialPort
- Returns:
- true if DSR is set, otherwise false
-
isCD
public boolean isCD()Description copied from interface:SerialPort
Check current state of CD (Carrier Detect).- Specified by:
isCD
in interfaceSerialPort
- Returns:
- true if CD is set, otherwise false
-
isRI
public boolean isRI()Description copied from interface:SerialPort
Check current state of RI (Ring Indicator).- Specified by:
isRI
in interfaceSerialPort
- Returns:
- true if RI is set, otherwise false
-
sendBreak
public void sendBreak(int duration) Description copied from interface:SerialPort
Send break.- Specified by:
sendBreak
in interfaceSerialPort
- Parameters:
duration
- Break duration parameter
-