Interface UsbSerialPort

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
CdcAcmSerialDriver.CdcAcmSerialPort, CommonUsbSerialPort

public interface UsbSerialPort extends Closeable
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    values for get[Supported]ControlLines()
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static 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 Type
    Method
    Description
    void
    Closes the port.
    boolean
    Gets the CD (Carrier Detect) bit from the underlying UART.
    Gets all control line values from the underlying UART, if supported.
    boolean
    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
    Gets the DSR (Data Set Ready) bit from the underlying UART.
    boolean
    Gets the DTR (Data Terminal Ready) bit from the underlying UART.
    int
    Port number within driver.
    boolean
    Gets the RI (Ring Indicator) bit from the underlying UART.
    boolean
    Gets the RTS (Request To Send) bit from the underlying UART.
    The serial number of the underlying UsbDeviceConnection, or null.
    Gets all control line supported flags.
    boolean
     
    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 data
    int
    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

  • 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, or null.
      Returns:
      value from UsbDeviceConnection.getSerial()
      Throws:
      SecurityException - starting with target SDK 29 (Android 10) if permission for USB device is not granted
    • open

      void open(android.hardware.usb.UsbDeviceConnection connection) throws IOException
      Opens and initializes the port. Upon success, caller must ensure that close() is eventually called.
      Parameters:
      connection - an open device connection, acquired with UsbManager.openDevice(UsbDevice)
      Throws:
      IOException - on error opening or initializing the port.
    • close

      void close() throws IOException
      Closes the port.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - on error closing the port.
    • read

      int read(byte[] dest, int timeout) throws IOException
      Reads as many bytes as possible into the destination buffer.
      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
    • write

      int write(byte[] src, int timeout) throws IOException
      Writes as many bytes as possible from the source buffer.
      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
    • setParameters

      void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException
      Sets various serial port parameters.
      Parameters:
      baudRate - baud rate as an integer, for example 115200.
      dataBits - one of DATABITS_5, DATABITS_6, DATABITS_7, or DATABITS_8.
      stopBits - one of STOPBITS_1, STOPBITS_1_5, or STOPBITS_2.
      parity - one of PARITY_NONE, PARITY_ODD, PARITY_EVEN, PARITY_MARK, or PARITY_SPACE.
      Throws:
      IOException - on error setting the port parameters
      UnsupportedOperationException - if not supported by a specific device
    • getCD

      boolean getCD() throws IOException
      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

      boolean getCTS() throws IOException
      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

      boolean getDSR() throws IOException
      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

      boolean getDTR() throws IOException
      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

      void setDTR(boolean value) throws IOException
      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

      boolean getRI() throws IOException
      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

      boolean getRTS() throws IOException
      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

      void setRTS(boolean value) throws IOException
      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

      EnumSet<UsbSerialPort.ControlLine> getControlLines() throws IOException
      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, else false
      Throws:
      IOException
    • getSupportedControlLines

      EnumSet<UsbSerialPort.ControlLine> getSupportedControlLines() throws IOException
      Gets all control line supported flags.
      Returns:
      EnumSet.contains(...) is true if supported, else false
      Throws:
      IOException
    • purgeHwBuffers

      boolean purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException
      purge non-transmitted output data and / or non-read input data
      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
    • isOpen

      boolean isOpen()
      Returns:
      the current state of the connection