Class CommonUsbSerialPort

java.lang.Object
com.zebra.commoniolib.CommonUsbSerialPort
All Implemented Interfaces:
UsbSerialPort, Closeable, AutoCloseable
Direct Known Subclasses:
CdcAcmSerialDriver.CdcAcmSerialPort

public abstract class CommonUsbSerialPort extends Object implements UsbSerialPort
  • 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

      protected final Object mWriteBufferLock
    • mWriteBuffer

      protected byte[] mWriteBuffer
      Internal write buffer. Guarded by mWriteBufferLock.
  • Constructor Details

    • CommonUsbSerialPort

      public CommonUsbSerialPort(android.hardware.usb.UsbDevice device, int portNumber)
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getDevice

      public android.hardware.usb.UsbDevice getDevice()
      Description copied from interface: UsbSerialPort
      Returns the currently-bound USB device.
      Specified by:
      getDevice in interface UsbSerialPort
    • getPortNumber

      public int getPortNumber()
      Description copied from interface: UsbSerialPort
      Port number within driver.
      Specified by:
      getPortNumber in interface UsbSerialPort
    • getSerial

      public String getSerial()
      Returns the device serial number
      Specified by:
      getSerial in interface UsbSerialPort
      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

      public void open(android.hardware.usb.UsbDeviceConnection connection) throws IOException
      Description copied from interface: UsbSerialPort
      Opens and initializes the port. Upon success, caller must ensure that UsbSerialPort.close() is eventually called.
      Specified by:
      open in interface UsbSerialPort
      Parameters:
      connection - an open device connection, acquired with UsbManager.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

      public void close() throws IOException
      Description copied from interface: UsbSerialPort
      Closes the port.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface UsbSerialPort
      Throws:
      IOException - on error closing the port.
    • closeInt

      protected abstract void closeInt()
    • read

      public int read(byte[] dest, int timeout) throws IOException
      Description copied from interface: UsbSerialPort
      Reads as many bytes as possible into the destination buffer.
      Specified by:
      read in interface UsbSerialPort
      Parameters:
      dest - the destination byte buffer
      timeout - 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

      protected int readFilter(byte[] buffer, int len) throws IOException
      Throws:
      IOException
    • write

      public int write(byte[] src, int timeout) throws IOException
      Description copied from interface: UsbSerialPort
      Writes as many bytes as possible from the source buffer.
      Specified by:
      write in interface UsbSerialPort
      Parameters:
      src - the source byte buffer
      timeout - 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 interface UsbSerialPort
      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 interface UsbSerialPort
      Parameters:
      baudRate - baud rate as an integer, for example 115200.
      dataBits - one of UsbSerialPort.DATABITS_5, UsbSerialPort.DATABITS_6, UsbSerialPort.DATABITS_7, or UsbSerialPort.DATABITS_8.
      stopBits - one of UsbSerialPort.STOPBITS_1, UsbSerialPort.STOPBITS_1_5, or UsbSerialPort.STOPBITS_2.
      parity - one of UsbSerialPort.PARITY_NONE, UsbSerialPort.PARITY_ODD, UsbSerialPort.PARITY_EVEN, UsbSerialPort.PARITY_MARK, or UsbSerialPort.PARITY_SPACE.
      Throws:
      IOException - on error setting the port parameters
    • getCD

      public abstract boolean getCD() throws IOException
      Description copied from interface: UsbSerialPort
      Gets the CD (Carrier Detect) bit from the underlying UART.
      Specified by:
      getCD in interface UsbSerialPort
      Returns:
      the current state, or false if not supported.
      Throws:
      IOException - if an error occurred during reading
    • getCTS

      public abstract boolean getCTS() throws IOException
      Description copied from interface: UsbSerialPort
      Gets the CTS (Clear To Send) bit from the underlying UART.
      Specified by:
      getCTS in interface UsbSerialPort
      Returns:
      the current state, or false if not supported.
      Throws:
      IOException - if an error occurred during reading
    • getDSR

      public abstract boolean getDSR() throws IOException
      Description copied from interface: UsbSerialPort
      Gets the DSR (Data Set Ready) bit from the underlying UART.
      Specified by:
      getDSR in interface UsbSerialPort
      Returns:
      the current state, or false if not supported.
      Throws:
      IOException - if an error occurred during reading
    • getDTR

      public abstract boolean getDTR() throws IOException
      Description copied from interface: UsbSerialPort
      Gets the DTR (Data Terminal Ready) bit from the underlying UART.
      Specified by:
      getDTR in interface UsbSerialPort
      Returns:
      the current state, or false if not supported.
      Throws:
      IOException - if an error occurred during reading
    • setDTR

      public abstract void setDTR(boolean value) throws IOException
      Description copied from interface: UsbSerialPort
      Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.
      Specified by:
      setDTR in interface UsbSerialPort
      Parameters:
      value - the value to set
      Throws:
      IOException - if an error occurred during writing
    • getRI

      public abstract boolean getRI() throws IOException
      Description copied from interface: UsbSerialPort
      Gets the RI (Ring Indicator) bit from the underlying UART.
      Specified by:
      getRI in interface UsbSerialPort
      Returns:
      the current state, or false if not supported.
      Throws:
      IOException - if an error occurred during reading
    • getRTS

      public abstract boolean getRTS() throws IOException
      Description copied from interface: UsbSerialPort
      Gets the RTS (Request To Send) bit from the underlying UART.
      Specified by:
      getRTS in interface UsbSerialPort
      Returns:
      the current state, or false if not supported.
      Throws:
      IOException - if an error occurred during reading
    • setRTS

      public abstract void setRTS(boolean value) throws IOException
      Description copied from interface: UsbSerialPort
      Sets the RTS (Request To Send) bit on the underlying UART, if supported.
      Specified by:
      setRTS in interface UsbSerialPort
      Parameters:
      value - the value to set
      Throws:
      IOException - if an error occurred during writing
    • getControlLines

      public abstract EnumSet<UsbSerialPort.ControlLine> getControlLines() throws IOException
      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 interface UsbSerialPort
      Returns:
      EnumSet.contains(...) is true if set, else false
      Throws:
      IOException
    • getSupportedControlLines

      public abstract EnumSet<UsbSerialPort.ControlLine> getSupportedControlLines() throws IOException
      Description copied from interface: UsbSerialPort
      Gets all control line supported flags.
      Specified by:
      getSupportedControlLines in interface UsbSerialPort
      Returns:
      EnumSet.contains(...) is true if supported, else false
      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 interface UsbSerialPort
      Parameters:
      purgeWriteBuffers - true to discard non-transmitted output data
      purgeReadBuffers - true to discard non-read input data
      Returns:
      true if the operation was successful, or false if the operation is not supported by the driver or device
      Throws:
      IOException - if an error occurred during flush