Package com.zebra.commoniolib
Interface UsbSerialPort
- All Superinterfaces:
AutoCloseable
,Closeable
- All Known Implementing Classes:
CdcAcmSerialDriver.CdcAcmSerialPort
,CommonUsbSerialPort
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enum
values for get[Supported]ControlLines() -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
5 data bits.static final int
6 data bits.static final int
7 data bits.static final int
8 data bits.static final int
No flow control.static final int
RTS/CTS input flow control.static final int
RTS/CTS output flow control.static final int
XON/XOFF input flow control.static final int
XON/XOFF output flow control.static final int
Even parity.static final int
Mark parity.static final int
No parity.static final int
Odd parity.static final int
Space parity.static final int
1 stop bit.static final int
1.5 stop bits.static final int
2 stop bits. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the port.boolean
getCD()
Gets the CD (Carrier Detect) bit from the underlying UART.Gets all control line values from the underlying UART, if supported.boolean
getCTS()
Gets the CTS (Clear To Send) bit from the underlying UART.android.hardware.usb.UsbDevice
Returns the currently-bound USB device.Returns the driver used by this port.boolean
getDSR()
Gets the DSR (Data Set Ready) bit from the underlying UART.boolean
getDTR()
Gets the DTR (Data Terminal Ready) bit from the underlying UART.int
Port number within driver.boolean
getRI()
Gets the RI (Ring Indicator) bit from the underlying UART.boolean
getRTS()
Gets the RTS (Request To Send) bit from the underlying UART.The serial number of the underlying UsbDeviceConnection, ornull
.Gets all control line supported flags.boolean
isOpen()
void
open
(android.hardware.usb.UsbDeviceConnection connection) Opens and initializes the port.boolean
purgeHwBuffers
(boolean purgeWriteBuffers, boolean purgeReadBuffers) purge non-transmitted output data and / or non-read input dataint
read
(byte[] dest, int timeout) Reads as many bytes as possible into the destination buffer.void
setDTR
(boolean value) Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.void
setParameters
(int baudRate, int dataBits, int stopBits, int parity) Sets various serial port parameters.void
setRTS
(boolean value) Sets the RTS (Request To Send) bit on the underlying UART, if supported.int
write
(byte[] src, int timeout) Writes as many bytes as possible from the source buffer.
-
Field Details
-
DATABITS_5
static final int DATABITS_55 data bits.- See Also:
-
DATABITS_6
static final int DATABITS_66 data bits.- See Also:
-
DATABITS_7
static final int DATABITS_77 data bits.- See Also:
-
DATABITS_8
static final int DATABITS_88 data bits.- See Also:
-
FLOWCONTROL_NONE
static final int FLOWCONTROL_NONENo flow control.- See Also:
-
FLOWCONTROL_RTSCTS_IN
static final int FLOWCONTROL_RTSCTS_INRTS/CTS input flow control.- See Also:
-
FLOWCONTROL_RTSCTS_OUT
static final int FLOWCONTROL_RTSCTS_OUTRTS/CTS output flow control.- See Also:
-
FLOWCONTROL_XONXOFF_IN
static final int FLOWCONTROL_XONXOFF_INXON/XOFF input flow control.- See Also:
-
FLOWCONTROL_XONXOFF_OUT
static final int FLOWCONTROL_XONXOFF_OUTXON/XOFF output flow control.- See Also:
-
PARITY_NONE
static final int PARITY_NONENo parity.- See Also:
-
PARITY_ODD
static final int PARITY_ODDOdd parity.- See Also:
-
PARITY_EVEN
static final int PARITY_EVENEven parity.- See Also:
-
PARITY_MARK
static final int PARITY_MARKMark parity.- See Also:
-
PARITY_SPACE
static final int PARITY_SPACESpace parity.- See Also:
-
STOPBITS_1
static final int STOPBITS_11 stop bit.- See Also:
-
STOPBITS_1_5
static final int STOPBITS_1_51.5 stop bits.- See Also:
-
STOPBITS_2
static final int STOPBITS_22 stop bits.- See Also:
-
-
Method Details
-
getDriver
UsbSerialDriver getDriver()Returns the driver used by this port. -
getDevice
android.hardware.usb.UsbDevice getDevice()Returns the currently-bound USB device. -
getPortNumber
int getPortNumber()Port number within driver. -
getSerial
String getSerial()The serial number of the underlying UsbDeviceConnection, ornull
.- Returns:
- value from
UsbDeviceConnection.getSerial()
- Throws:
SecurityException
- starting with target SDK 29 (Android 10) if permission for USB device is not granted
-
open
Opens and initializes the port. Upon success, caller must ensure thatclose()
is eventually called.- Parameters:
connection
- an open device connection, acquired withUsbManager.openDevice(UsbDevice)
- Throws:
IOException
- on error opening or initializing the port.
-
close
Closes the port.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- on error closing the port.
-
read
Reads as many bytes as possible into the destination buffer.- Parameters:
dest
- the destination byte buffertimeout
- the timeout for reading in milliseconds, 0 is infinite- Returns:
- the actual number of bytes read
- Throws:
IOException
- if an error occurred during reading
-
write
Writes as many bytes as possible from the source buffer.- Parameters:
src
- the source byte buffertimeout
- the timeout for writing in milliseconds, 0 is infinite- Returns:
- the actual number of bytes written
- Throws:
IOException
- if an error occurred during writing
-
setParameters
Sets various serial port parameters.- Parameters:
baudRate
- baud rate as an integer, for example115200
.dataBits
- one ofDATABITS_5
,DATABITS_6
,DATABITS_7
, orDATABITS_8
.stopBits
- one ofSTOPBITS_1
,STOPBITS_1_5
, orSTOPBITS_2
.parity
- one ofPARITY_NONE
,PARITY_ODD
,PARITY_EVEN
,PARITY_MARK
, orPARITY_SPACE
.- Throws:
IOException
- on error setting the port parametersUnsupportedOperationException
- if not supported by a specific device
-
getCD
Gets the CD (Carrier Detect) bit from the underlying UART.- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getCTS
Gets the CTS (Clear To Send) bit from the underlying UART.- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getDSR
Gets the DSR (Data Set Ready) bit from the underlying UART.- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getDTR
Gets the DTR (Data Terminal Ready) bit from the underlying UART.- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
setDTR
Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.- Parameters:
value
- the value to set- Throws:
IOException
- if an error occurred during writing
-
getRI
Gets the RI (Ring Indicator) bit from the underlying UART.- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getRTS
Gets the RTS (Request To Send) bit from the underlying UART.- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
setRTS
Sets the RTS (Request To Send) bit on the underlying UART, if supported.- Parameters:
value
- the value to set- Throws:
IOException
- if an error occurred during writing
-
getControlLines
Gets all control line values from the underlying UART, if supported. Requires less USB calls than calling getRTS() + ... + getRI() individually.- Returns:
- EnumSet.contains(...) is
true
if set, elsefalse
- Throws:
IOException
-
getSupportedControlLines
Gets all control line supported flags.- Returns:
- EnumSet.contains(...) is
true
if supported, elsefalse
- Throws:
IOException
-
purgeHwBuffers
purge non-transmitted output data and / or non-read input data- Parameters:
purgeWriteBuffers
-true
to discard non-transmitted output datapurgeReadBuffers
-true
to discard non-read input data- Returns:
true
if the operation was successful, orfalse
if the operation is not supported by the driver or device- Throws:
IOException
- if an error occurred during flush
-
isOpen
boolean isOpen()- Returns:
- the current state of the connection
-