public interface SerialPortDevice extends Device
The serial port device object is obtained from the POSTerminal
, its' as follows:
SerialPortDevice serialPortDevice = (SerialPortDevice) POSTerminal.getInstance().getDevice("com.cloudpos.device.serialport");Among them, the "com.cloudpos.device.serialport" string is to identify the serial port device, defined by the specific implementation.
Uses the serial port device object to control the operation of the serial port device.
An app can request permission to access the device, as shown below:
<uses-permission android:name="android.permission.CLOUDPOS_SERIAL"/>
Device
Modifier and Type | Field and Description |
---|---|
static int |
DATABITS_5
5 data bit format
|
static int |
DATABITS_6
6 data bit format
|
static int |
DATABITS_7
7 data bit format
|
static int |
DATABITS_8
8 data bit format
|
static int |
FLOWCONTROL_NONE
flow control off
|
static int |
FLOWCONTROL_RTSCTS_IN
RTS/CTS flow control on input
|
static int |
FLOWCONTROL_RTSCTS_IN_OUT
RTS/CTS flow control on 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 input and output
|
static int |
FLOWCONTROL_XONXOFF_OUT
XON/XOFF flow control on output
|
static int |
ID_SERIAL_EXT
Works for internal fiscal/ other serial port module
|
static int |
ID_USB_CDC
Works for USB Communication Device Class
|
static int |
ID_USB_HOST_SERIAL
Works for USB serial port in master mode
|
static int |
ID_USB_SLAVE_SERIAL
Works for USB serial port in slave mode
|
static int |
PARITY_EVEN
EVEN parity scheme.
|
static int |
PARITY_NONE
no parity bits
|
static int |
PARITY_ODD
ODD parity scheme.
|
static int |
STOPBITS_1
1 stop bit
|
static int |
STOPBITS_1_5
1-1/2 stop bits.
|
static int |
STOPBITS_2
2 stop bits
|
Modifier and Type | Method and Description |
---|---|
void |
changeFlowControlMode(int flowControl)
Changes the flow control mode.
|
void |
changeRTS(boolean rts)
Sets or clears the RTS (Request To Send) bit in the UART.
|
void |
changeSerialPortParams(int baudrate,
int dataBits,
int stopBits,
int parity)
Changes the serial port parameters.
|
int |
getBaudRate()
Gets the currently configured baud rate.
|
int |
getDataBits()
Gets the currently configured number of data bits.
|
int |
getFlowControlMode()
Gets the currently configured flow control mode.
|
int |
getParity()
Gets the currently configured parity setting.
|
int |
getStopBits()
Gets the currently defined stop bits.
|
void |
listenForRead(int len,
OperationListener listener,
int timeout)
Reads up to byteCount bytes from this serial control and stores them in
the byte array buffer starting at byteOffset.
|
void |
open(int logicID)
Connects the serial communication control to a specific serial port.
|
boolean |
retrieveCTS()
Retrieves the state of the CTS (Clear To Send) bit in the UART.
|
boolean |
retrieveRTS()
Retrieves the state of the RTS (Request To Send) bit in the UART.
|
SerialPortOperationResult |
waitForRead(int len,
int timeout)
The synchronous method of
listenForRead(int,OperationListener,int) . |
void |
write(byte[] data,
int offset,
int len)
Writes
len bytes from the specified byte array starting at
offset to the open output stream. |
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
void open(int logicID) throws DeviceException
Opens and initializes the platform serial port specified in parameter.
logicID
- the logical ID of the serial port to open, logicID = ID_USB_SLAVE_SERIAL
or
ID_USB_HOST_SERIAL
or
ID_SERIAL_EXT
or
ID_USB_CDC
.DeviceException
- for the standard reasons that are documented in
DeviceException
.int getBaudRate() throws DeviceException
DeviceException
- for the standard reasons that are documented in
DeviceException
.int getDataBits() throws DeviceException
DATABITS_5
,
DATABITS_6
, DATABITS_7
, or DATABITS_8
.DeviceException
- for the standard reasons that are documented in
DeviceException
.int getStopBits() throws DeviceException
STOPBITS_1
,
STOPBITS_2
, or STOPBITS_1_5
.DeviceException
- for the standard reasons that are documented in
DeviceException
.int getParity() throws DeviceException
PARITY_NONE
,
PARITY_ODD
, or PARITY_EVEN
.DeviceException
- for the standard reasons that are documented in
DeviceException
.void changeRTS(boolean rts) throws DeviceException
rts
- true
to set RTS, false
to clear RTSDeviceException
- for the standard reasons that are documented in
DeviceException
.boolean retrieveRTS() throws DeviceException
DeviceException
- for the standard reasons that are documented in
DeviceException
.boolean retrieveCTS() throws DeviceException
DeviceException
- for the standard reasons that are documented in
DeviceException
.void changeFlowControlMode(int flowControl) throws DeviceException
flowControl
- a constant as defined in SerialControl:
FLOWCONTROL_NONE
- no flow control FLOWCONTROL_RTSCTS_IN_OUT
- RTS/CTS
(hardware) flow control for input and output DeviceException
- for the standard reasons that are documented in
DeviceException
.int getFlowControlMode() throws DeviceException
FLOWCONTROL_NONE
- no flow control
FLOWCONTROL_RTSCTS_IN_OUT
- RTS/CTS (hardware) flow
control for input and output
DeviceException
- for the standard reasons that are documented in
DeviceException
.void changeSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) throws DeviceException
baudrate
- the desired data communication speeddataBits
- DATABITS_8
- 8 bits
stopBits
- STOPBITS_1
- 1 stop bit
parity
- PARITY_NONE
- no parity
DeviceException
- for the standard reasons that are documented in
DeviceException
.void write(byte[] data, int offset, int len) throws DeviceException
len
bytes from the specified byte array starting at
offset
to the open output stream.
This method is synchronous.
data
- the data to be written to the outputoffset
- the starting offset in the data bufferlen
- the number of bytes to be writtenDeviceException
- for the standard reasons that are documented in
DeviceException
.void listenForRead(int len, OperationListener listener, int timeout) throws DeviceException
len
bytes of data.
This method is asynchronous. On success, the read data can be
retrieved using SerialOperationResult
's
getBuffer()
method.
Because this method uses a timeout parameter, the device receiving this request MUST be able to
successfully respond to cancelRequest()
and abort the operation.
In the case that a timeout occurs, the buffer in the operation event
contains the bytes received so far, up to the requested length. This
operation event has code = ERR_TIMEOUT
regardless of
whether any data has been received.
SerialOperationResult.getDataLength()
can be used to check
how much data, if any, have been received.
len
- the number of bytes to be readlistener
- the OperationListener to handle the result.timeout
- the maximum sleeping time of the method, in milliseconds.
Note that the timeout will begin when the request begins to be
processed. The timeout
parameter is then only
indicative. There are two special cases for
timeout
, as follows:
DeviceException
- for the standard reasons that are documented in
DeviceException
.OperationListener.handleResult(com.cloudpos.OperationResult)
,
SerialPortOperationResult
,
TimeConstants.FOREVER
,
TimeConstants.IMMEDIATE
SerialPortOperationResult waitForRead(int len, int timeout) throws DeviceException
listenForRead(int,OperationListener,int)
.len
- the number of bytes to be readtimeout
- the maximum sleeping time of the method, in milliseconds.
Note that the timeout will begin when the request begins to be
processed. The timeout
parameter is then only
indicative. There are two special cases for
timeout
, as follows:
SerialOperationResult
of this operation.DeviceException
- for the standard reasons that are documented in
DeviceException
.SerialPortOperationResult
,
TimeConstants.FOREVER
,
TimeConstants.IMMEDIATE