Package jssc
Class SerialPort
java.lang.Object
jssc.SerialPort
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addEventListener
(SerialPortEventListener listener) Add event listener.void
addEventListener
(SerialPortEventListener listener, int mask) Add event listener.boolean
Close port.int
Getting events mask for the portint
Get flow control modeint
Get count of bytes in input bufferint[]
Getting lines status.int
Get count of bytes in output bufferGetting port name under operationboolean
isCTS()
Get state of CTS lineboolean
isDSR()
Get state of DSR lineboolean
isOpened()
Getting port stateboolean
isRING()
Get state of RING lineboolean
isRLSD()
Get state of RLSD lineboolean
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[]
Read all available bytes from port like a byte arraybyte[]
readBytes
(int byteCount) Read byte array from portbyte[]
readBytes
(int byteCount, int timeout) Read byte array from portRead all available bytes from port like a Hex StringreadHexString
(int byteCount) Read Hex string from port (example: FF 0A FF).readHexString
(int byteCount, int timeout) Read Hex string from port (example: FF 0A FF).readHexString
(int byteCount, String separator) Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)readHexString
(int byteCount, String separator, int timeout) Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)readHexString
(String separator) Read all available bytes from port like a Hex String with setted separatorString[]
Read all available bytes from port like a Hex String arrayString[]
readHexStringArray
(int byteCount) Read Hex String array from portString[]
readHexStringArray
(int byteCount, int timeout) Read Hex String array from portint[]
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 portint[]
readIntArray
(int byteCount, int timeout) Read int array from portRead all available bytes from port like a StringreadString
(int byteCount) Read string from portreadString
(int byteCount, int timeout) Read string from portboolean
Delete event listener.boolean
sendBreak
(int duration) Send Break singnal for setted durationboolean
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 portboolean
setRTS
(boolean enabled) Change RTS line state.boolean
writeByte
(byte singleByte) Write single byte to portboolean
writeBytes
(byte[] buffer) Write byte array to portboolean
writeInt
(int singleInt) Write int value (in range from 0 to 255 (0x00 - 0xFF)) to portboolean
writeIntArray
(int[] buffer) Write int array (in range from 0 to 255 (0x00 - 0xFF)) to portboolean
writeString
(String string) Write String to portboolean
writeString
(String string, String charsetName) Write String to port
-
Field Details
-
BAUDRATE_110
public static final int BAUDRATE_110- See Also:
-
BAUDRATE_300
public static final int BAUDRATE_300- See Also:
-
BAUDRATE_600
public static final int BAUDRATE_600- See Also:
-
BAUDRATE_1200
public static final int BAUDRATE_1200- See Also:
-
BAUDRATE_4800
public static final int BAUDRATE_4800- See Also:
-
BAUDRATE_9600
public static final int BAUDRATE_9600- See Also:
-
BAUDRATE_14400
public static final int BAUDRATE_14400- See Also:
-
BAUDRATE_19200
public static final int BAUDRATE_19200- See Also:
-
BAUDRATE_38400
public static final int BAUDRATE_38400- See Also:
-
BAUDRATE_57600
public static final int BAUDRATE_57600- See Also:
-
BAUDRATE_115200
public static final int BAUDRATE_115200- See Also:
-
BAUDRATE_128000
public static final int BAUDRATE_128000- See Also:
-
BAUDRATE_256000
public static final int BAUDRATE_256000- See Also:
-
DATABITS_5
public static final int DATABITS_5- See Also:
-
DATABITS_6
public static final int DATABITS_6- See Also:
-
DATABITS_7
public static final int DATABITS_7- See Also:
-
DATABITS_8
public static final int DATABITS_8- See Also:
-
STOPBITS_1
public static final int STOPBITS_1- See Also:
-
STOPBITS_2
public static final int STOPBITS_2- See Also:
-
STOPBITS_1_5
public static final int STOPBITS_1_5- See Also:
-
PARITY_NONE
public static final int PARITY_NONE- See Also:
-
PARITY_ODD
public static final int PARITY_ODD- See Also:
-
PARITY_EVEN
public static final int PARITY_EVEN- See Also:
-
PARITY_MARK
public static final int PARITY_MARK- See Also:
-
PARITY_SPACE
public static final int PARITY_SPACE- See Also:
-
PURGE_RXABORT
public static final int PURGE_RXABORT- See Also:
-
PURGE_RXCLEAR
public static final int PURGE_RXCLEAR- See Also:
-
PURGE_TXABORT
public static final int PURGE_TXABORT- See Also:
-
PURGE_TXCLEAR
public static final int PURGE_TXCLEAR- See Also:
-
MASK_RXCHAR
public static final int MASK_RXCHAR- See Also:
-
MASK_RXFLAG
public static final int MASK_RXFLAG- See Also:
-
MASK_TXEMPTY
public static final int MASK_TXEMPTY- See Also:
-
MASK_CTS
public static final int MASK_CTS- See Also:
-
MASK_DSR
public static final int MASK_DSR- See Also:
-
MASK_RLSD
public static final int MASK_RLSD- See Also:
-
MASK_BREAK
public static final int MASK_BREAK- See Also:
-
MASK_ERR
public static final int MASK_ERR- See Also:
-
MASK_RING
public static final int MASK_RING- See Also:
-
FLOWCONTROL_NONE
public static final int FLOWCONTROL_NONE- See Also:
-
FLOWCONTROL_RTSCTS_IN
public static final int FLOWCONTROL_RTSCTS_IN- See Also:
-
FLOWCONTROL_RTSCTS_OUT
public static final int FLOWCONTROL_RTSCTS_OUT- See Also:
-
FLOWCONTROL_XONXOFF_IN
public static final int FLOWCONTROL_XONXOFF_IN- See Also:
-
FLOWCONTROL_XONXOFF_OUT
public static final int FLOWCONTROL_XONXOFF_OUT- See Also:
-
ERROR_FRAME
public static final int ERROR_FRAME- See Also:
-
ERROR_OVERRUN
public static final int ERROR_OVERRUN- See Also:
-
ERROR_PARITY
public static final int ERROR_PARITY- See Also:
-
-
Constructor Details
-
SerialPort
-
-
Method Details
-
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
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 ratedataBits
- number of data bitsstopBits
- number of stop bitsparity
- 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 ratedataBits
- number of data bitsstopBits
- number of stop bitsparity
- paritysetRTS
- 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
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
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
Getting events mask for the port- Returns:
- Method returns events mask as int type variable. This variable is an additive value
- Throws:
SerialPortException
-
setRTS
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
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
Write byte array to port- Returns:
- If the operation is successfully completed, the method returns true, otherwise false
- Throws:
SerialPortException
-
writeByte
Write single byte to port- Returns:
- If the operation is successfully completed, the method returns true, otherwise false
- Throws:
SerialPortException
- Since:
- 0.8
-
writeString
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(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException Write String to port- Returns:
- If the operation is successfully completed, the method returns true, otherwise false
- Throws:
SerialPortException
UnsupportedEncodingException
- Since:
- 2.8.0
-
writeInt
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
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
Read byte array from port- Parameters:
byteCount
- count of bytes for reading- Returns:
- byte array with "byteCount" length
- Throws:
SerialPortException
-
readString
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
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
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
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
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 readingtimeout
- timeout in milliseconds- Returns:
- byte array with "byteCount" length
- Throws:
SerialPortException
SerialPortTimeoutException
- Since:
- 2.0
-
readString
public String readString(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException Read string from port- Parameters:
byteCount
- count of bytes for readingtimeout
- timeout in milliseconds- Returns:
- byte array with "byteCount" length converted to String
- Throws:
SerialPortException
SerialPortTimeoutException
- Since:
- 2.0
-
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 readingtimeout
- timeout in milliseconds- Returns:
- byte array with "byteCount" length converted to Hexadecimal String
- Throws:
SerialPortException
SerialPortTimeoutException
- Since:
- 2.0
-
readHexString
public String readHexString(int byteCount, 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 readingtimeout
- timeout in milliseconds- Returns:
- byte array with "byteCount" length converted to Hexadecimal String
- Throws:
SerialPortException
SerialPortTimeoutException
- Since:
- 2.0
-
readHexStringArray
public String[] readHexStringArray(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutException Read Hex String array from port- Parameters:
byteCount
- count of bytes for readingtimeout
- 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 readingtimeout
- timeout in milliseconds- Returns:
- int array with values in range from 0 to 255
- Throws:
SerialPortException
SerialPortTimeoutException
- Since:
- 2.0
-
readBytes
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
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
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
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
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
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
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
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
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
Get flow control mode- Returns:
- Mask of setted flow control mode
- Throws:
SerialPortException
- Since:
- 0.8
-
sendBreak
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
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
Get state of CTS line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
isDSR
Get state of DSR line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
isRING
Get state of RING line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
isRLSD
Get state of RLSD line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
addEventListener
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
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:
-
removeEventListener
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
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
-