Package jssc

Class SerialPort


  • public class SerialPort
    extends java.lang.Object
    • Constructor Detail

      • SerialPort

        public SerialPort​(java.lang.String portName)
    • Method Detail

      • getPortName

        public java.lang.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
      • 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
      • 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
        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
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false.
        Throws:
        SerialPortException
      • 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"
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException
      • 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
      • setRTS

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

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

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

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

        public boolean writeString​(java.lang.String string)
                            throws SerialPortException
        Write String to port
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException
        Since:
        0.8
      • writeString

        public boolean writeString​(java.lang.String string,
                                   java.lang.String charsetName)
                            throws SerialPortException,
                                   java.io.UnsupportedEncodingException
        Write String to port
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException
        java.io.UnsupportedEncodingException
        Since:
        2.8.0
      • writeInt

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

        public boolean writeIntArray​(int[] buffer)
                              throws SerialPortException
        Write int array (in range from 0 to 255 (0x00 - 0xFF)) to port
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException
        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
      • readString

        public java.lang.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
        Since:
        0.8
      • readHexString

        public java.lang.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
        Since:
        0.8
      • readHexString

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

        public java.lang.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
        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
        Since:
        0.8
      • readHexString

        public java.lang.String readHexString​(int byteCount,
                                              java.lang.String separator,
                                              int timeout)
                                       throws SerialPortException,
                                              SerialPortTimeoutException
        Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
        Parameters:
        byteCount - count of bytes for reading
        timeout - timeout in milliseconds
        Returns:
        byte array with "byteCount" length converted to Hexadecimal String
        Throws:
        SerialPortException
        SerialPortTimeoutException
        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
        Since:
        0.8
      • readString

        public java.lang.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
        Since:
        0.8
      • readHexString

        public java.lang.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
        Since:
        0.8
      • readHexString

        public java.lang.String readHexString​(java.lang.String separator)
                                       throws SerialPortException
        Read all available bytes from port like a Hex String with setted separator
        Returns:
        If input buffer is empty null will be returned, else byte array with all data from port converted to Hex String
        Throws:
        SerialPortException
        Since:
        0.8
      • readHexStringArray

        public java.lang.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
        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
        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
        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
        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);
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException
        Since:
        0.8
      • sendBreak

        public boolean sendBreak​(int duration)
                          throws SerialPortException
        Send Break singnal for setted duration
        Parameters:
        duration - duration of Break signal
        Returns:
        If the operation is successfully completed, the method returns true, otherwise false
        Throws:
        SerialPortException
        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
      • 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
        Throws:
        SerialPortException
      • 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"
        Throws:
        SerialPortException
        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
      • 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