Search code examples
androidusbserialadafruit

How to read for data from CDC device connected to Android using usb-serial-for-android?


I'm using this library to send and receive data from a Feather 32u4 device. The Feather sends some canned data when the port is opened, and then echos back what is sent to it.

I'm having issues understanding how the read and write methods work in the library. I'm pretty sure I'm using write correctly, but I don't know about read. The returned length when read is used is always 0, and I am using the correct baudrate. I'm just not totally sure how it should be used. I'm still trying to figure out the event driven option also.

private void DoTheThing () {

      String textMessage = "";

      UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);



      // Find available drivers for Feather board.
      ProbeTable customTable = new ProbeTable();
      customTable.addProduct(0x239a, 0x800c, CdcAcmSerialDriver.class);
      UsbSerialProber prober = new UsbSerialProber(customTable);
      List<UsbSerialDriver> drivers = prober.findAllDrivers(manager);
      if (drivers.isEmpty()) {
         textMessage += "Could not find any drivers.\n";
         m_textView.setText(textMessage);
         return;
      }

      textMessage += "Driver found\n";
      m_textView.setText(textMessage);

      UsbDeviceConnection connection = null;
      UsbSerialDriver driver = drivers.get(0);
      PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(INTENT_ACTION_GRANT_USB), 0);
      manager.requestPermission(driver.getDevice(), usbPermissionIntent);
      try {
         connection = manager.openDevice(driver.getDevice());
      } catch (Exception e) {
         textMessage += e + "\n";
         m_textView.setText(textMessage);
      }
      if (connection == null) {
         // add UsbManager.requestPermission(driver.getDevice(), ..) handling here
         textMessage += "Could not open device.\n";
         m_textView.setText(textMessage);
         return;
      }

      textMessage += "Device opened\n";
      m_textView.setText(textMessage);

      UsbSerialPort port = driver.getPorts().get(0);
      // Most devices have just one port (port 0)
      try {
         port.open(connection);
         port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
      } catch (IOException e) {
         textMessage += e + "\n";
         m_textView.setText(textMessage);
         return;
      }

      textMessage += "Connection established\n";
      m_textView.setText(textMessage);

      // Get received data on connection.
      int length = -2;
      byte[] receivedData = new byte[64];
      try {
         length = port.read(receivedData, 2000);
      } catch (Exception e) {
         textMessage += "Oops: " + e + "\n";
         m_textView.setText(textMessage);
      }

      String data = new String(receivedData);
      textMessage += "Data recieved: " + length + " " + data + "\n";
      m_textView.setText(textMessage);


      // Send canned data.
      length = -2;
      try {
         length = port.write("a".getBytes(), 2000);
      } catch (Exception e) {
         textMessage += "Oops: " + e + "\n";
         m_textView.setText(textMessage);
      }

      textMessage += "Data sent " + length + "\n";
      m_textView.setText(textMessage);

      // Get received data.
      length = -2;
      receivedData = new byte[64];
      try {
         length = port.read(receivedData, 2000);
      } catch (Exception e) {
         textMessage += "Oops: " + e + "\n";
         m_textView.setText(textMessage);
      }

      data = new String(receivedData);
      textMessage += "Data recieved: " + length + " " + data;
      m_textView.setText(textMessage);

   }

enter image description here

EDIT

Getting a hard crash when I set the DTR flag to true on my port.

  m_port = driver.getPorts().get(0);

  // Most devices have just one port (port 0)
  try {
     m_port.setDTR(true);   //  <-- hard crash
     m_port.open(connection);
     m_port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
  } catch (IOException e) {
     m_textMessage += e + "\n";
     m_textView.setText(m_textMessage);
     return;
  }

Could m_port still be uninitialized when it gets to that point?

Derp.


Solution

  • length is 0 when nothing is received within the 2000 msec timeout.

    For CDC devices like the 32u4 you typically have to set DTR line as mentioned here