Class SerialImpl

    • Constructor Detail

      • SerialImpl

        public SerialImpl()
        default constructor
    • Method Detail

      • open

        public void open​(String device,
                         int baud,
                         int dataBits,
                         int parity,
                         int stopBits,
                         int flowControl)
                  throws IOException

        This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations).

        (ATTENTION: the 'device' argument can only be a maximum of 128 characters.)

        Specified by:
        open in interface Serial
        Parameters:
        device - The device address of the serial port to access. You can use constant 'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the GPIO header.
        baud - The baud rate to use with the serial port. (Custom baud rate are not supported)
        dataBits - The data bits to use for serial communication. (5,6,7,8)
        parity - The parity setting to use for serial communication. (None, Event, Odd, Mark, Space)
        stopBits - The stop bits to use for serial communication. (1,2)
        flowControl - The flow control option to use for serial communication. (none, hardware, software)
        Throws:
        IOException - thrown on any error.
        See Also:
        Serial.DEFAULT_COM_PORT
      • open

        public void open​(String device,
                         int baud)
                  throws IOException

        This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations). This method will use the following default serial configuration parameters: - DATA BITS = 8 - PARITY = NONE - STOP BITS = 1 - FLOW CONTROL = NONE

        (ATTENTION: the 'device' argument can only be a maximum of 128 characters.)

        Specified by:
        open in interface Serial
        Parameters:
        device - The device address of the serial port to access. You can use constant 'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the GPIO header.
        baud - The baud rate to use with the serial port.
        Throws:
        IOException - thrown on any error.
        See Also:
        Serial.DEFAULT_COM_PORT
      • open

        public void open​(String device,
                         Baud baud,
                         DataBits dataBits,
                         Parity parity,
                         StopBits stopBits,
                         FlowControl flowControl)
                  throws IOException

        This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations).

        (ATTENTION: the 'device' argument can only be a maximum of 128 characters.)

        Specified by:
        open in interface Serial
        Parameters:
        device - The device address of the serial port to access. You can use constant 'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the GPIO header.
        baud - The baud rate to use with the serial port.
        dataBits - The data bits to use for serial communication. (5,6,7,8)
        parity - The parity setting to use for serial communication. (None, Event, Odd, Mark, Space)
        stopBits - The stop bits to use for serial communication. (1,2)
        flowControl - The flow control option to use for serial communication. (none, hardware, software)
        Throws:
        IOException - thrown on any error.
        See Also:
        Serial.DEFAULT_COM_PORT
      • open

        public void open​(SerialConfig serialConfig)
                  throws IOException

        This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations).

        (ATTENTION: the 'device' argument can only be a maximum of 128 characters.)

        Specified by:
        open in interface Serial
        Parameters:
        serialConfig - A serial configuration object that contains the device, baud rate, data bits, parity, stop bits, and flow control settings.
        Throws:
        IOException - thrown on any error.
        See Also:
        Serial.DEFAULT_COM_PORT
      • isOpen

        public boolean isOpen()
        This method is called to determine if the serial port is already open.
        Specified by:
        isOpen in interface Serial
        Returns:
        a value of 'true' is returned if the serial port is already open.
        See Also:
        open(String, int)
      • isClosed

        public boolean isClosed()
        This method is called to determine if the serial port is already closed.
        Specified by:
        isClosed in interface Serial
        Returns:
        a value of 'true' is returned if the serial port is already in the closed state.
        See Also:
        open(String, int)
      • setBreak

        public void setBreak​(boolean enabled)
                      throws IllegalStateException,
                             IOException

        Send a constant BREAK signal to connected device. (Turn break on/off) When enabled this will send a steady stream of zero bits. When enabled, no (other) data transmitting is possible.

        Specified by:
        setBreak in interface Serial
        Parameters:
        enabled - The enable or disable state to control the BREAK signal
        Throws:
        IllegalStateException - thrown if the serial port is not already open.
        IOException - thrown on any error.
      • setRTS

        public void setRTS​(boolean enabled)
                    throws IllegalStateException,
                           IOException

        Control the RTS (request-to-send) pin state. When enabled this will set the RTS pin to the HIGH state.

        Specified by:
        setRTS in interface Serial
        Parameters:
        enabled - The enable or disable state to control the RTS pin state.
        Throws:
        IllegalStateException - thrown if the serial port is not already open.
        IOException - thrown on any error.
      • setDTR

        public void setDTR​(boolean enabled)
                    throws IllegalStateException,
                           IOException

        Control the DTR (data-terminal-ready) pin state. When enabled this will set the DTR pin to the HIGH state.

        Specified by:
        setDTR in interface Serial
        Parameters:
        enabled - The enable or disable state to control the RTS pin state.
        Throws:
        IllegalStateException - thrown if the serial port is not already open.
        IOException - thrown on any error.
      • write

        public void write​(byte[] data,
                          int offset,
                          int length)
                   throws IllegalStateException,
                          IOException

        Sends an array of bytes to the serial port/device identified by the given file descriptor.

        Specified by:
        write in interface SerialDataWriter
        Specified by:
        write in class AbstractSerialDataWriter
        Parameters:
        data - A ByteBuffer of data to be transmitted.
        offset - The starting index (inclusive) in the array to send from.
        length - The number of bytes from the byte array to transmit to the serial port.
        Throws:
        IllegalStateException - thrown if the serial port is not already open.
        IOException - thrown on any error.
      • getFileDescriptor

        public int getFileDescriptor()
        This method returns the serial device file descriptor
        Specified by:
        getFileDescriptor in interface Serial
        Returns:
        fileDescriptor file descriptor
      • getInputStream

        public InputStream getInputStream()
        This method returns the input data stream for the serial port's receive buffer
        Specified by:
        getInputStream in interface Serial
        Returns:
        InputStream input stream
      • getOutputStream

        public OutputStream getOutputStream()
        This method returns the output data stream for the serial port's transmit buffer
        Specified by:
        getOutputStream in interface Serial
        Returns:
        OutputStream output stream
      • isBufferingDataReceived

        public boolean isBufferingDataReceived()
        This method returns the buffering state for data received from the serial device/port.
        Specified by:
        isBufferingDataReceived in interface Serial
        Returns:
        'true' if buffering is enabled; else 'false'
      • setBufferingDataReceived

        public void setBufferingDataReceived​(boolean enabled)

        This method controls the buffering state for data received from the serial device/port.

        If the buffering state is enabled, then all data bytes received from the serial port will get copied into a data receive buffer. You can use the 'getInputStream()' or and of the 'read()' methods to access this data. The data will also be available via the 'SerialDataEvent' event. It is important to know that if you are using data buffering, the data will continue to grow in memory until your program consume it from the data reader/stream.

        If the buffering state is disabled, then all data bytes received from the serial port will NOT get copied into the data receive buffer, but will be included in the 'SerialDataEvent' event's data payload. If you program does not care about or use data received from the serial port, then you should disable the data buffering state to prevent memory waste/leak.

        Specified by:
        setBufferingDataReceived in interface Serial
        Parameters:
        enabled - sets the buffering behavior state