Package jssc

Class SerialPort


  • public class SerialPort
    extends Object
    Author:
    scream3r
    • Field Detail

      • BAUDRATE_110

        public static final int BAUDRATE_110
        Baud rate 110 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_300

        public static final int BAUDRATE_300
        Baud rate 300 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_600

        public static final int BAUDRATE_600
        Baud rate 600 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_1200

        public static final int BAUDRATE_1200
        Baud rate 1200 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_2400

        public static final int BAUDRATE_2400
        Baud rate 2400 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_4800

        public static final int BAUDRATE_4800
        Baud rate 4800 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_9600

        public static final int BAUDRATE_9600
        Baud rate 9600 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_14400

        public static final int BAUDRATE_14400
        Baud rate 14400 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_19200

        public static final int BAUDRATE_19200
        Baud rate 19200 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_38400

        public static final int BAUDRATE_38400
        Baud rate 38400 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_57600

        public static final int BAUDRATE_57600
        Baud rate 57600 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_115200

        public static final int BAUDRATE_115200
        Baud rate 115200 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_128000

        public static final int BAUDRATE_128000
        Baud rate 128000 symbols/second
        See Also:
        Constant Field Values
      • BAUDRATE_256000

        public static final int BAUDRATE_256000
        Baud rate 256000 symbols/second
        See Also:
        Constant Field Values
      • DATABITS_5

        public static final int DATABITS_5
        Five (5) data bits per byte
        See Also:
        Constant Field Values
      • DATABITS_6

        public static final int DATABITS_6
        Six (6) data bits per byte
        See Also:
        Constant Field Values
      • DATABITS_7

        public static final int DATABITS_7
        Seven (7) data bits per byte
        See Also:
        Constant Field Values
      • DATABITS_8

        public static final int DATABITS_8
        Eight (8) data bits per byte
        See Also:
        Constant Field Values
      • STOPBITS_1

        public static final int STOPBITS_1
        One (1) stop bits per byte
        See Also:
        Constant Field Values
      • STOPBITS_2

        public static final int STOPBITS_2
        Two (2) stop bits per byte
        See Also:
        Constant Field Values
      • STOPBITS_1_5

        public static final int STOPBITS_1_5
        One and a half (1.5) stop bits per byte
        See Also:
        Constant Field Values
      • PARITY_NONE

        public static final int PARITY_NONE
        Do not send a parity bit
        See Also:
        Constant Field Values
      • PARITY_ODD

        public static final int PARITY_ODD
        Use a very basic checksum: Send an additional bit to report if odd number of data bits
        See Also:
        Constant Field Values
      • PARITY_EVEN

        public static final int PARITY_EVEN
        Use a very basic checksum: Send an additional bit to report if even number of data bits
        See Also:
        Constant Field Values
      • PARITY_MARK

        public static final int PARITY_MARK
        Use a very basic checksum: Send a parity bit to indicate "mark signal" condition (rarely used)
        See Also:
        Constant Field Values
      • PARITY_SPACE

        public static final int PARITY_SPACE
        Use a very basic checksum: Send a parity bit to indicate "space signal" condition (rarely used)
        See Also:
        Constant Field Values
      • PURGE_RXABORT

        public static final int PURGE_RXABORT
        Receive "abort" status code
        See Also:
        Constant Field Values
      • PURGE_RXCLEAR

        public static final int PURGE_RXCLEAR
        Receive "clear" status code
        See Also:
        Constant Field Values
      • PURGE_TXABORT

        public static final int PURGE_TXABORT
        Transfer "abort" status code
        See Also:
        Constant Field Values
      • PURGE_TXCLEAR

        public static final int PURGE_TXCLEAR
        Transfer "clear" status code
        See Also:
        Constant Field Values
      • MASK_RXCHAR

        public static final int MASK_RXCHAR
        Receive character flag
        See Also:
        Constant Field Values
      • MASK_DSR

        public static final int MASK_DSR
        DSR (data set to ready) flag
        See Also:
        Constant Field Values
      • MASK_RLSD

        public static final int MASK_RLSD
        RLSD (Receive Line Signal Detected) flag
        See Also:
        Constant Field Values
      • MASK_BREAK

        public static final int MASK_BREAK
        Break (reset a communications line or change the operating mode) flag
        See Also:
        Constant Field Values
      • MASK_RING

        public static final int MASK_RING
        State of RING line (0 - OFF, 1 - ON)
        See Also:
        Constant Field Values
      • FLOWCONTROL_NONE

        public static final int FLOWCONTROL_NONE
        Disable flow control
        See Also:
        Constant Field Values
      • FLOWCONTROL_RTSCTS_IN

        public static final int FLOWCONTROL_RTSCTS_IN
        Legacy hardware control: RTS/CTS (request to send/clear to send) flow control on input
        See Also:
        Constant Field Values
      • FLOWCONTROL_RTSCTS_OUT

        public static final int FLOWCONTROL_RTSCTS_OUT
        Legacy hardware control: RTS/CTS (request to send/clear to send) flow control on output
        See Also:
        Constant Field Values
      • FLOWCONTROL_XONXOFF_IN

        public static final int FLOWCONTROL_XONXOFF_IN
        Software flow control: XON/XOFF flow control on input
        See Also:
        Constant Field Values
      • FLOWCONTROL_XONXOFF_OUT

        public static final int FLOWCONTROL_XONXOFF_OUT
        Software flow control: XON/XOFF flow control on output
        See Also:
        Constant Field Values
      • ERROR_FRAME

        public static final int ERROR_FRAME
        Indicates faulty data or frame
        See Also:
        Constant Field Values
      • ERROR_OVERRUN

        public static final int ERROR_OVERRUN
        Overrun error; receiver hardware unable to handle receive data
        See Also:
        Constant Field Values
      • ERROR_PARITY

        public static final int ERROR_PARITY
        Parity/check bit error
        See Also:
        Constant Field Values
    • Constructor Detail

      • SerialPort

        public SerialPort​(String portName)
        Construct a serial port object with the specified portName
        Parameters:
        portName - Name of the port, e.g. COM1, /dev/tty.FOO, etc.
    • Method Detail

      • getPortName

        public String getPortName()
        Getting port name under operation
        Returns:
        Method returns port name under operation as a String
      • isOpened

        public boolean isOpened()
        Getting port state
        Returns:
        Method returns true if port is open, otherwise false
      • openPort

        public boolean openPort()
                         throws SerialPortException
        Port opening

        Note: If port busy TYPE_PORT_BUSY exception will be thrown. If port not found TYPE_PORT_NOT_FOUND exception will be thrown.
        Returns:
        If the operation is successfully completed, the method returns true
        Throws:
        SerialPortException - if exception occurred
      • setParams

        public boolean setParams​(int baudRate,
                                 int dataBits,
                                 int stopBits,
                                 int parity)
                          throws SerialPortException
        Setting the parameters of port. RTS and DTR lines are enabled by default
        Parameters:
        baudRate - data transfer rate
        dataBits - number of data bits
        stopBits - number of stop bits
        parity - parity
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • setParams

        public boolean setParams​(int baudRate,
                                 int dataBits,
                                 int stopBits,
                                 int parity,
                                 boolean setRTS,
                                 boolean setDTR)
                          throws SerialPortException
        Setting the parameters of port
        Parameters:
        baudRate - data transfer rate
        dataBits - number of data bits
        stopBits - number of stop bits
        parity - parity
        setRTS - initial state of RTS line(ON/OFF)
        setDTR - initial state of DTR line(ON/OFF)
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • purgePort

        public boolean purgePort​(int flags)
                          throws SerialPortException
        Purge of input and output buffer. Required flags shall be sent to the input. Variables with prefix "PURGE_", for example "PURGE_RXCLEAR". Sent parameter "flags" is additive value, so addition of flags is allowed. For example, if input or output buffer shall be purged, parameter "PURGE_RXCLEAR | PURGE_TXCLEAR".
        Note: some devices or drivers may not support this function
        Parameters:
        flags - One or more SerialPort.PURGE_* flag
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false.
        Throws:
        SerialPortException - if exception occurred
      • setEventsMask

        public boolean setEventsMask​(int mask)
                              throws SerialPortException
        Set events mask. Required flags shall be sent to the input. Variables with prefix "MASK_", shall be used as flags, for example "MASK_RXCHAR". Sent parameter "mask" is additive value, so addition of flags is allowed. For example if messages about data receipt and CTS and DSR status changing shall be received, it is required to set the mask - "MASK_RXCHAR | MASK_CTS | MASK_DSR"
        Parameters:
        mask - One or more SerialPort.MASK_* value
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • getEventsMask

        public int getEventsMask()
                          throws SerialPortException
        Getting events mask for the port
        Returns:
        Method returns events mask as int type variable. This variable is an additive value
        Throws:
        SerialPortException - if exception occurred
      • setRTS

        public boolean setRTS​(boolean enabled)
                       throws SerialPortException
        Change RTS line state. Set "true" for switching ON and "false" for switching OFF RTS line
        Parameters:
        enabled - on/off flag
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • setDTR

        public boolean setDTR​(boolean enabled)
                       throws SerialPortException
        Change DTR line state. Set "true" for switching ON and "false" for switching OFF DTR line
        Parameters:
        enabled - on/off flag
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • writeBytes

        public boolean writeBytes​(byte[] buffer)
                           throws SerialPortException
        Write byte array to port
        Parameters:
        buffer - byte[] array to write
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • writeByte

        public boolean writeByte​(byte singleByte)
                          throws SerialPortException
        Write single byte to port
        Parameters:
        singleByte - single byte value to write
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • writeString

        public boolean writeString​(String string)
                            throws SerialPortException
        Write String to port
        Parameters:
        string - String value to write
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • writeInt

        public boolean writeInt​(int singleInt)
                         throws SerialPortException
        Write int value (in range from 0 to 255 (0x00 - 0xFF)) to port
        Parameters:
        singleInt - single int value to write
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • writeIntArray

        public boolean writeIntArray​(int[] buffer)
                              throws SerialPortException
        Write int array (in range from 0 to 255 (0x00 - 0xFF)) to port
        Parameters:
        buffer - int[] array to write
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readBytes

        public byte[] readBytes​(int byteCount)
                         throws SerialPortException
        Read byte array from port
        Parameters:
        byteCount - count of bytes for reading
        Returns:
        byte array with "byteCount" length
        Throws:
        SerialPortException - if exception occurred
      • readString

        public String readString​(int byteCount)
                          throws SerialPortException
        Read string from port
        Parameters:
        byteCount - count of bytes for reading
        Returns:
        byte array with "byteCount" length converted to String
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexString

        public String readHexString​(int byteCount)
                             throws SerialPortException
        Read Hex string from port (example: FF 0A FF). Separator by default is a space
        Parameters:
        byteCount - count of bytes for reading
        Returns:
        byte array with "byteCount" length converted to Hexadecimal String
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexString

        public String readHexString​(int byteCount,
                                    String separator)
                             throws SerialPortException
        Read Hex string from port with set separator (example if separator is "::": FF::0A::FF)
        Parameters:
        byteCount - count of bytes for reading
        separator - the hex string separator, e.g. "::", etc
        Returns:
        byte array with "byteCount" length converted to Hexadecimal String
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexStringArray

        public String[] readHexStringArray​(int byteCount)
                                    throws SerialPortException
        Read Hex String array from port
        Parameters:
        byteCount - count of bytes for reading
        Returns:
        String array with "byteCount" length and Hexadecimal String values
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readIntArray

        public int[] readIntArray​(int byteCount)
                           throws SerialPortException
        Read int array from port
        Parameters:
        byteCount - count of bytes for reading
        Returns:
        int array with values in range from 0 to 255
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexString

        public String readHexString​(int byteCount,
                                    int timeout)
                             throws SerialPortException,
                                    SerialPortTimeoutException
        Read Hex string from port (example: FF 0A FF). Separator by default is a space
        Parameters:
        byteCount - count of bytes for reading
        timeout - timeout in milliseconds
        Returns:
        byte array with "byteCount" length converted to Hexadecimal String
        Throws:
        SerialPortException - if exception occurred
        SerialPortTimeoutException - if timeout exception occurred
        Since:
        2.0
      • readHexString

        public String readHexString​(int byteCount,
                                    String separator,
                                    int timeout)
                             throws SerialPortException,
                                    SerialPortTimeoutException
        Read Hex string from port with set separator (example if separator is "::": FF::0A::FF)
        Parameters:
        byteCount - count of bytes for reading
        separator - the hex string separator, e.g. "::", etc
        timeout - timeout in milliseconds
        Returns:
        byte array with "byteCount" length converted to Hexadecimal String
        Throws:
        SerialPortException - if exception occurred
        SerialPortTimeoutException - if timeout exception occurred
        Since:
        2.0
      • readBytes

        public byte[] readBytes()
                         throws SerialPortException
        Read all available bytes from port like a byte array
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readString

        public String readString()
                          throws SerialPortException
        Read all available bytes from port like a String
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port converted to String
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexString

        public String readHexString()
                             throws SerialPortException
        Read all available bytes from port like a Hex String
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port converted to Hex String
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexString

        public String readHexString​(String separator)
                             throws SerialPortException
        Read all available bytes from port like a Hex String with set separator
        Parameters:
        separator - the hex string separator, e.g. "::", etc
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port converted to Hex String
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readHexStringArray

        public String[] readHexStringArray()
                                    throws SerialPortException
        Read all available bytes from port like a Hex String array
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port converted to Hex String array
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • readIntArray

        public int[] readIntArray()
                           throws SerialPortException
        Read all available bytes from port like a int array (values in range from 0 to 255)
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port converted to int array
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • getInputBufferBytesCount

        public int getInputBufferBytesCount()
                                     throws SerialPortException
        Get count of bytes in input buffer
        Returns:
        Count of bytes in input buffer or -1 if error occured
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • getOutputBufferBytesCount

        public int getOutputBufferBytesCount()
                                      throws SerialPortException
        Get count of bytes in output buffer
        Returns:
        Count of bytes in output buffer or -1 if error occured
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • setFlowControlMode

        public boolean setFlowControlMode​(int mask)
                                   throws SerialPortException
        Set flow control mode. For required mode use variables with prefix "FLOWCONTROL_". Example of hardware flow control mode(RTS/CTS): setFlowControlMode(FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT);
        Parameters:
        mask - mask of flow control mode
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • getFlowControlMode

        public int getFlowControlMode()
                               throws SerialPortException
        Get flow control mode
        Returns:
        Mask of set flow control mode
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • sendBreak

        public boolean sendBreak​(int duration)
                          throws SerialPortException
        Send Break singnal for set duration
        Parameters:
        duration - duration of Break signal, in milliseconds
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
        Since:
        0.8
      • getLinesStatus

        public int[] getLinesStatus()
                             throws SerialPortException
        Getting lines status. Lines status is sent as 0 – OFF and 1 - ON
        Returns:
        Method returns the array containing information about lines in following order:
        element 0 - CTS line state
        element 1 - DSR line state
        element 2 - RING line state
        element 3 - RLSD line state
        Throws:
        SerialPortException - if exception occurred
      • isCTS

        public boolean isCTS()
                      throws SerialPortException
        Get state of CTS line
        Returns:
        If line is active, method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • isDSR

        public boolean isDSR()
                      throws SerialPortException
        Get state of DSR line
        Returns:
        If line is active, method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • isRING

        public boolean isRING()
                       throws SerialPortException
        Get state of RING line
        Returns:
        If line is active, method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • isRLSD

        public boolean isRLSD()
                       throws SerialPortException
        Get state of RLSD line
        Returns:
        If line is active, method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • addEventListener

        public void addEventListener​(SerialPortEventListener listener)
                              throws SerialPortException
        Add event listener. Object of "SerialPortEventListener" type shall be sent to the method. This object shall be properly described, as it will be in charge for handling of occurred events. This method will independently set the mask in "MASK_RXCHAR" state if it was not set beforehand
        Parameters:
        listener - Event Listener of type SerialPortListener
        Throws:
        SerialPortException - if exception occurred
      • addEventListener

        public void addEventListener​(SerialPortEventListener listener,
                                     int mask)
                              throws SerialPortException
        Add event listener. Object of "SerialPortEventListener" type shall be sent to the method. This object shall be properly described, as it will be in charge for handling of occurred events. Also events mask shall be sent to this method, to do it use variables with prefix "MASK_" for example "MASK_RXCHAR"
        Parameters:
        listener - Event Listener of type SerialPortListener
        mask - SerialPort.MASK_* representing the event mask to listen upon
        Throws:
        SerialPortException - if exception occurred
        See Also:
        setEventsMask(int mask)
      • removeEventListener

        public boolean removeEventListener()
                                    throws SerialPortException
        Delete event listener. Mask is set to 0. So at the next addition of event handler you shall set required event mask again
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred
      • closePort

        public boolean closePort()
                          throws SerialPortException
        Close port. This method deletes event listener first, then closes the port
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException - if exception occurred