Package com.zebra.commoniolib
Interface UsbSerialPort
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
CdcAcmSerialDriver.CdcAcmSerialPort,CommonUsbSerialPort
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumvalues for get[Supported]ControlLines() -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int5 data bits.static final int6 data bits.static final int7 data bits.static final int8 data bits.static final intNo flow control.static final intRTS/CTS input flow control.static final intRTS/CTS output flow control.static final intXON/XOFF input flow control.static final intXON/XOFF output flow control.static final intEven parity.static final intMark parity.static final intNo parity.static final intOdd parity.static final intSpace parity.static final int1 stop bit.static final int1.5 stop bits.static final int2 stop bits. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the port.booleangetCD()Gets the CD (Carrier Detect) bit from the underlying UART.Gets all control line values from the underlying UART, if supported.booleangetCTS()Gets the CTS (Clear To Send) bit from the underlying UART.android.hardware.usb.UsbDeviceReturns the currently-bound USB device.Returns the driver used by this port.booleangetDSR()Gets the DSR (Data Set Ready) bit from the underlying UART.booleangetDTR()Gets the DTR (Data Terminal Ready) bit from the underlying UART.intPort number within driver.booleangetRI()Gets the RI (Ring Indicator) bit from the underlying UART.booleangetRTS()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.booleanisOpen()voidopen(android.hardware.usb.UsbDeviceConnection connection) Opens and initializes the port.booleanpurgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) purge non-transmitted output data and / or non-read input dataintread(byte[] dest, int timeout) Reads as many bytes as possible into the destination buffer.voidsetDTR(boolean value) Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.voidsetParameters(int baudRate, int dataBits, int stopBits, int parity) Sets various serial port parameters.voidsetRTS(boolean value) Sets the RTS (Request To Send) bit on the underlying UART, if supported.intwrite(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:
closein interfaceAutoCloseable- Specified by:
closein 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
falseif 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
falseif 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
falseif 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
falseif 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
falseif 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
falseif 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
trueif set, elsefalse - Throws:
IOException
-
getSupportedControlLines
Gets all control line supported flags.- Returns:
- EnumSet.contains(...) is
trueif supported, elsefalse - Throws:
IOException
-
purgeHwBuffers
purge non-transmitted output data and / or non-read input data- Parameters:
purgeWriteBuffers-trueto discard non-transmitted output datapurgeReadBuffers-trueto discard non-read input data- Returns:
trueif the operation was successful, orfalseif 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
-