Package purejavacomm

Class PureJavaSerialPort


  • public class PureJavaSerialPort
    extends SerialPort
    • Method Detail

      • getBaudRate

        public int getBaudRate()
        Description copied from class: SerialPort
        Returns the currently configured baud rate.
        Specified by:
        getBaudRate in class SerialPort
        Returns:
        The currently configured baud rate.
      • getDataBits

        public int getDataBits()
        Description copied from class: SerialPort
        Returns the currently configured number of data bits.
        Specified by:
        getDataBits in class SerialPort
        Returns:
        The currently configured number of data bits.
      • getFlowControlMode

        public int getFlowControlMode()
        Description copied from class: SerialPort
        Returns the currently configured flow control mode.
        Specified by:
        getFlowControlMode in class SerialPort
        Returns:
        The currently configured flow control mode.
      • getParity

        public int getParity()
        Description copied from class: SerialPort
        Returns the currently configured parity setting.
        Specified by:
        getParity in class SerialPort
        Returns:
        The currently configured parity setting.
      • getStopBits

        public int getStopBits()
        Description copied from class: SerialPort
        Returns the currently defined stop bits.
        Specified by:
        getStopBits in class SerialPort
        Returns:
        The currently defined stop bits.
      • isCD

        public boolean isCD()
        Description copied from class: SerialPort
        Returns the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.
        Specified by:
        isCD in class SerialPort
        Returns:
        The state of the CD (Carrier Detect) bit.
      • isCTS

        public boolean isCTS()
        Description copied from class: SerialPort
        Returns the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.
        Specified by:
        isCTS in class SerialPort
        Returns:
        The state of the CTS (Clear To Send) bit.
      • isDSR

        public boolean isDSR()
        Description copied from class: SerialPort
        Returns the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.
        Specified by:
        isDSR in class SerialPort
        Returns:
        The state of the DSR (Data Set Ready) bit.
      • isDTR

        public boolean isDTR()
        Description copied from class: SerialPort
        Returns the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
        Specified by:
        isDTR in class SerialPort
        Returns:
        The state of the DTR (Data Terminal Ready) bit.
      • isRI

        public boolean isRI()
        Description copied from class: SerialPort
        Returns the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.
        Specified by:
        isRI in class SerialPort
        Returns:
        The state of the RI (Ring Indicator) bit.
      • isRTS

        public boolean isRTS()
        Description copied from class: SerialPort
        Returns the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
        Specified by:
        isRTS in class SerialPort
        Returns:
        The state of the RTS (Request To Send) bit.
      • notifyOnBreakInterrupt

        public void notifyOnBreakInterrupt​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when there is a break interrupt on the line.
        Specified by:
        notifyOnBreakInterrupt in class SerialPort
      • notifyOnCTS

        public void notifyOnCTS​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when the CTS (Clear To Send) bit changes.
        Specified by:
        notifyOnCTS in class SerialPort
      • notifyOnCarrierDetect

        public void notifyOnCarrierDetect​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when the CD (Carrier Detect) bit changes.
        Specified by:
        notifyOnCarrierDetect in class SerialPort
      • notifyOnDSR

        public void notifyOnDSR​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes.

        This notification is hardware dependent and may not be supported by all implementations.

        Specified by:
        notifyOnDSR in class SerialPort
      • notifyOnDataAvailable

        public void notifyOnDataAvailable​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when input data is available.
        Specified by:
        notifyOnDataAvailable in class SerialPort
      • notifyOnFramingError

        public void notifyOnFramingError​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when there is a framing error.
        Specified by:
        notifyOnFramingError in class SerialPort
      • notifyOnOutputEmpty

        public void notifyOnOutputEmpty​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when the output buffer is empty.
        Specified by:
        notifyOnOutputEmpty in class SerialPort
      • notifyOnOverrunError

        public void notifyOnOverrunError​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when there is an overrun error.
        Specified by:
        notifyOnOverrunError in class SerialPort
      • notifyOnParityError

        public void notifyOnParityError​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when there is a parity error.
        Specified by:
        notifyOnParityError in class SerialPort
      • notifyOnRingIndicator

        public void notifyOnRingIndicator​(boolean x)
        Description copied from class: SerialPort
        Expresses interest in receiving notification when the RI (Ring Indicator) bit changes.
        Specified by:
        notifyOnRingIndicator in class SerialPort
      • removeEventListener

        public void removeEventListener()
        Description copied from class: SerialPort
        Deregisters event listener registered using addEventListener .

        This is done automatically when the port is closed.

        Specified by:
        removeEventListener in class SerialPort
      • sendBreak

        public void sendBreak​(int duration)
        Description copied from class: SerialPort
        Sends a break of duration milliseconds duration.
        Specified by:
        sendBreak in class SerialPort
        Parameters:
        duration - The break duration in milliseconds.
      • setDTR

        public void setDTR​(boolean x)
        Description copied from class: SerialPort
        Sets or clears the DTR (Data Terminal Ready) signal, if supported by the underlying implementation.
        Specified by:
        setDTR in class SerialPort
      • setRTS

        public void setRTS​(boolean x)
        Description copied from class: SerialPort
        Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
        Specified by:
        setRTS in class SerialPort
      • getInputBufferSize

        public int getInputBufferSize()
        Description copied from class: CommPort
        Returns the input buffer size in bytes.
        Specified by:
        getInputBufferSize in class CommPort
        Returns:
        The input buffer size in bytes.
      • getOutputBufferSize

        public int getOutputBufferSize()
        Description copied from class: CommPort
        Returns the output buffer size in bytes.
        Specified by:
        getOutputBufferSize in class CommPort
        Returns:
        The output buffer size in bytes.
      • getNativeFileDescriptor

        public int getNativeFileDescriptor()
        Gets the native file descriptor used by this port.

        The file descriptor can be used in calls to JTermios functions. This maybe useful in extreme cases where performance is more important than convenience, for example using JTermios.read(...) instead of SerialPort.getInputStream().read(...).

        Note that mixing direct JTermios read/write calls with SerialPort stream read/write calls is at best fragile and likely to fail, which also implies that when using JTermios directly then configuring the port, especially termios.cc[VMIN] and termios.cc[VTIME] is the users responsibility.

        Below is a sketch of minimum necessary to perform a read using raw JTermios functionality.

                        // import the JTermios functionality like this
                        import jtermios.*;
                        import static jtermios.JTermios.*;
         
                        SerialPort port = ...;
         
                        // cast the port to PureJavaSerialPort to get access to getNativeFileDescriptor
                        int FD = ((PureJavaSerialPort) port).getNativeFileDescriptor();
         
                        // timeout and threshold values
                        int messageLength = 25; // bytes
                        int timeout = 200; // msec
         
                        // to initialize timeout and threshold first read current termios
                        Termios termios = new Termios();
         
                        if (0 != tcgetattr(FD, termios))
                                errorHandling();
         
                        // then set VTIME and VMIN, note VTIME in 1/10th of sec and both max 255
                        termios.c_cc[VTIME] = (byte) ((timeout+99) / 100);
                        termios.c_cc[VMIN] = (byte) messageLength;
         
                        // update termios
                        if (0 != tcsetattr(FD, TCSANOW, termios))
                                errorHandling();
         
                        ...
                        // allocate read buffer
                        byte[] readBuffer = new byte[messageLength];
                ...
         
                        // then perform raw read, not this may block indefinitely
                        int n = read(FD, readBuffer, messageLength);
                        if (n < 0)
                                errorHandling();
         
        Returns:
        the native OS file descriptor as int
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Description copied from class: 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 behavior of the input stream returned by getInputStream depends on combination of the threshold and timeout values. The behaviors are described in the table below.

        threshold timeout read buffer size read behaviour
        state value state value
        disabled - disabled - n bytes block until minimum one byte of data is available
        enabled m bytes disabled - n bytes block until min(m,n) bytes are available
        disabled - enabled x msec n bytes block for x msec or until any data is available
        enabled m bytes enabled x msec n bytes block for x msec or until min(m,n) bytes are available

        Framing errors may cause the Timeout and Threshold trigger early and to complete the read 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. Returns: InputStream object that can be used to read from the port Throws: java.io.IOException - if an I/O error occurred.

        Timeout is interpreted as inter character timeout, in other words the timeout will not occur as long as the pause before the first character or between characters is shorter that the timeout value.

        Specified by:
        getInputStream in class SerialPort
        Returns:
        An input stream, or null if the port is unidirectional and doesn't support receiving data.
        Throws:
        IOException
      • getReceiveFramingByte

        public int getReceiveFramingByte()
        Description copied from class: CommPort
        Returns the current byte used for receive framing.
        Specified by:
        getReceiveFramingByte in class CommPort
        Returns:
        The current byte used for receive framing.
      • getReceiveThreshold

        public int getReceiveThreshold()
        Description copied from class: CommPort
        Returns the integer value of the receive threshold.
        Specified by:
        getReceiveThreshold in class CommPort
        Returns:
        The integer value of the receive threshold.
      • getReceiveTimeout

        public int getReceiveTimeout()
        Description copied from class: CommPort
        Returns the integer value of the receive timeout. If the receive timeout is disabled, then the value returned is meaningless.
        Specified by:
        getReceiveTimeout in class CommPort
        Returns:
        The integer value of the receive timeout.
      • isReceiveFramingEnabled

        public boolean isReceiveFramingEnabled()
        Description copied from class: CommPort
        Returns true if receive framing is enabled.
        Specified by:
        isReceiveFramingEnabled in class CommPort
        Returns:
        true if receive framing is enabled.
      • isReceiveThresholdEnabled

        public boolean isReceiveThresholdEnabled()
        Description copied from class: CommPort
        Returns true if receive threshold is enabled.
        Specified by:
        isReceiveThresholdEnabled in class CommPort
        Returns:
        true if receive threshold is enabled.
      • isReceiveTimeoutEnabled

        public boolean isReceiveTimeoutEnabled()
        Description copied from class: CommPort
        Returns true if receive timeout is enabled.
        Specified by:
        isReceiveTimeoutEnabled in class CommPort
        Returns:
        true if receive timeout is enabled.
      • setInputBufferSize

        public void setInputBufferSize​(int arg0)
        Description copied from class: CommPort
        Sets the input buffer size.
        Specified by:
        setInputBufferSize in class CommPort
      • setOutputBufferSize

        public void setOutputBufferSize​(int arg0)
        Description copied from class: CommPort
        Sets the output buffer size.
        Specified by:
        setOutputBufferSize in class CommPort
      • close

        public void close()
        Description copied from class: CommPort
        Closes the communications port.
        Overrides:
        close in class CommPort
      • isInternalThreadRunning

        public boolean isInternalThreadRunning()
        This is not part of the PureJavaComm API, this is purely for testing, do not depend on this