Interface SerialIOAdapter

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
JnaLinuxSerial, JnaSerial, JSCSerial, JSSCSerial

public interface SerialIOAdapter extends AutoCloseable
Wrapper interface that provides the interface used by SerialIOProcessor, to be implemented for different implementations for serial communication, such as jSSC, jSerialComm or others.
Each SerialIOAdapter is a very simple class that holds the absolute minimum of functionality to support serial IO. It contains at least a default constructor.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Special exception to be used when the port is not available.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
    Baudrate constant for 115200 baud.
    static int
    Baudrate constant for 1200 baud.
    static int
    Baudrate constant for 128000 baud.
    static int
    Baudrate constant for 19200 baud.
    static int
    Baudrate constant for 2400 baud.
    static int
    Baudrate constant for 256000 baud.
    static int
    Baudrate constant for 38400 baud.
    static int
    Baudrate constant for 4800 baud.
    static int
    Baudrate constant for 57600 baud.
    static int
    Baudrate constant for 9600 baud.
    static Integer
    Current baud rate.
    static int
    Constant for 7 bit data size.
    static int
    Constant for 8 bit data size.
    static int
    Constant for even parity.
    static int
    Constant for mark parity.
    static int
    Constant for no parity.
    static int
    Constant for odd parity.
    static int
    Constant for space parity.
    static int
    Constant for serial communication using 1 stop bit.
    static int
    Constant for serial communication using 2 stop bits.
    static int
    Constant for requesting valid baud rate constants.
    static int
    Constant for requesting valid baud rate constants.
    static int
    Constant for requesting valid baud rate constants.
    static int
    Constant for requesting valid baud rate constants.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of received bytes that can be read without blocking.
    void
    Close the communication port.
    boolean
    exits​(String port)
    Checks whether a port still exists.
    void
    Flushed input and output buffers.
    int[][]
    getCommunicationConstants​(int constantType)
    Retrieves an array of integer pairs specifying all valid parameters for the given constant type.
    void
    open​(String port)
    Opens a port for serial IO.
    byte[]
    read​(int count, int timeout)
    Reads data from a communication port.
    void
    setParameters​(int baudrate, int databits, int stopbits, int parity)
    Set communication parameters for speed, data size and frame structure.
    baudrate specifies the baud rate, databits the number of data bits, stopbits the number of stop bits per data unit and parity the kind of parity bit, if parity shall be used.
    void
    write​(byte[] buffer)
    Write the given byte buffer to the com port.
  • Field Details

    • B_1200

      static final int B_1200
      Baudrate constant for 1200 baud.
      See Also:
      Constant Field Values
    • B_2400

      static final int B_2400
      Baudrate constant for 2400 baud.
      See Also:
      Constant Field Values
    • B_4800

      static final int B_4800
      Baudrate constant for 4800 baud.
      See Also:
      Constant Field Values
    • B_9600

      static final int B_9600
      Baudrate constant for 9600 baud.
      See Also:
      Constant Field Values
    • B_19200

      static final int B_19200
      Baudrate constant for 19200 baud.
      See Also:
      Constant Field Values
    • B_38400

      static final int B_38400
      Baudrate constant for 38400 baud.
      See Also:
      Constant Field Values
    • B_57600

      static final int B_57600
      Baudrate constant for 57600 baud.
      See Also:
      Constant Field Values
    • B_115200

      static final int B_115200
      Baudrate constant for 115200 baud.
      See Also:
      Constant Field Values
    • B_128000

      static final int B_128000
      Baudrate constant for 128000 baud.
      See Also:
      Constant Field Values
    • B_256000

      static final int B_256000
      Baudrate constant for 256000 baud.
      See Also:
      Constant Field Values
    • D_7

      static final int D_7
      Constant for 7 bit data size.
      See Also:
      Constant Field Values
    • D_8

      static final int D_8
      Constant for 8 bit data size.
      See Also:
      Constant Field Values
    • S_1

      static final int S_1
      Constant for serial communication using 1 stop bit.
      See Also:
      Constant Field Values
    • S_2

      static final int S_2
      Constant for serial communication using 2 stop bits.
      See Also:
      Constant Field Values
    • P_NO

      static final int P_NO
      Constant for no parity.
      See Also:
      Constant Field Values
    • P_ODD

      static final int P_ODD
      Constant for odd parity.
      See Also:
      Constant Field Values
    • P_EVEN

      static final int P_EVEN
      Constant for even parity.
      See Also:
      Constant Field Values
    • P_MARK

      static final int P_MARK
      Constant for mark parity.
      See Also:
      Constant Field Values
    • P_SPACE

      static final int P_SPACE
      Constant for space parity.
      See Also:
      Constant Field Values
    • T_BAUD

      static final int T_BAUD
      Constant for requesting valid baud rate constants.
      See Also:
      Constant Field Values
    • T_DATA

      static final int T_DATA
      Constant for requesting valid baud rate constants.
      See Also:
      Constant Field Values
    • T_STOP

      static final int T_STOP
      Constant for requesting valid baud rate constants.
      See Also:
      Constant Field Values
    • T_PARITY

      static final int T_PARITY
      Constant for requesting valid baud rate constants.
      See Also:
      Constant Field Values
    • Baudrate

      static final Integer Baudrate
      Current baud rate. Set to null as long as it has not been set to a valid baud rate.
  • Method Details

    • open

      void open(String port) throws IOException
      Opens a port for serial IO.
      Parameters:
      port - Port. e.g. COM2
      Throws:
      IOException - If the port does not exist or in case of IO errors.
    • close

      void close() throws IOException
      Close the communication port.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException - In case of IO error.
    • setParameters

      void setParameters(int baudrate, int databits, int stopbits, int parity) throws IOException
      Set communication parameters for speed, data size and frame structure.
      baudrate specifies the baud rate, databits the number of data bits, stopbits the number of stop bits per data unit and parity the kind of parity bit, if parity shall be used. Keep in mind that these constants can vary between adapter implementations. The application should use method getCommunicationConstants to retrieve the valid constant values and the corresponding adapter independent values.
      Parameters:
      baudrate - Baud rate.
      databits - Bits per date unit.
      stopbits - Number of stop bits to be used.
      parity - Parity constant.
      Throws:
      IOException - If an IO error occurs of if a parameter is invalid.
    • available

      int available() throws IOException
      Returns the number of received bytes that can be read without blocking.
      Returns:
      Number of bytes just in device input buffer.
      Throws:
      IOException - In case of an IO error
    • read

      byte[] read(int count, int timeout) throws IOException
      Reads data from a communication port. If no data are available, read blocks until data are available or the specified timeout has been reached.
      Parameters:
      count - Maximum number of bytes to be read.
      timeout - Maximum time to wait for data in milliseconds.
      Returns:
      byte array holding all available data bytes
      Throws:
      IOException - In case of an IO error.
    • write

      void write(byte[] buffer) throws IOException
      Write the given byte buffer to the com port.
      Parameters:
      buffer - Buffer to be written
      Throws:
      IOException - If the port has not been opened or in case of an IO error.
    • flush

      void flush() throws IOException
      Flushed input and output buffers.
      Throws:
      IOException - In case of an IO error.
    • exits

      boolean exits(String port)
      Checks whether a port still exists. In case of serial communication via Bluetooth or USB, ports can go lost.
      Parameters:
      port - Port to be checked.
      Returns:
      true if the port still exists, false otherwise-
    • getCommunicationConstants

      int[][] getCommunicationConstants(int constantType)
      Retrieves an array of integer pairs specifying all valid parameters for the given constant type. constantType can be one of T_BAUD, T_DATA, T_STOP or T_PARITY. The method returns an array of integer pairs where the first element specifies the constant value to be passed to the setParameters method of the SerialIOProcessor and the second element specifies the corresponding value to be passed to the setParameters method of the specific SerialIOAdapter implementation.
      Parameters:
      constantType - Type of constants to be requested.
      Returns:
      Array of integer pairs containing the adapter independent and adapter specific constants of the requested type. null if constantType is invalid.