public class

SerialPort

extends Object
java.lang.Object
   ↳ com.digi.android.serial.SerialPort

Class Overview

Represents a serial port to communicate with.

To obtain an instance of the serial port and open it you have to use one of the following methods: SerialPortManager.openSerialPort(String), SerialPortManager.openSerialPort(String, int, int, int, int, int, int), or SerialPortManager.openSerialPort(String, SerialPortParameters).

Summary

Constants
int DATABITS_5 5 data bits: .DATABITS_5.
int DATABITS_6 6 data bits: .DATABITS_6.
int DATABITS_7 7 data bits: .DATABITS_7.
int DATABITS_8 8 data bits: .DATABITS_8.
int DEFAULT_READ_TIMEOUT Default read timeout: .DEFAULT_READ_TIMEOUT milliseconds.
int FLOWCONTROL_NONE No flow control: .FLOWCONTROL_NONE.
int FLOWCONTROL_RTSCTS_IN Hardware flow control (input): .FLOWCONTROL_RTSCTS_IN.
int FLOWCONTROL_RTSCTS_OUT Hardware flow control (output): .FLOWCONTROL_RTSCTS_OUT.
int FLOWCONTROL_XONXOFF_IN Xon/Xoff flow control (input): .FLOWCONTROL_XONXOFF_IN.
int FLOWCONTROL_XONXOFF_OUT Xon/Xoff flow control (output): .FLOWCONTROL_XONXOFF_OUT.
int PARITY_EVEN Parity even: .PARITY_EVEN.
int PARITY_MARK Parity mark: .PARITY_MARK.
int PARITY_NONE Parity none: .PARITY_NONE.
int PARITY_ODD Parity odd: .PARITY_ODD.
int PARITY_SPACE Parity space: .PARITY_SPACE.
int STOPBITS_1 1 stop bit: .STOPBITS_1.
int STOPBITS_1_5 1.5 stop bits: .STOPBITS_1_5.
int STOPBITS_2 2 stop bits: .STOPBITS_2.
Public Methods
void close()
Closes the communications port.
void disableReceiveThreshold()
Disables receive threshold.
void disableReceiveTimeout()
Disables receive timeout.
void enableReceiveThreshold(int thresh)
Enables receive threshold, if this feature is supported by the driver.
void enableReceiveTimeout(int time)
Enables receive timeout, if this feature is supported by the driver.
int getBaudBase()
Returns the baud base value.
int getBaudRate()
Returns the configured baud rate.
int getDataBits()
Returns the configured data bits.
int getDivisor()
Returns the divisor value.
byte getEndOfInputChar()
Returns the end of input character.
int getFlowControl()
Returns the configured flow control.
int getInputBufferSize()
Returns the input buffer size.
InputStream getInputStream()
Returns the connection interface input stream to read data from.
String getName()
Returns the serial port name.
int getOutputBufferSize()
Returns the output buffer size.
OutputStream getOutputStream()
Returns the connection interface output stream to write data to.
int getParity()
Returns the configured parity.
byte getParityErrorChar()
Returns the parity error character.
int getReceiveThreshold()
Returns the integer value of the receive threshold.
int getReceiveTimeout()
Returns the integer value of the receive timeout.
int getStopBits()
Returns the configured stop bits.
boolean isCD()
Returns the state of the CD line.
boolean isCTS()
Returns the state of the CTS line.
boolean isDSR()
Returns the state of the DSR line.
boolean isDTR()
Returns the state of the DTR line.
boolean isRI()
Returns the state of the RI line.
boolean isRTS()
Returns the state of the RTS line.
boolean isReceiveThresholdEnabled()
Checks if receive threshold is enabled.
boolean isReceiveTimeoutEnabled()
Checks if receive timeout is enabled.
void notifyOnBreakInterrupt(boolean enable)
Sets whether the listener should be notified on break interrupt.
void notifyOnCTS(boolean enable)
Sets whether the listener should be notified on CTS.
void notifyOnCarrierDetect(boolean enable)
Sets whether the listener should be notified on carrier detect.
void notifyOnDSR(boolean enable)
Sets whether the listener should be notified on DSR.
void notifyOnDataAvailable(boolean enable)
Sets whether the listener should be notified on data available.
void notifyOnFramingError(boolean enable)
Sets whether the listener should be notified on framing error.
void notifyOnOutputEmpty(boolean enable)
Sets whether the listener should be notified on output empty.
void notifyOnOverrunError(boolean enable)
Sets whether the listener should be notified on overrun error.
void notifyOnParityError(boolean enable)
Sets whether the listener should be notified on parity error.
void notifyOnRingIndicator(boolean enable)
Sets whether the listener should be notified on ring indicator.
int readData(byte[] buffer, int numBytes)
Attempts to read the given amount of bytes from the serial port.
void registerEventListener(ISerialPortEventListener listener)
Registers the given listener to be notified when a serial port event occurs.
void sendBreak(int duration)
Sends a break signal to the serial port with the given duration (in milliseconds).
boolean setBaudBase(int baudBase)
Sets the baud base value.
void setDTR(boolean state)
Sets the state of the DTR.
boolean setDivisor(int divisor)
Sets the divisor value.
boolean setEndOfInputChar(byte b)
Sets the end of input character.
void setInputBufferSize(int size)
Sets the input buffer size.
void setOutputBufferSize(int size)
Sets the output buffer size.
boolean setParityErrorChar(byte b)
Sets the parity error character.
void setPortParameters(int baudRate, int dataBits, int stopBits, int parity, int flowControl)
Sets the new parameters of the serial port.
void setRTS(boolean state)
Sets the state of the RTS line.
String toString()
void unregisterEventListener()
Unregisters the configured serial port event listener.
void writeData(byte[] data, int numBytes)
Attempts to write the given amount of bytes to the serial port.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int DATABITS_5

5 data bits: .DATABITS_5.

Constant Value: 5 (0x00000005)

public static final int DATABITS_6

6 data bits: .DATABITS_6.

Constant Value: 6 (0x00000006)

public static final int DATABITS_7

7 data bits: .DATABITS_7.

Constant Value: 7 (0x00000007)

public static final int DATABITS_8

8 data bits: .DATABITS_8.

Constant Value: 8 (0x00000008)

public static final int DEFAULT_READ_TIMEOUT

Default read timeout: .DEFAULT_READ_TIMEOUT milliseconds.

Constant Value: 3000 (0x00000bb8)

public static final int FLOWCONTROL_NONE

No flow control: .FLOWCONTROL_NONE.

Constant Value: 0 (0x00000000)

public static final int FLOWCONTROL_RTSCTS_IN

Hardware flow control (input): .FLOWCONTROL_RTSCTS_IN.

Constant Value: 1 (0x00000001)

public static final int FLOWCONTROL_RTSCTS_OUT

Hardware flow control (output): .FLOWCONTROL_RTSCTS_OUT.

Constant Value: 2 (0x00000002)

public static final int FLOWCONTROL_XONXOFF_IN

Xon/Xoff flow control (input): .FLOWCONTROL_XONXOFF_IN.

Constant Value: 4 (0x00000004)

public static final int FLOWCONTROL_XONXOFF_OUT

Xon/Xoff flow control (output): .FLOWCONTROL_XONXOFF_OUT.

Constant Value: 8 (0x00000008)

public static final int PARITY_EVEN

Parity even: .PARITY_EVEN.

Constant Value: 2 (0x00000002)

public static final int PARITY_MARK

Parity mark: .PARITY_MARK.

Constant Value: 3 (0x00000003)

public static final int PARITY_NONE

Parity none: .PARITY_NONE.

Constant Value: 0 (0x00000000)

public static final int PARITY_ODD

Parity odd: .PARITY_ODD.

Constant Value: 1 (0x00000001)

public static final int PARITY_SPACE

Parity space: .PARITY_SPACE.

Constant Value: 4 (0x00000004)

public static final int STOPBITS_1

1 stop bit: .STOPBITS_1.

Constant Value: 1 (0x00000001)

public static final int STOPBITS_1_5

1.5 stop bits: .STOPBITS_1_5.

Constant Value: 3 (0x00000003)

public static final int STOPBITS_2

2 stop bits: .STOPBITS_2.

Constant Value: 2 (0x00000002)

Public Methods

public void close ()

Closes the communications port.

The application must call this method when it is done with the port.

public void disableReceiveThreshold ()

public void disableReceiveTimeout ()

public void enableReceiveThreshold (int thresh)

Enables receive threshold, if this feature is supported by the driver. When the receive threshold condition becomes true, a read from the input stream for this port will return immediately.

This is an advisory method which the driver may not implement. By default, receive threshold is not enabled.

An application can determine whether the driver supports this feature by first calling the enableReceiveThreshold(int) method and then calling the isReceiveThresholdEnabled() method. If it returns false, then receive threshold is not supported by the driver.

Parameters
thresh int: When this many bytes are in the input buffer, return immediately from read.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public void enableReceiveTimeout (int time)

Enables receive timeout, if this feature is supported by the driver. When the receive timeout condition becomes true, a read from the input stream for this port will return immediately.

This is an advisory method which the driver may not implement. By default, receive timeout is not enabled.

An application can determine whether the driver supports this feature by first calling the enableReceiveTimeout(int) method and then calling the isReceiveTimeoutEnabled() method. If it returns false, then receive timeout is not supported by the driver.

Parameters
time int: When this many milliseconds have elapsed, return immediately from read, regardless of bytes in input buffer.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public int getBaudBase ()

Returns the baud base value.

Returns
int Baud base.
Throws
IOException If an I/O error occurs.
UnsupportedCommOperationException If the operation is not supported by the driver.

See also:

public int getBaudRate ()

Returns the configured baud rate.

Returns
int Baud rate.

public int getDataBits ()

Returns the configured data bits.

Returns
int Data bits.

public int getDivisor ()

Returns the divisor value.

Returns
int Divisor.
Throws
IOException If an I/O error occurs.
UnsupportedCommOperationException If the operation is not supported by the driver.

See also:

public byte getEndOfInputChar ()

Returns the end of input character.

Returns
byte The End of Input Character.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public int getFlowControl ()

Returns the configured flow control.

Returns
int Flow control.

public int getInputBufferSize ()

Returns the input buffer size.

Returns
int Input buffer size currently in use in bytes.

public InputStream getInputStream ()

Returns the connection interface input stream to read data from.

Returns
InputStream The connection interface input stream to read data from.
Throws
IOException If an I/O error occurs.

public String getName ()

Returns the serial port name.

Returns
String The serial port name.

public int getOutputBufferSize ()

Returns the output buffer size.

Returns
int Output buffer size currently in use in bytes.

public OutputStream getOutputStream ()

Returns the connection interface output stream to write data to.

Returns
OutputStream The connection interface output stream to write data to.
Throws
IOException If an I/O error occurs.

public int getParity ()

Returns the configured parity.

Returns
int Parity.

public byte getParityErrorChar ()

Returns the parity error character.

Returns
byte The Parity Error Character.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public int getReceiveThreshold ()

Returns the integer value of the receive threshold.

Returns
int Number of bytes for receive threshold.

public int getReceiveTimeout ()

Returns the integer value of the receive timeout.

Returns
int Number of milliseconds in receive timeout.

public int getStopBits ()

Returns the configured stop bits.

Returns
int Stop bits.

public boolean isCD ()

Returns the state of the CD line.

Returns
boolean true if the line is high, false otherwise.

public boolean isCTS ()

Returns the state of the CTS line.

Returns
boolean true if the line is high, false otherwise.

public boolean isDSR ()

Returns the state of the DSR line.

Returns
boolean true if the line is high, false otherwise.

public boolean isDTR ()

Returns the state of the DTR line.

Returns
boolean true if the line is high, false otherwise.

public boolean isRI ()

Returns the state of the RI line.

Returns
boolean true if the line is high, false otherwise.

public boolean isRTS ()

Returns the state of the RTS line.

Returns
boolean true if the line is high, false otherwise.

public boolean isReceiveThresholdEnabled ()

Checks if receive threshold is enabled.

An application can determine whether the driver supports this feature by first calling the enableReceiveThreshold(int) method and then calling the isReceiveThresholdEnabled() method. If it returns false, then receive threshold is not supported by the driver.

Returns
boolean true if the receive threshold is enabled, false otherwise.

public boolean isReceiveTimeoutEnabled ()

Checks if receive timeout is enabled.

An application can determine whether the driver supports this feature by first calling the enableReceiveTimeout(int) method and then calling the isReceiveTimeoutEnabled() method. If it returns false, then receive timeout is not supported by the driver.

Returns
boolean true if the receive timeout is enabled, false otherwise.

public void notifyOnBreakInterrupt (boolean enable)

Sets whether the listener should be notified on break interrupt.

Parameters
enable boolean: true to notify.

public void notifyOnCTS (boolean enable)

Sets whether the listener should be notified on CTS.

Parameters
enable boolean: true to notify.

public void notifyOnCarrierDetect (boolean enable)

Sets whether the listener should be notified on carrier detect.

Parameters
enable boolean: true to notify.

public void notifyOnDSR (boolean enable)

Sets whether the listener should be notified on DSR.

Parameters
enable boolean: true to notify.

public void notifyOnDataAvailable (boolean enable)

Sets whether the listener should be notified on data available.

Parameters
enable boolean: true to notify.

public void notifyOnFramingError (boolean enable)

Sets whether the listener should be notified on framing error.

Parameters
enable boolean: true to notify.

public void notifyOnOutputEmpty (boolean enable)

Sets whether the listener should be notified on output empty.

Parameters
enable boolean: true to notify.

public void notifyOnOverrunError (boolean enable)

Sets whether the listener should be notified on overrun error.

Parameters
enable boolean: true to notify.

public void notifyOnParityError (boolean enable)

Sets whether the listener should be notified on parity error.

Parameters
enable boolean: true to notify.

public void notifyOnRingIndicator (boolean enable)

Sets whether the listener should be notified on ring indicator.

Parameters
enable boolean: true to notify.

public int readData (byte[] buffer, int numBytes)

Attempts to read the given amount of bytes from the serial port.

Parameters
buffer byte: Byte array to store read data.
numBytes int: Number of bytes to read.
Returns
int The number of read bytes.
Throws
IllegalArgumentException If numBytes < 1 or if numBytes > buffer.length.
IOException If an I/O error occurs.

public void registerEventListener (ISerialPortEventListener listener)

Registers the given listener to be notified when a serial port event occurs.

Note that you can only register one listener.

Parameters
listener ISerialPortEventListener: Serial port event listener.
Throws
TooManyListenersException If you register more than one listener.

public void sendBreak (int duration)

Sends a break signal to the serial port with the given duration (in milliseconds).

Parameters
duration int: Duration of the break signal in milliseconds.

public boolean setBaudBase (int baudBase)

Sets the baud base value.

Parameters
baudBase int: The clock frequency divided by 16. Default BaudBase is 115200.
Returns
boolean true on success.
Throws
IOException If an I/O error occurs.
UnsupportedCommOperationException If the operation is not supported by the driver.

See also:

public void setDTR (boolean state)

Sets the state of the DTR.

Parameters
state boolean: true to set the line status high, false to set it low.

public boolean setDivisor (int divisor)

Sets the divisor value.

Parameters
divisor int: Divisor.
Returns
boolean true on success.
Throws
IOException If an I/O error occurs.
UnsupportedCommOperationException If the operation is not supported by the driver.

See also:

public boolean setEndOfInputChar (byte b)

Sets the end of input character.

Parameters
b byte: End Of Input Character.
Returns
boolean true on success.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public void setInputBufferSize (int size)

Sets the input buffer size.

Parameters
size int: Size of the input buffer in bytes.

public void setOutputBufferSize (int size)

Sets the output buffer size.

Parameters
size int: Size of the output buffer in bytes.

public boolean setParityErrorChar (byte b)

Sets the parity error character.

Parameters
b byte: Parity Error Character.
Returns
boolean true on success.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public void setPortParameters (int baudRate, int dataBits, int stopBits, int parity, int flowControl)

Sets the new parameters of the serial port.

Parameters
baudRate int: Baud rate.
dataBits int: Data bits, one of DATABITS_5, DATABITS_6, DATABITS_7, and DATABITS_8.
stopBits int: Stop bits, one of STOPBITS_1, STOPBITS_2, and STOPBITS_1_5.
parity int: Parity, one of PARITY_NONE, PARITY_ODD, PARITY_EVEN, PARITY_MARK, and PARITY_SPACE.
flowControl int: Flow control, one of FLOWCONTROL_NONE, FLOWCONTROL_RTSCTS_IN, FLOWCONTROL_RTSCTS_OUT, FLOWCONTROL_XONXOFF_IN, and FLOWCONTROL_XONXOFF_OUT. Use an "|" to combine them, e.g. FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT.
Throws
UnsupportedCommOperationException If the operation is not supported by the driver.

public void setRTS (boolean state)

Sets the state of the RTS line.

Parameters
state boolean: true to set the line status high, false to set it low.

public String toString ()

Returns
String

public void unregisterEventListener ()

Unregisters the configured serial port event listener.

public void writeData (byte[] data, int numBytes)

Attempts to write the given amount of bytes to the serial port.

Parameters
data byte: Data to write.
numBytes int: Number of bytes to write.
Throws
IllegalArgumentException If numBytes < 1 or if numBytes > data.length.
IOException If an I/O error occurs.