jssc
Class SerialPort

java.lang.Object
  extended by jssc.SerialPort

public class SerialPort
extends java.lang.Object


Field Summary
static int BAUDRATE_110
           
static int BAUDRATE_115200
           
static int BAUDRATE_1200
           
static int BAUDRATE_128000
           
static int BAUDRATE_14400
           
static int BAUDRATE_19200
           
static int BAUDRATE_256000
           
static int BAUDRATE_300
           
static int BAUDRATE_38400
           
static int BAUDRATE_4800
           
static int BAUDRATE_57600
           
static int BAUDRATE_600
           
static int BAUDRATE_9600
           
static int DATABITS_5
           
static int DATABITS_6
           
static int DATABITS_7
           
static int DATABITS_8
           
static int ERROR_FRAME
           
static int ERROR_OVERRUN
           
static int ERROR_PARITY
           
static int FLOWCONTROL_NONE
           
static int FLOWCONTROL_RTSCTS_IN
           
static int FLOWCONTROL_RTSCTS_OUT
           
static int FLOWCONTROL_XONXOFF_IN
           
static int FLOWCONTROL_XONXOFF_OUT
           
static int MASK_BREAK
           
static int MASK_CTS
           
static int MASK_DSR
           
static int MASK_ERR
           
static int MASK_RING
           
static int MASK_RLSD
           
static int MASK_RXCHAR
           
static int MASK_RXFLAG
           
static int MASK_TXEMPTY
           
static int PARITY_EVEN
           
static int PARITY_MARK
           
static int PARITY_NONE
           
static int PARITY_ODD
           
static int PARITY_SPACE
           
static int PURGE_RXABORT
           
static int PURGE_RXCLEAR
           
static int PURGE_TXABORT
           
static int PURGE_TXCLEAR
           
static int STOPBITS_1
           
static int STOPBITS_1_5
           
static int STOPBITS_2
           
 
Constructor Summary
SerialPort(java.lang.String portName)
           
 
Method Summary
 void addEventListener(SerialPortEventListener listener)
          Add event listener.
 void addEventListener(SerialPortEventListener listener, int mask)
          Add event listener.
 boolean closePort()
          Close port.
 int getEventsMask()
          Getting events mask for the port
 int getFlowControlMode()
          Get flow control mode
 int getInputBufferBytesCount()
          Get count of bytes in input buffer
 int[] getLinesStatus()
          Getting lines status.
 int getOutputBufferBytesCount()
          Get count of bytes in output buffer
 java.lang.String getPortName()
          Getting port name under operation
 boolean isCTS()
          Get state of CTS line
 boolean isDSR()
          Get state of DSR line
 boolean isOpened()
          Getting port state
 boolean isRING()
          Get state of RING line
 boolean isRLSD()
          Get state of RLSD line
 boolean openPort()
          Port opening

Note: If port busy TYPE_PORT_BUSY exception will be thrown.
 boolean purgePort(int flags)
          Purge of input and output buffer.
 byte[] readBytes()
          Read all available bytes from port like a byte array
 byte[] readBytes(int byteCount)
          Read byte array from port
 byte[] readBytes(int byteCount, int timeout)
          Read byte array from port
 java.lang.String readHexString()
          Read all available bytes from port like a Hex String
 java.lang.String readHexString(int byteCount)
          Read Hex string from port (example: FF 0A FF).
 java.lang.String readHexString(int byteCount, int timeout)
          Read Hex string from port (example: FF 0A FF).
 java.lang.String readHexString(int byteCount, java.lang.String separator)
          Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
 java.lang.String readHexString(int byteCount, java.lang.String separator, int timeout)
          Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)
 java.lang.String readHexString(java.lang.String separator)
          Read all available bytes from port like a Hex String with setted separator
 java.lang.String[] readHexStringArray()
          Read all available bytes from port like a Hex String array
 java.lang.String[] readHexStringArray(int byteCount)
          Read Hex String array from port
 java.lang.String[] readHexStringArray(int byteCount, int timeout)
          Read Hex String array from port
 int[] readIntArray()
          Read all available bytes from port like a int array (values in range from 0 to 255)
 int[] readIntArray(int byteCount)
          Read int array from port
 int[] readIntArray(int byteCount, int timeout)
          Read int array from port
 java.lang.String readString()
          Read all available bytes from port like a String
 java.lang.String readString(int byteCount)
          Read string from port
 java.lang.String readString(int byteCount, int timeout)
          Read string from port
 boolean removeEventListener()
          Delete event listener.
 boolean sendBreak(int duration)
          Send Break singnal for setted duration
 boolean setDTR(boolean enabled)
          Change DTR line state.
 boolean setEventsMask(int mask)
          Set events mask.
 boolean setFlowControlMode(int mask)
          Set flow control mode.
 boolean setParams(int baudRate, int dataBits, int stopBits, int parity)
          Setting the parameters of port.
 boolean setParams(int baudRate, int dataBits, int stopBits, int parity, boolean setRTS, boolean setDTR)
          Setting the parameters of port
 boolean setRTS(boolean enabled)
          Change RTS line state.
 boolean writeByte(byte singleByte)
          Write single byte to port
 boolean writeBytes(byte[] buffer)
          Write byte array to port
 boolean writeInt(int singleInt)
          Write int value (in range from 0 to 255 (0x00 - 0xFF)) to port
 boolean writeIntArray(int[] buffer)
          Write int array (in range from 0 to 255 (0x00 - 0xFF)) to port
 boolean writeString(java.lang.String string)
          Write String to port
 boolean writeString(java.lang.String string, java.lang.String charsetName)
          Write String to port
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BAUDRATE_110

public static final int BAUDRATE_110
See Also:
Constant Field Values

BAUDRATE_300

public static final int BAUDRATE_300
See Also:
Constant Field Values

BAUDRATE_600

public static final int BAUDRATE_600
See Also:
Constant Field Values

BAUDRATE_1200

public static final int BAUDRATE_1200
See Also:
Constant Field Values

BAUDRATE_4800

public static final int BAUDRATE_4800
See Also:
Constant Field Values

BAUDRATE_9600

public static final int BAUDRATE_9600
See Also:
Constant Field Values

BAUDRATE_14400

public static final int BAUDRATE_14400
See Also:
Constant Field Values

BAUDRATE_19200

public static final int BAUDRATE_19200
See Also:
Constant Field Values

BAUDRATE_38400

public static final int BAUDRATE_38400
See Also:
Constant Field Values

BAUDRATE_57600

public static final int BAUDRATE_57600
See Also:
Constant Field Values

BAUDRATE_115200

public static final int BAUDRATE_115200
See Also:
Constant Field Values

BAUDRATE_128000

public static final int BAUDRATE_128000
See Also:
Constant Field Values

BAUDRATE_256000

public static final int BAUDRATE_256000
See Also:
Constant Field Values

DATABITS_5

public static final int DATABITS_5
See Also:
Constant Field Values

DATABITS_6

public static final int DATABITS_6
See Also:
Constant Field Values

DATABITS_7

public static final int DATABITS_7
See Also:
Constant Field Values

DATABITS_8

public static final int DATABITS_8
See Also:
Constant Field Values

STOPBITS_1

public static final int STOPBITS_1
See Also:
Constant Field Values

STOPBITS_2

public static final int STOPBITS_2
See Also:
Constant Field Values

STOPBITS_1_5

public static final int STOPBITS_1_5
See Also:
Constant Field Values

PARITY_NONE

public static final int PARITY_NONE
See Also:
Constant Field Values

PARITY_ODD

public static final int PARITY_ODD
See Also:
Constant Field Values

PARITY_EVEN

public static final int PARITY_EVEN
See Also:
Constant Field Values

PARITY_MARK

public static final int PARITY_MARK
See Also:
Constant Field Values

PARITY_SPACE

public static final int PARITY_SPACE
See Also:
Constant Field Values

PURGE_RXABORT

public static final int PURGE_RXABORT
See Also:
Constant Field Values

PURGE_RXCLEAR

public static final int PURGE_RXCLEAR
See Also:
Constant Field Values

PURGE_TXABORT

public static final int PURGE_TXABORT
See Also:
Constant Field Values

PURGE_TXCLEAR

public static final int PURGE_TXCLEAR
See Also:
Constant Field Values

MASK_RXCHAR

public static final int MASK_RXCHAR
See Also:
Constant Field Values

MASK_RXFLAG

public static final int MASK_RXFLAG
See Also:
Constant Field Values

MASK_TXEMPTY

public static final int MASK_TXEMPTY
See Also:
Constant Field Values

MASK_CTS

public static final int MASK_CTS
See Also:
Constant Field Values

MASK_DSR

public static final int MASK_DSR
See Also:
Constant Field Values

MASK_RLSD

public static final int MASK_RLSD
See Also:
Constant Field Values

MASK_BREAK

public static final int MASK_BREAK
See Also:
Constant Field Values

MASK_ERR

public static final int MASK_ERR
See Also:
Constant Field Values

MASK_RING

public static final int MASK_RING
See Also:
Constant Field Values

FLOWCONTROL_NONE

public static final int FLOWCONTROL_NONE
See Also:
Constant Field Values

FLOWCONTROL_RTSCTS_IN

public static final int FLOWCONTROL_RTSCTS_IN
See Also:
Constant Field Values

FLOWCONTROL_RTSCTS_OUT

public static final int FLOWCONTROL_RTSCTS_OUT
See Also:
Constant Field Values

FLOWCONTROL_XONXOFF_IN

public static final int FLOWCONTROL_XONXOFF_IN
See Also:
Constant Field Values

FLOWCONTROL_XONXOFF_OUT

public static final int FLOWCONTROL_XONXOFF_OUT
See Also:
Constant Field Values

ERROR_FRAME

public static final int ERROR_FRAME
See Also:
Constant Field Values

ERROR_OVERRUN

public static final int ERROR_OVERRUN
See Also:
Constant Field Values

ERROR_PARITY

public static final int ERROR_PARITY
See Also:
Constant Field Values
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

readBytes

public byte[] readBytes(int byteCount,
                        int timeout)
                 throws SerialPortException,
                        SerialPortTimeoutException
Read byte array from port

Parameters:
byteCount - count of bytes for reading
timeout - timeout in milliseconds
Returns:
byte array with "byteCount" length
Throws:
SerialPortException
SerialPortTimeoutException
Since:
2.0

readString

public java.lang.String readString(int byteCount,
                                   int timeout)
                            throws SerialPortException,
                                   SerialPortTimeoutException
Read string from port

Parameters:
byteCount - count of bytes for reading
timeout - timeout in milliseconds
Returns:
byte array with "byteCount" length converted to String
Throws:
SerialPortException
SerialPortTimeoutException
Since:
2.0

readHexString

public java.lang.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
SerialPortTimeoutException
Since:
2.0

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

readHexStringArray

public java.lang.String[] readHexStringArray(int byteCount,
                                             int timeout)
                                      throws SerialPortException,
                                             SerialPortTimeoutException
Read Hex String array from port

Parameters:
byteCount - count of bytes for reading
timeout - timeout in milliseconds
Returns:
String array with "byteCount" length and Hexadecimal String values
Throws:
SerialPortException
SerialPortTimeoutException
Since:
2.0

readIntArray

public int[] readIntArray(int byteCount,
                          int timeout)
                   throws SerialPortException,
                          SerialPortTimeoutException
Read int array from port

Parameters:
byteCount - count of bytes for reading
timeout - timeout in milliseconds
Returns:
int array with values in range from 0 to 255
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

getFlowControlMode

public int getFlowControlMode()
                       throws SerialPortException
Get flow control mode

Returns:
Mask of setted flow control mode
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

isCTS

public boolean isCTS()
              throws SerialPortException
Get state of CTS line

Returns:
If line is active, method returns true, otherwise false
Throws:
SerialPortException

isDSR

public boolean isDSR()
              throws SerialPortException
Get state of DSR line

Returns:
If line is active, method returns true, otherwise false
Throws:
SerialPortException

isRING

public boolean isRING()
               throws SerialPortException
Get state of RING line

Returns:
If line is active, method returns true, otherwise false
Throws:
SerialPortException

isRLSD

public boolean isRLSD()
               throws SerialPortException
Get state of RLSD line

Returns:
If line is active, method returns true, otherwise false
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