Android Open Source - UsbSerial C D C Serial Device






From Project

Back to project page UsbSerial.

License

The source code is released under:

MIT License

If you think the Android project UsbSerial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.felhr.usbserial;
/* w  w w .j a  v a 2  s.  c  o m*/
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbRequest;
import android.util.Log;

public class CDCSerialDevice extends UsbSerialDevice
{
  private static final String CLASS_ID = CDCSerialDevice.class.getSimpleName();

  private static final int CDC_REQTYPE_HOST2DEVICE = 0x21;
  private static final int CDC_REQTYPE_DEVICE2HOST = 0xA1;

  private static final int CDC_SET_LINE_CODING = 0x20;
  private static final int CDC_GET_LINE_CODING = 0x21;
  private static final int CDC_SET_CONTROL_LINE_STATE = 0x22;

  /***
   *  Default Serial Configuration
   *  Baud rate: 115200
   *  Data bits: 8
   *  Stop bits: 1
   *  Parity: None
   *  Flow Control: Off
   */
  private static final byte[] CDC_DEFAULT_LINE_CODING = new byte[] {
    (byte) 0x00, // Offset 0:4 dwDTERate
    (byte) 0x01,
    (byte) 0xC2,
    (byte) 0x00,
    (byte) 0x00, // Offset 5 bCharFormat (1 Stop bit)
    (byte) 0x00, // bParityType (None)
    (byte) 0x08  // bDataBits (8)
  };

  private static final int CDC_DEFAULT_CONTROL_LINE = 0x0003;
  private static final int CDC_DISCONNECT_CONTROL_LINE = 0x0002;

  private UsbInterface mInterface;
  private UsbEndpoint inEndpoint;
  private UsbEndpoint outEndpoint;
  private UsbRequest requestIN;

  public CDCSerialDevice(UsbDevice device, UsbDeviceConnection connection) 
  {
    super(device, connection);
  }

  @Override
  public boolean open() 
  {
    // Restart the working thread if it has been killed before and  get and claim interface
    restartWorkingThread();
    restartWriteThread();
    
    int interfaceCount = device.getInterfaceCount();
    int iIndex = 0;
    boolean keepGettingInterfaces = true;
    while(iIndex <= interfaceCount -1 && keepGettingInterfaces)
    {
      mInterface = device.getInterface(iIndex);
      if(mInterface.getInterfaceClass() != UsbConstants.USB_CLASS_CDC_DATA)
        mInterface = null;
      else
        keepGettingInterfaces = false;
      iIndex++;
    }
    
    if(mInterface == null)
    {
      Log.i(CLASS_ID, "There is no CDC class interface");
      return false;
    }
    
    if(connection.claimInterface(mInterface, true))
    {
      Log.i(CLASS_ID, "Interface succesfully claimed");
    }else
    {
      Log.i(CLASS_ID, "Interface could not be claimed");
      return false;
    }

    // Assign endpoints
    int numberEndpoints = mInterface.getEndpointCount();
    for(int i=0;i<=numberEndpoints-1;i++)
    {
      UsbEndpoint endpoint = mInterface.getEndpoint(i);
      if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
          && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
      {
        inEndpoint = endpoint;
      }else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
          && endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
      {
        outEndpoint = endpoint;
      }
    }
    
    if(outEndpoint == null || inEndpoint == null)
    {
      Log.i(CLASS_ID, "Interface does not have an IN or OUT interface");
      return false;
    }

    // Default Setup
    if(setControlCommand(CDC_SET_LINE_CODING, 0, CDC_DEFAULT_LINE_CODING) < 0)
    {
      Log.i(CLASS_ID, "CDC SET LINE CODING command could not be send correctly");
      return false;
    }if(setControlCommand(CDC_SET_CONTROL_LINE_STATE, CDC_DEFAULT_CONTROL_LINE, null) < 0)
    {
      Log.i(CLASS_ID, "CDC SET CONTROL LINE STATE command could not be send correctly");
      return false;
    }

    // Initialize UsbRequest
    requestIN = new UsbRequest();
    requestIN.initialize(connection, inEndpoint);

    // Pass references to the threads
    setThreadsParams(requestIN, outEndpoint);
    
    return true;
  }

  @Override
  public void close() 
  {
    setControlCommand(CDC_SET_CONTROL_LINE_STATE, CDC_DISCONNECT_CONTROL_LINE , null);
    killWorkingThread();
    killWriteThread();
    connection.close();
  }

  @Override
  public void setBaudRate(int baudRate) 
  {
    byte[] data = getLineCoding();

    data[3] = (byte) (baudRate & 0xff);
    data[2] = (byte) (baudRate >> 8 & 0xff);
    data[1] = (byte) (baudRate >> 16 & 0xff);
    data[0] = (byte) (baudRate >> 24 & 0xff);

    setControlCommand(CDC_SET_LINE_CODING, 0, data);
  }

  @Override
  public void setDataBits(int dataBits)
  {
    byte[] data = getLineCoding();
    switch(dataBits)
    {
    case UsbSerialInterface.DATA_BITS_5:
      data[6] = 0x05;
      break;
    case UsbSerialInterface.DATA_BITS_6:
      data[6] = 0x06;
      break;
    case UsbSerialInterface.DATA_BITS_7:
      data[6] = 0x07;
      break;
    case UsbSerialInterface.DATA_BITS_8:
      data[6] = 0x08;
      break;
    default:
      return;
    }

    setControlCommand(CDC_SET_LINE_CODING, 0, data);

  }

  @Override
  public void setStopBits(int stopBits) 
  {
    byte[] data = getLineCoding();
    switch(stopBits)
    {
    case UsbSerialInterface.STOP_BITS_1:
      data[4] = 0x00;
      break;
    case UsbSerialInterface.STOP_BITS_15:
      data[4] = 0x01;
      break;
    case UsbSerialInterface.STOP_BITS_2:
      data[4] = 0x02;
      break;
    default:
      return;
    }

    setControlCommand(CDC_SET_LINE_CODING, 0, data);


  }

  @Override
  public void setParity(int parity) 
  {
    byte[] data = getLineCoding();
    switch(parity)
    {
    case UsbSerialInterface.PARITY_NONE:
      data[5] = 0x00;
      break;
    case UsbSerialInterface.PARITY_ODD:
      data[5] = 0x01;
      break;
    case UsbSerialInterface.PARITY_EVEN:
      data[5] = 0x02;
      break;
    case UsbSerialInterface.PARITY_MARK:
      data[5] = 0x03;
      break;
    case UsbSerialInterface.PARITY_SPACE:
      data[5] = 0x04;
      break;
    default:
      return;
    }

    setControlCommand(CDC_SET_LINE_CODING, 0, data);

  }

  @Override
  public void setFlowControl(int flowControl) 
  {
    // TODO Auto-generated method stub

  }

  private int setControlCommand(int request, int value, byte[] data)
  {
    int dataLength = 0;
    if(data != null)
    {
      dataLength = data.length;
    }
    int response = connection.controlTransfer(CDC_REQTYPE_HOST2DEVICE, request, value, 0, data, dataLength, USB_TIMEOUT);
    Log.i(CLASS_ID,"Control Transfer Response: " + String.valueOf(response));
    return response;
  }

  private byte[] getLineCoding()
  {
    byte[] data = new byte[7];
    int response = connection.controlTransfer(CDC_REQTYPE_DEVICE2HOST, CDC_GET_LINE_CODING, 0, 0, data, data.length, USB_TIMEOUT);
    Log.i(CLASS_ID,"Control Transfer Response: " + String.valueOf(response));
    return data;
  }

}




Java Source Code List

com.felhr.deviceids.CP210xIds.java
com.felhr.deviceids.FTDISioIds.java
com.felhr.deviceids.PL2303Ids.java
com.felhr.usbserial.BLED112SerialDevice.java
com.felhr.usbserial.CDCSerialDevice.java
com.felhr.usbserial.CP2102SerialDevice.java
com.felhr.usbserial.FTDISerialDevice.java
com.felhr.usbserial.PL2303SerialDevice.java
com.felhr.usbserial.SerialBuffer.java
com.felhr.usbserial.UsbSerialDebugger.java
com.felhr.usbserial.UsbSerialDevice.java
com.felhr.usbserial.UsbSerialInterface.java
com.felhr.utils.HexData.java