2012-10-28 4 views
10

Ich habe eine Frage zum Lesen von USB-Daten für FTDI-Geräte geschrieben. Transferring data USB Ich habe diesen Code für produktiv Gerät, und der USB kann erkannt werden, usw., außer dass die conn.bulkTransfer() -1 gibt.Datenübertragung von USB (produktiv) zu Android-Gerät

private class UsbRunnable implements Runnable { 
    private final UsbDevice mDevice; 

    UsbRunnable(UsbDevice dev) { 
     mDevice = dev; 
    } 


    public void run() {//here the main USB functionality is implemented 
     UsbDeviceConnection conn = mUsbManager.openDevice(mDevice); 
     if (!conn.claimInterface(mDevice.getInterface(1), true)) { 
      l("in run(), no connection"); 
      return; 
     } 
     l("in run(), connection"); 



     conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset 
     // mConnection.controlTransfer(0×40, 
     // 0, 1, 0, null, 0, 
     // 0);//clear Rx 
     conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx 
     conn.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);// flow 
       // control 
       // none 
     conn.controlTransfer(0x40, 0x03, 0xC04E, 0, null, 0, 0);// baudrate 
       // 38400 
     conn.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);// data bit 
       // 8, parity 
       // none, 
       // stop bit 
       // 1, tx off 




     UsbEndpoint epIN = null; 
     UsbEndpoint epOUT = null; 

     byte counter=0; 
    //conn.bulkTransfer(epOUT, new byte[]{msData}, 1, 0); 

     UsbInterface usbIf = mDevice.getInterface(0); 
     for (int i = 0; i < usbIf.getEndpointCount(); i++) { 
      l("EP: " 
        + String.format("0x%02X", usbIf.getEndpoint(i) 
          .getAddress())); 
      l("Type:"+usbIf.getEndpoint(i).getType()); 
      if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_INT) { 
       l("Bulk Endpoint"); 
       l("direction: "+usbIf.getEndpoint(i).getDirection()); 
       if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) 
        epIN = usbIf.getEndpoint(i); 

      } 
      else l("no bulk"); 
      l("epIN: "+epIN.toString()); 
     } 

     for (;;) {// this is the main loop for transferring 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      // This is where it is meant to receive 
      byte[] buffer = new byte[38400]; 

      StringBuilder str = new StringBuilder(); 

      l("bulk transfer thing: " + conn.bulkTransfer(epIN, buffer, 38400, 1000)); 

      if (conn.bulkTransfer(epIN, buffer, 38400, 1000) > 0) { 
       l("bulk transfer is success"); 
       for (int i = 2; i < 64; i++) { 
        if (buffer[i] != 0) { 
         str.append((char) buffer[i]); 
        } else { 
         l(str); 
         break; 
        } 
       } 

      } 
      // this shows the complete string 
      l(str); 


      if (mStop) { 
       mConnectionHandler.onUsbStopped(); 
       return; 
      } 
      l("sent " + counter); 
       counter++; 
       counter = (byte) (counter % 16); 
     } 

    } 
} 

// END MAIN LOOP 
private BroadcastReceiver mPermissionReceiver = new PermissionReceiver(
     new IPermissionListener() { 

      public void onPermissionDenied(UsbDevice d) { 
       l("Permission denied on " + d.getDeviceId()); 
      } 
     }); 

private static interface IPermissionListener { 
    void onPermissionDenied(UsbDevice d); 
} 

public final static String TAG = "USBController"; 

private void l(Object msg) { 
    Log.d(TAG, ">==< " + msg.toString() + " >==<"); 
} 

private void e(Object msg) { 
    Log.e(TAG, ">==< " + msg.toString() + " >==<"); 
} 

Kann mir jemand sagen, welche Änderungen ich machen muss? Vielen Dank im Voraus.

+0

Haben Sie eine Lösung gefunden? – user427969

Antwort

0

Es gibt mehrere Probleme.

Ich nehme an, dass die auskommentierte Zeile ist, wo Sie Probleme haben. Möglicherweise möchten Sie den Codeeinzug neu formatieren und weitere Informationen bereitstellen. In jedem Fall davon aus, dass das Problem hier auftritt:

byte counter=0; 
//conn.bulkTransfer(epOUT, new byte[]{msData}, 1, 0); 

Das unmittelbare Problem, das ich sehe - epOut, an dem Endpunkt, die Sie Daten senden null ist. Also wird es natürlich scheitern. Sie senden Daten an nichts. Ich folgte dem Link, der Sie zum Beispiel Code geschrieben, die Sie kopiert haben sie eine Linie zeigt:

epOUT = usbIf.getEndpoint(i); 

Ich würde zumindest deuten stark darauf hin, müssen Sie zunächst sicherstellen, dass Sie den ursprünglichen Code verstehen oder haben es funktioniert in Ihrem eigenen Programm, bevor Sie Änderungen vornehmen. NICHT kopieren Sie einfach die obige Zeile des Codes, um Ihr Problem zu "beheben".