Class RxTxSerialPort

java.lang.Object
org.openhab.core.io.transport.serial.rxtx.RxTxSerialPort
All Implemented Interfaces:
Closeable, AutoCloseable, SerialPort

@NonNullByDefault public class RxTxSerialPort extends Object implements SerialPort
Specific serial port implementation.
Author:
Markus Rathgeb - Initial contribution, Vita Tucek - added further methods
  • 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 interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface SerialPort
    • 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 interface SerialPort
      Parameters:
      baudrate - the baud rate
      dataBits - the number of data bits
      stopBits - the number of stop bits
      parity - the parity
      Throws:
      UnsupportedCommOperationException - if the operation is not supported
    • getInputStream

      public @Nullable InputStream getInputStream() throws IOException
      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 interface SerialPort
      Returns:
      the input stream or null
      Throws:
      IOException - on I/O error
    • getOutputStream

      public @Nullable OutputStream getOutputStream() throws IOException
      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 interface SerialPort
      Returns:
      the output stream or null
      Throws:
      IOException - on I/O error
    • addEventListener

      public void addEventListener(SerialPortEventListener listener) throws TooManyListenersException
      Description copied from interface: SerialPort
      Registers a SerialPortEventListener object to listen for SerialPortEvents.

      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 interface SerialPort
      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 interface SerialPort
    • notifyOnDataAvailable

      public void notifyOnDataAvailable(boolean enable)
      Description copied from interface: SerialPort
      Enable / disable the notification for 'data available'.
      Specified by:
      notifyOnDataAvailable in interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      Parameters:
      enable - true rts is set, false if rts cleared
    • enableReceiveTimeout

      public void enableReceiveTimeout(int timeout) throws UnsupportedCommOperationException
      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 interface SerialPort
      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 interface SerialPort
    • getName

      public String getName()
      Description copied from interface: SerialPort
      Retrieves the name of the serial port.
      Specified by:
      getName in interface SerialPort
      Returns:
      the name of the serial port
    • setFlowControlMode

      public void setFlowControlMode(int flowcontrolRtsctsOut) throws UnsupportedCommOperationException
      Description copied from interface: SerialPort
      Sets the flow control mode value.
      Specified by:
      setFlowControlMode in interface SerialPort
      Parameters:
      flowcontrolRtsctsOut - The flowcontrol (int) parameter.
      Throws:
      UnsupportedCommOperationException - Unsupported Comm Operation Exception.
    • enableReceiveThreshold

      public void enableReceiveThreshold(int i) throws UnsupportedCommOperationException
      Description copied from interface: SerialPort
      Enable receive threshold with the specified thresh parameter.
      Specified by:
      enableReceiveThreshold in interface SerialPort
      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 interface SerialPort
      Returns:
      baud rate
    • getDataBits

      public int getDataBits()
      Description copied from interface: SerialPort
      Gets number of port data bits.
      Specified by:
      getDataBits in interface SerialPort
      Returns:
      data bits
    • getStopBits

      public int getStopBits()
      Description copied from interface: SerialPort
      Gets number of port stop bits.
      Specified by:
      getStopBits in interface SerialPort
      Returns:
      stop bits count
    • getParity

      public int getParity()
      Description copied from interface: SerialPort
      Gets port parity.
      Specified by:
      getParity in interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      Returns:
      flowcontrol value.
    • isRTS

      public boolean isRTS()
      Description copied from interface: SerialPort
      Check current state of RTS (Request To Send).
      Specified by:
      isRTS in interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      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 interface SerialPort
      Parameters:
      duration - Break duration parameter