Package jssc
Class SerialPort
- java.lang.Object
-
- jssc.SerialPort
-
public class SerialPort extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static intBAUDRATE_110static intBAUDRATE_115200static intBAUDRATE_1200static intBAUDRATE_128000static intBAUDRATE_14400static intBAUDRATE_19200static intBAUDRATE_256000static intBAUDRATE_300static intBAUDRATE_38400static intBAUDRATE_4800static intBAUDRATE_57600static intBAUDRATE_600static intBAUDRATE_9600static intDATABITS_5static intDATABITS_6static intDATABITS_7static intDATABITS_8static intERROR_FRAMEstatic intERROR_OVERRUNstatic intERROR_PARITYstatic intFLOWCONTROL_NONEstatic intFLOWCONTROL_RTSCTS_INstatic intFLOWCONTROL_RTSCTS_OUTstatic intFLOWCONTROL_XONXOFF_INstatic intFLOWCONTROL_XONXOFF_OUTstatic intMASK_BREAKstatic intMASK_CTSstatic intMASK_DSRstatic intMASK_ERRstatic intMASK_RINGstatic intMASK_RLSDstatic intMASK_RXCHARstatic intMASK_RXFLAGstatic intMASK_TXEMPTYstatic intPARITY_EVENstatic intPARITY_MARKstatic intPARITY_NONEstatic intPARITY_ODDstatic intPARITY_SPACEstatic intPURGE_RXABORTstatic intPURGE_RXCLEARstatic intPURGE_TXABORTstatic intPURGE_TXCLEARstatic intSTOPBITS_1static intSTOPBITS_1_5static intSTOPBITS_2
-
Constructor Summary
Constructors Constructor Description SerialPort(java.lang.String portName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEventListener(SerialPortEventListener listener)Add event listener.voidaddEventListener(SerialPortEventListener listener, int mask)Add event listener.booleanclosePort()Close port.intgetEventsMask()Getting events mask for the portintgetFlowControlMode()Get flow control modeintgetInputBufferBytesCount()Get count of bytes in input bufferint[]getLinesStatus()Getting lines status.intgetOutputBufferBytesCount()Get count of bytes in output bufferjava.lang.StringgetPortName()Getting port name under operationbooleanisCTS()Get state of CTS linebooleanisDSR()Get state of DSR linebooleanisOpened()Getting port statebooleanisRING()Get state of RING linebooleanisRLSD()Get state of RLSD linebooleanopenPort()Port opening
Note: If port busy TYPE_PORT_BUSY exception will be thrown.booleanpurgePort(int flags)Purge of input and output buffer.byte[]readBytes()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 portjava.lang.StringreadHexString()Read all available bytes from port like a Hex Stringjava.lang.StringreadHexString(int byteCount)Read Hex string from port (example: FF 0A FF).java.lang.StringreadHexString(int byteCount, int timeout)Read Hex string from port (example: FF 0A FF).java.lang.StringreadHexString(int byteCount, java.lang.String separator)Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF)java.lang.StringreadHexString(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.StringreadHexString(java.lang.String separator)Read all available bytes from port like a Hex String with setted separatorjava.lang.String[]readHexStringArray()Read all available bytes from port like a Hex String arrayjava.lang.String[]readHexStringArray(int byteCount)Read Hex String array from portjava.lang.String[]readHexStringArray(int byteCount, int timeout)Read Hex String array from portint[]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 portint[]readIntArray(int byteCount, int timeout)Read int array from portjava.lang.StringreadString()Read all available bytes from port like a Stringjava.lang.StringreadString(int byteCount)Read string from portjava.lang.StringreadString(int byteCount, int timeout)Read string from portbooleanremoveEventListener()Delete event listener.booleansendBreak(int duration)Send Break singnal for setted durationbooleansetDTR(boolean enabled)Change DTR line state.booleansetEventsMask(int mask)Set events mask.booleansetFlowControlMode(int mask)Set flow control mode.booleansetParams(int baudRate, int dataBits, int stopBits, int parity)Setting the parameters of port.booleansetParams(int baudRate, int dataBits, int stopBits, int parity, boolean setRTS, boolean setDTR)Setting the parameters of portbooleansetRTS(boolean enabled)Change RTS line state.booleanwriteByte(byte singleByte)Write single byte to portbooleanwriteBytes(byte[] buffer)Write byte array to portbooleanwriteInt(int singleInt)Write int value (in range from 0 to 255 (0x00 - 0xFF)) to portbooleanwriteIntArray(int[] buffer)Write int array (in range from 0 to 255 (0x00 - 0xFF)) to portbooleanwriteString(java.lang.String string)Write String to portbooleanwriteString(java.lang.String string, java.lang.String charsetName)Write String to port
-
-
-
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
-
-
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 SerialPortExceptionPort 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 SerialPortExceptionSetting 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 SerialPortExceptionSetting 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
public boolean purgePort(int flags) throws SerialPortExceptionPurge 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 SerialPortExceptionSet 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 SerialPortExceptionGetting 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 SerialPortExceptionChange 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 SerialPortExceptionChange 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 SerialPortExceptionWrite 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 SerialPortExceptionWrite 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 SerialPortExceptionWrite 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.UnsupportedEncodingExceptionWrite String to port- Returns:
- If the operation is successfully completed, the method returns true, otherwise false
- Throws:
SerialPortExceptionjava.io.UnsupportedEncodingException- Since:
- 2.8.0
-
writeInt
public boolean writeInt(int singleInt) throws SerialPortExceptionWrite 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 SerialPortExceptionWrite 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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, SerialPortTimeoutExceptionRead byte array from port- Parameters:
byteCount- count of bytes for readingtimeout- timeout in milliseconds- Returns:
- byte array with "byteCount" length
- Throws:
SerialPortExceptionSerialPortTimeoutException- Since:
- 2.0
-
readString
public java.lang.String readString(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutExceptionRead string from port- Parameters:
byteCount- count of bytes for readingtimeout- timeout in milliseconds- Returns:
- byte array with "byteCount" length converted to String
- Throws:
SerialPortExceptionSerialPortTimeoutException- Since:
- 2.0
-
readHexString
public java.lang.String readHexString(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutExceptionRead 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:
SerialPortExceptionSerialPortTimeoutException- Since:
- 2.0
-
readHexString
public java.lang.String readHexString(int byteCount, java.lang.String separator, int timeout) throws SerialPortException, SerialPortTimeoutExceptionRead 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:
SerialPortExceptionSerialPortTimeoutException- Since:
- 2.0
-
readHexStringArray
public java.lang.String[] readHexStringArray(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutExceptionRead 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:
SerialPortExceptionSerialPortTimeoutException- Since:
- 2.0
-
readIntArray
public int[] readIntArray(int byteCount, int timeout) throws SerialPortException, SerialPortTimeoutExceptionRead 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:
SerialPortExceptionSerialPortTimeoutException- Since:
- 2.0
-
readBytes
public byte[] readBytes() throws SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionRead 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 SerialPortExceptionGet 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 SerialPortExceptionGet 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 SerialPortExceptionSet 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 SerialPortExceptionGet flow control mode- Returns:
- Mask of setted flow control mode
- Throws:
SerialPortException- Since:
- 0.8
-
sendBreak
public boolean sendBreak(int duration) throws SerialPortExceptionSend 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 SerialPortExceptionGetting 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 SerialPortExceptionGet state of CTS line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
isDSR
public boolean isDSR() throws SerialPortExceptionGet state of DSR line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
isRING
public boolean isRING() throws SerialPortExceptionGet state of RING line- Returns:
- If line is active, method returns true, otherwise false
- Throws:
SerialPortException
-
isRLSD
public boolean isRLSD() throws SerialPortExceptionGet 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 SerialPortExceptionDelete 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 SerialPortExceptionClose 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
-
-