Search code examples
androidarduinousbftdi

Android USB reading Arduino return 19600


I am trying to code a Android Activity to perform a read data from Arduino Duemilanove. This Arduino model has a FTDI 232RL chip. I have studied the follow sites and answers:

  1. setting parity with controlTransfer method
  2. http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html
  3. http://read.pudn.com/downloads181/sourcecode/embed/842049/usb/serial/ftdi_sio.h__.htm and so on...

My code works fine to perform controlTransfer operation on Android USB device. But, on bulkTransfer the returned data is just 1 96 0... I already revised baudrate: Arduino serial runs on 57600 and the controlTransfer to. Follow a piece of my code:

        if(device != null){            
        UsbDeviceConnection conn = usbManager.openDevice(device);
        if (!conn.claimInterface(device.getInterface(0), true)) {
            return;
        }
        //configuring the usb device: https://stackoverflow.com/questions/8546099/setting-parity-with-controltransfer-method
        if(conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0) < 0){//reset
            Toast.makeText(getApplicationContext(), "Reset Fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0, 0x01, 0, null, 0, 0) < 0){//clear Rx
            Toast.makeText(getApplicationContext(), "Clean RX Fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0, 0x02, 0, null, 0, 0) < 0){//clear Tx
            Toast.makeText(getApplicationContext(), "Clean TX Fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0) < 0 ){ //flow control none            
            Toast.makeText(getApplicationContext(), "Flow Control fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0x03, 0x0034, 0, null, 0, 0) < 0){//baudrate 57600
            Toast.makeText(getApplicationContext(), "Baudrate fail", Toast.LENGTH_LONG).show();
        }
        if(conn.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0) < 0){ //data bit 8 parity none stop bit 1
            Toast.makeText(getApplicationContext(), "settings fail", Toast.LENGTH_LONG).show();
        }            

        Toast.makeText(getApplicationContext(), "Reading data...", Toast.LENGTH_LONG).show();            
        byte[] data = new byte[4096];
        if(conn.bulkTransfer(epIN, data, 4096, 5000) >= 0){
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                builder.append(data[i]);
            }                        
            Toast.makeText(getApplicationContext(), "Data: "+builder, Toast.LENGTH_LONG).show();            
            dispositivos.setText(builder);
        }            

        /*
        try{
            Thread.sleep(5000);                     
        }
        catch(InterruptedException e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Error: "+e.getMessage(), Toast.LENGTH_LONG).show();
        }*/
    }

Have anyone some idea or suggestion?

Tanks for help.


Solution

  • I found a lib that helped me to perform a Android communicate with a Arduino Duemilanove. This arduino model has a FTDI chip (232R). This chip is a USB UART interface. Then, i am using this lib:

    It helped me to make a communication between Android and Arduino in a simple way, with few lines of code.

    Thanks.