public interface SerialPortDevice extends Device
SerialPortDevice
interface defines the APIs for serial port communication to be used by applications.
A serial port device object is obtained from the POSTerminal
as follows:
SerialPortDevice serialPortDevice = (SerialPortDevice) POSTerminal.getInstance().getDevice("com.cloudpos.device.serialport");
The identifier "com.cloudpos.device.serialport" is used to specify the serial port device, as defined by the implementation.
Applications can control the serial port device using this interface and must request the necessary permissions as shown below:
<uses-permission android:name="android.permission.CLOUDPOS_SERIAL"/>
Device
Modifier and Type | Field and Description |
---|---|
static int |
DATABITS_5
5 data bits
|
static int |
DATABITS_6
6 data bits
|
static int |
DATABITS_7
7 data bits
|
static int |
DATABITS_8
8 data bits
|
static int |
FLOWCONTROL_NONE
No flow control
|
static int |
FLOWCONTROL_RTSCTS_IN
RTS/CTS flow control on input
|
static int |
FLOWCONTROL_RTSCTS_IN_OUT
RTS/CTS flow control on both input and output
|
static int |
FLOWCONTROL_RTSCTS_OUT
RTS/CTS flow control on output
|
static int |
FLOWCONTROL_XONXOFF_IN
XON/XOFF flow control on input
|
static int |
FLOWCONTROL_XONXOFF_IN_OUT
XON/XOFF flow control on both input and output
|
static int |
FLOWCONTROL_XONXOFF_OUT
XON/XOFF flow control on output
|
static int |
ID_SERIAL_EXT
Internal fiscal/other serial port module
|
static int |
ID_USB_CDC
USB Communication Device Class
|
static int |
ID_USB_GPRINTER
USB GPRINTER
|
static int |
ID_USB_HOST_SERIAL
USB serial port in master mode
|
static int |
ID_USB_SLAVE_SERIAL
USB serial port in slave mode
|
static int |
ID_USB_SLAVE_SERIAL_ACM
USB serial port in slave mode (ACM)
|
static int |
PARITY_EVEN
Even parity.
|
static int |
PARITY_NONE
No parity
|
static int |
PARITY_ODD
Odd parity.
|
static int |
STOPBITS_1
1 stop bits
|
static int |
STOPBITS_1_5
1.5 stop bits.
|
static int |
STOPBITS_2
2 stop bits
|
Modifier and Type | Method and Description |
---|---|
void |
changeFlowControlMode(int flowControl)
Changes the flow control mode of the serial port.
|
void |
changeRTS(boolean rts)
Sets or clears the RTS (Request To Send) control signal.
|
void |
changeSerialPortParams(int baudrate,
int dataBits,
int stopBits,
int parity)
Changes the serial port parameters like baud rate, data bits, stop bits, and parity.
|
int |
getBaudRate()
Retrieves the current baud rate configuration of the serial port.
|
int |
getDataBits()
Retrieves the current data bit configuration of the serial port.
|
int |
getFlowControlMode()
Gets the currently configured flow control mode.
|
int |
getParity()
Retrieves the current parity setting of the serial port.
|
int |
getStopBits()
Retrieves the current stop bit configuration of the serial port.
|
void |
listenForRead(int len,
OperationListener listener,
int timeout)
Asynchronously reads a specified number of bytes from the serial port.
|
void |
open(int logicID)
Connects to a specific serial port identified by the logical ID.
|
boolean |
retrieveCTS()
Retrieves the current state of the CTS (Clear To Send) control signal.
|
boolean |
retrieveRTS()
Retrieves the current state of the RTS (Request To Send) control signal.
|
SerialPortOperationResult |
waitForRead(int len,
int timeout)
Synchronously reads a specified number of bytes from the serial port.
|
void |
write(byte[] data,
int offset,
int len)
Writes a specified number of bytes to the open output stream of the serial port.
|
cancelRequest, close, getFailCount, getUsageCount, open
static final int DATABITS_5
static final int DATABITS_6
static final int DATABITS_7
static final int DATABITS_8
static final int STOPBITS_1
static final int STOPBITS_2
static final int STOPBITS_1_5
static final int PARITY_NONE
static final int PARITY_ODD
static final int PARITY_EVEN
static final int FLOWCONTROL_NONE
static final int FLOWCONTROL_RTSCTS_IN
static final int FLOWCONTROL_RTSCTS_OUT
static final int FLOWCONTROL_RTSCTS_IN_OUT
static final int FLOWCONTROL_XONXOFF_IN
static final int FLOWCONTROL_XONXOFF_OUT
static final int FLOWCONTROL_XONXOFF_IN_OUT
static final int ID_USB_SLAVE_SERIAL
static final int ID_USB_HOST_SERIAL
static final int ID_SERIAL_EXT
static final int ID_USB_CDC
static final int ID_USB_GPRINTER
static final int ID_USB_SLAVE_SERIAL_ACM
void open(int logicID) throws DeviceException
Initializes the specified platform serial port.
logicID
- Logical ID of the serial port to open. Can be one of ID_USB_SLAVE_SERIAL
,
ID_USB_HOST_SERIAL
, ID_SERIAL_EXT
, ID_USB_CDC
,
ID_USB_GPRINTER
, or ID_USB_SLAVE_SERIAL_ACM
.DeviceException
- for standard errors as documented in DeviceException
.int getBaudRate() throws DeviceException
DeviceException
- for standard errors as documented in DeviceException
.int getDataBits() throws DeviceException
DATABITS_5
, DATABITS_6
,
DATABITS_7
, or DATABITS_8
.DeviceException
- for standard errors as documented in DeviceException
.int getStopBits() throws DeviceException
STOPBITS_1
, STOPBITS_2
, or STOPBITS_1_5
.DeviceException
- for standard errors as documented in DeviceException
.int getParity() throws DeviceException
PARITY_NONE
, PARITY_ODD
, or PARITY_EVEN
.DeviceException
- for standard errors as documented in DeviceException
.void changeRTS(boolean rts) throws DeviceException
Note: This function is not implemented yet.
rts
- true
to set RTS, false
to clear RTS.DeviceException
- for standard errors as documented in DeviceException
.boolean retrieveRTS() throws DeviceException
Note: This function is not implemented yet.
true
if RTS is set, false
if RTS is cleared.DeviceException
- for standard errors as documented in DeviceException
.boolean retrieveCTS() throws DeviceException
Note: This function is not implemented yet.
true
if CTS is set, false
if CTS is cleared.DeviceException
- for standard errors as documented in DeviceException
.void changeFlowControlMode(int flowControl) throws DeviceException
flowControl
- One of the flow control constants defined in this interface, such as
FLOWCONTROL_NONE
or FLOWCONTROL_RTSCTS_IN_OUT
.DeviceException
- for standard errors as documented in DeviceException
.int getFlowControlMode() throws DeviceException
DeviceException
- for standard errors as documented in DeviceException
.void changeSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) throws DeviceException
baudrate
- Desired data communication speed.dataBits
- Data bit setting, such as DATABITS_8
.stopBits
- Stop bit setting, such as STOPBITS_1
.parity
- Parity setting, such as PARITY_NONE
.DeviceException
- for standard errors as documented in DeviceException
.void write(byte[] data, int offset, int len) throws DeviceException
This method is synchronous, meaning it will block until all bytes are written or an error occurs.
It writes len
bytes from the provided byte array, starting at the specified offset
, to the serial port's output stream.
data
- The byte array containing the data to be written.offset
- The starting position in the byte array from where the data should be written.len
- The number of bytes to write from the array.DeviceException
- for standard errors as documented in DeviceException
.void listenForRead(int len, OperationListener listener, int timeout) throws DeviceException
This method reads up to len
bytes of data and stores them in a byte array buffer.
It returns the actual number of bytes read, or -1 if the end of the stream has been reached.
Upon successful completion, the read data can be retrieved using SerialOperationResult#getBuffer()
.
This method supports a timeout mechanism. The device must be capable of responding to
Device.cancelRequest()
to abort the operation in case of a timeout.
If a timeout occurs, the buffer in the operation event contains the bytes received so far, up to the requested length.
The operation event code will be OperationResult.ERR_TIMEOUT
, regardless of whether any data has been received.
Use SerialOperationResult#getDataLength()
to check the amount of data received.
The timeout
parameter indicates the maximum time to wait in milliseconds:
timeout == {@link TimeConstants#FOREVER}
, the method waits until data is available on the input.
timeout == {@link TimeConstants#IMMEDIATE}
, the method immediately reads the available input.
len
- The number of bytes to read.listener
- The OperationListener
to handle the result.timeout
- The maximum time to wait for the read operation, in milliseconds. Special cases include TimeConstants.FOREVER
and TimeConstants.IMMEDIATE
.DeviceException
- for standard errors as documented in DeviceException
.OperationListener.handleResult(com.cloudpos.OperationResult)
,
SerialPortOperationResult
,
TimeConstants.FOREVER
,
TimeConstants.IMMEDIATE
SerialPortOperationResult waitForRead(int len, int timeout) throws DeviceException
This is the synchronous counterpart of listenForRead(int,OperationListener,int)
.
It reads up to len
bytes and returns a SerialOperationResult
containing the read data.
The timeout
parameter specifies the maximum time to wait for the read operation, in milliseconds:
timeout == {@link TimeConstants#FOREVER}
, the method waits indefinitely until data becomes available on the input.
timeout == {@link TimeConstants#IMMEDIATE}
, the method immediately reads the available input without waiting.
timeout
duration only indicative.len
- The number of bytes to read.timeout
- The maximum time to wait for the read operation, in milliseconds. Special cases include TimeConstants.FOREVER
and TimeConstants.IMMEDIATE
.SerialOperationResult
object representing the result of the operation.DeviceException
- for standard errors as documented in DeviceException
.SerialPortOperationResult
,
TimeConstants.FOREVER
,
TimeConstants.IMMEDIATE