Package com.zebra.commoniolib
Class CommonUsbSerialPort
java.lang.Object
com.zebra.commoniolib.CommonUsbSerialPort
- All Implemented Interfaces:
UsbSerialPort
,Closeable
,AutoCloseable
- Direct Known Subclasses:
CdcAcmSerialDriver.CdcAcmSerialPort
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.zebra.commoniolib.UsbSerialPort
UsbSerialPort.ControlLine
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected android.hardware.usb.UsbDeviceConnection
protected final android.hardware.usb.UsbDevice
protected final int
protected android.hardware.usb.UsbEndpoint
protected android.hardware.usb.UsbRequest
protected byte[]
Internal write buffer.protected final Object
protected android.hardware.usb.UsbEndpoint
Fields inherited from interface com.zebra.commoniolib.UsbSerialPort
DATABITS_5, DATABITS_6, DATABITS_7, DATABITS_8, FLOWCONTROL_NONE, FLOWCONTROL_RTSCTS_IN, FLOWCONTROL_RTSCTS_OUT, FLOWCONTROL_XONXOFF_IN, FLOWCONTROL_XONXOFF_OUT, PARITY_EVEN, PARITY_MARK, PARITY_NONE, PARITY_ODD, PARITY_SPACE, STOPBITS_1, STOPBITS_1_5, STOPBITS_2
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the port.protected abstract void
closeInt()
abstract boolean
getCD()
Gets the CD (Carrier Detect) bit from the underlying UART.abstract EnumSet<UsbSerialPort.ControlLine>
Gets all control line values from the underlying UART, if supported.abstract boolean
getCTS()
Gets the CTS (Clear To Send) bit from the underlying UART.android.hardware.usb.UsbDevice
Returns the currently-bound USB device.abstract boolean
getDSR()
Gets the DSR (Data Set Ready) bit from the underlying UART.abstract boolean
getDTR()
Gets the DTR (Data Terminal Ready) bit from the underlying UART.int
Port number within driver.abstract boolean
getRI()
Gets the RI (Ring Indicator) bit from the underlying UART.abstract boolean
getRTS()
Gets the RTS (Request To Send) bit from the underlying UART.Returns the device serial numberabstract EnumSet<UsbSerialPort.ControlLine>
Gets all control line supported flags.boolean
isOpen()
void
open
(android.hardware.usb.UsbDeviceConnection connection) Opens and initializes the port.protected abstract void
openInt
(android.hardware.usb.UsbDeviceConnection connection) 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.protected int
readFilter
(byte[] buffer, int len) abstract void
setDTR
(boolean value) Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.abstract void
setParameters
(int baudRate, int dataBits, int stopBits, int parity) Sets various serial port parameters.abstract void
setRTS
(boolean value) Sets the RTS (Request To Send) bit on the underlying UART, if supported.final void
setWriteBufferSize
(int bufferSize) Sets the size of the internal buffer used to exchange data with the USB stack for write operations.toString()
int
write
(byte[] src, int timeout) Writes as many bytes as possible from the source buffer.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.zebra.commoniolib.UsbSerialPort
getDriver
-
Field Details
-
mDevice
protected final android.hardware.usb.UsbDevice mDevice -
mPortNumber
protected final int mPortNumber -
mConnection
protected android.hardware.usb.UsbDeviceConnection mConnection -
mReadEndpoint
protected android.hardware.usb.UsbEndpoint mReadEndpoint -
mWriteEndpoint
protected android.hardware.usb.UsbEndpoint mWriteEndpoint -
mUsbRequest
protected android.hardware.usb.UsbRequest mUsbRequest -
mWriteBufferLock
-
mWriteBuffer
protected byte[] mWriteBufferInternal write buffer. Guarded bymWriteBufferLock
.
-
-
Constructor Details
-
CommonUsbSerialPort
public CommonUsbSerialPort(android.hardware.usb.UsbDevice device, int portNumber)
-
-
Method Details
-
toString
-
getDevice
public android.hardware.usb.UsbDevice getDevice()Description copied from interface:UsbSerialPort
Returns the currently-bound USB device.- Specified by:
getDevice
in interfaceUsbSerialPort
-
getPortNumber
public int getPortNumber()Description copied from interface:UsbSerialPort
Port number within driver.- Specified by:
getPortNumber
in interfaceUsbSerialPort
-
getSerial
Returns the device serial number- Specified by:
getSerial
in interfaceUsbSerialPort
- Returns:
- serial number
-
setWriteBufferSize
public final void setWriteBufferSize(int bufferSize) Sets the size of the internal buffer used to exchange data with the USB stack for write operations. Most users should not need to change this.- Parameters:
bufferSize
- the size in bytes
-
open
Description copied from interface:UsbSerialPort
Opens and initializes the port. Upon success, caller must ensure thatUsbSerialPort.close()
is eventually called.- Specified by:
open
in interfaceUsbSerialPort
- Parameters:
connection
- an open device connection, acquired withUsbManager.openDevice(UsbDevice)
- Throws:
IOException
- on error opening or initializing the port.
-
openInt
protected abstract void openInt(android.hardware.usb.UsbDeviceConnection connection) throws IOException - Throws:
IOException
-
close
Description copied from interface:UsbSerialPort
Closes the port.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceUsbSerialPort
- Throws:
IOException
- on error closing the port.
-
closeInt
protected abstract void closeInt() -
read
Description copied from interface:UsbSerialPort
Reads as many bytes as possible into the destination buffer.- Specified by:
read
in interfaceUsbSerialPort
- 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
-
readFilter
- Throws:
IOException
-
write
Description copied from interface:UsbSerialPort
Writes as many bytes as possible from the source buffer.- Specified by:
write
in interfaceUsbSerialPort
- 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
-
isOpen
public boolean isOpen()- Specified by:
isOpen
in interfaceUsbSerialPort
- Returns:
- the current state of the connection
-
setParameters
public abstract void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException Description copied from interface:UsbSerialPort
Sets various serial port parameters.- Specified by:
setParameters
in interfaceUsbSerialPort
- Parameters:
baudRate
- baud rate as an integer, for example115200
.dataBits
- one ofUsbSerialPort.DATABITS_5
,UsbSerialPort.DATABITS_6
,UsbSerialPort.DATABITS_7
, orUsbSerialPort.DATABITS_8
.stopBits
- one ofUsbSerialPort.STOPBITS_1
,UsbSerialPort.STOPBITS_1_5
, orUsbSerialPort.STOPBITS_2
.parity
- one ofUsbSerialPort.PARITY_NONE
,UsbSerialPort.PARITY_ODD
,UsbSerialPort.PARITY_EVEN
,UsbSerialPort.PARITY_MARK
, orUsbSerialPort.PARITY_SPACE
.- Throws:
IOException
- on error setting the port parameters
-
getCD
Description copied from interface:UsbSerialPort
Gets the CD (Carrier Detect) bit from the underlying UART.- Specified by:
getCD
in interfaceUsbSerialPort
- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getCTS
Description copied from interface:UsbSerialPort
Gets the CTS (Clear To Send) bit from the underlying UART.- Specified by:
getCTS
in interfaceUsbSerialPort
- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getDSR
Description copied from interface:UsbSerialPort
Gets the DSR (Data Set Ready) bit from the underlying UART.- Specified by:
getDSR
in interfaceUsbSerialPort
- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getDTR
Description copied from interface:UsbSerialPort
Gets the DTR (Data Terminal Ready) bit from the underlying UART.- Specified by:
getDTR
in interfaceUsbSerialPort
- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
setDTR
Description copied from interface:UsbSerialPort
Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.- Specified by:
setDTR
in interfaceUsbSerialPort
- Parameters:
value
- the value to set- Throws:
IOException
- if an error occurred during writing
-
getRI
Description copied from interface:UsbSerialPort
Gets the RI (Ring Indicator) bit from the underlying UART.- Specified by:
getRI
in interfaceUsbSerialPort
- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
getRTS
Description copied from interface:UsbSerialPort
Gets the RTS (Request To Send) bit from the underlying UART.- Specified by:
getRTS
in interfaceUsbSerialPort
- Returns:
- the current state, or
false
if not supported. - Throws:
IOException
- if an error occurred during reading
-
setRTS
Description copied from interface:UsbSerialPort
Sets the RTS (Request To Send) bit on the underlying UART, if supported.- Specified by:
setRTS
in interfaceUsbSerialPort
- Parameters:
value
- the value to set- Throws:
IOException
- if an error occurred during writing
-
getControlLines
Description copied from interface:UsbSerialPort
Gets all control line values from the underlying UART, if supported. Requires less USB calls than calling getRTS() + ... + getRI() individually.- Specified by:
getControlLines
in interfaceUsbSerialPort
- Returns:
- EnumSet.contains(...) is
true
if set, elsefalse
- Throws:
IOException
-
getSupportedControlLines
Description copied from interface:UsbSerialPort
Gets all control line supported flags.- Specified by:
getSupportedControlLines
in interfaceUsbSerialPort
- Returns:
- EnumSet.contains(...) is
true
if supported, elsefalse
- Throws:
IOException
-
purgeHwBuffers
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException Description copied from interface:UsbSerialPort
purge non-transmitted output data and / or non-read input data- Specified by:
purgeHwBuffers
in interfaceUsbSerialPort
- 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
-