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.UsbDeviceConnectionprotected final android.hardware.usb.UsbDeviceprotected final intprotected android.hardware.usb.UsbEndpointprotected android.hardware.usb.UsbRequestprotected byte[]Internal write buffer.protected final Objectprotected android.hardware.usb.UsbEndpointFields 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 TypeMethodDescriptionvoidclose()Closes the port.protected abstract voidcloseInt()abstract booleangetCD()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 booleangetCTS()Gets the CTS (Clear To Send) bit from the underlying UART.android.hardware.usb.UsbDeviceReturns the currently-bound USB device.abstract booleangetDSR()Gets the DSR (Data Set Ready) bit from the underlying UART.abstract booleangetDTR()Gets the DTR (Data Terminal Ready) bit from the underlying UART.intPort number within driver.abstract booleangetRI()Gets the RI (Ring Indicator) bit from the underlying UART.abstract booleangetRTS()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.booleanisOpen()voidopen(android.hardware.usb.UsbDeviceConnection connection) Opens and initializes the port.protected abstract voidopenInt(android.hardware.usb.UsbDeviceConnection connection) 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.protected intreadFilter(byte[] buffer, int len) abstract voidsetDTR(boolean value) Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.abstract voidsetParameters(int baudRate, int dataBits, int stopBits, int parity) Sets various serial port parameters.abstract voidsetRTS(boolean value) Sets the RTS (Request To Send) bit on the underlying UART, if supported.final voidsetWriteBufferSize(int bufferSize) Sets the size of the internal buffer used to exchange data with the USB stack for write operations.toString()intwrite(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, waitMethods 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:UsbSerialPortReturns the currently-bound USB device.- Specified by:
getDevicein interfaceUsbSerialPort
-
getPortNumber
public int getPortNumber()Description copied from interface:UsbSerialPortPort number within driver.- Specified by:
getPortNumberin interfaceUsbSerialPort
-
getSerial
Returns the device serial number- Specified by:
getSerialin 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:UsbSerialPortOpens and initializes the port. Upon success, caller must ensure thatUsbSerialPort.close()is eventually called.- Specified by:
openin 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:UsbSerialPortCloses the port.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceUsbSerialPort- Throws:
IOException- on error closing the port.
-
closeInt
protected abstract void closeInt() -
read
Description copied from interface:UsbSerialPortReads as many bytes as possible into the destination buffer.- Specified by:
readin 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:UsbSerialPortWrites as many bytes as possible from the source buffer.- Specified by:
writein 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:
isOpenin 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:UsbSerialPortSets various serial port parameters.- Specified by:
setParametersin 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:UsbSerialPortGets the CD (Carrier Detect) bit from the underlying UART.- Specified by:
getCDin interfaceUsbSerialPort- Returns:
- the current state, or
falseif not supported. - Throws:
IOException- if an error occurred during reading
-
getCTS
Description copied from interface:UsbSerialPortGets the CTS (Clear To Send) bit from the underlying UART.- Specified by:
getCTSin interfaceUsbSerialPort- Returns:
- the current state, or
falseif not supported. - Throws:
IOException- if an error occurred during reading
-
getDSR
Description copied from interface:UsbSerialPortGets the DSR (Data Set Ready) bit from the underlying UART.- Specified by:
getDSRin interfaceUsbSerialPort- Returns:
- the current state, or
falseif not supported. - Throws:
IOException- if an error occurred during reading
-
getDTR
Description copied from interface:UsbSerialPortGets the DTR (Data Terminal Ready) bit from the underlying UART.- Specified by:
getDTRin interfaceUsbSerialPort- Returns:
- the current state, or
falseif not supported. - Throws:
IOException- if an error occurred during reading
-
setDTR
Description copied from interface:UsbSerialPortSets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.- Specified by:
setDTRin interfaceUsbSerialPort- Parameters:
value- the value to set- Throws:
IOException- if an error occurred during writing
-
getRI
Description copied from interface:UsbSerialPortGets the RI (Ring Indicator) bit from the underlying UART.- Specified by:
getRIin interfaceUsbSerialPort- Returns:
- the current state, or
falseif not supported. - Throws:
IOException- if an error occurred during reading
-
getRTS
Description copied from interface:UsbSerialPortGets the RTS (Request To Send) bit from the underlying UART.- Specified by:
getRTSin interfaceUsbSerialPort- Returns:
- the current state, or
falseif not supported. - Throws:
IOException- if an error occurred during reading
-
setRTS
Description copied from interface:UsbSerialPortSets the RTS (Request To Send) bit on the underlying UART, if supported.- Specified by:
setRTSin interfaceUsbSerialPort- Parameters:
value- the value to set- Throws:
IOException- if an error occurred during writing
-
getControlLines
Description copied from interface:UsbSerialPortGets all control line values from the underlying UART, if supported. Requires less USB calls than calling getRTS() + ... + getRI() individually.- Specified by:
getControlLinesin interfaceUsbSerialPort- Returns:
- EnumSet.contains(...) is
trueif set, elsefalse - Throws:
IOException
-
getSupportedControlLines
Description copied from interface:UsbSerialPortGets all control line supported flags.- Specified by:
getSupportedControlLinesin interfaceUsbSerialPort- Returns:
- EnumSet.contains(...) is
trueif supported, elsefalse - Throws:
IOException
-
purgeHwBuffers
public boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException Description copied from interface:UsbSerialPortpurge non-transmitted output data and / or non-read input data- Specified by:
purgeHwBuffersin interfaceUsbSerialPort- 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
-