1

This code is able to make the android device as a USB host for the hardware model. It also can read data from the hardware correctly in Main Activity. However, as soon as I moved it to another activity, everything still works but the data reading is incorrect. For instance, I'm trying to write the data read into file. First activity is to input filename and just a button to send to another activity. The code below is in the second activity

public class Temp extends Activity {
private FileOutputStream outputStream;

public static D2xxManager ftD2xx= null;

Handler mHandler = new Handler();

FT_Device ftDev = null;
int devCount = 0;
UsbDevice device = null;
TextView Text =null;
String temp = null;

_4DPoint P = null;

 int rd = 0;
byte[] byt = null;
byte[] Fdata = null;

String outp = "";
String From_Serial = "";
int Min = -1;

String fileName;
Context c;

final Runnable updateResults = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
         Text.setText("" + Min + '\n' + temp);          
    }
};

public void getData(){
    try {

        outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
         byt = new byte[256];//{(byte)'a','b','c','d',};
         Toast.makeText(getBaseContext(), "start " + fileName , Toast.LENGTH_LONG).show();


        Text = (TextView)findViewById(R.id.test2);

        device = (UsbDevice) getIntent().getParcelableExtra("USB");
        ftD2xx = D2xxManager.getInstance(c);
        ftD2xx.addUsbDevice(device);


        devCount = ftD2xx.createDeviceInfoList(c);
            if (devCount > 0) {
                ftDev = ftD2xx.openByUsbDevice(c, device);
            }
            if( ftDev.isOpen() == true ) {
                ftDev.setBitMode((byte)0 , D2xxManager.FT_BITMODE_RESET);
                ftDev.setBaudRate(38400);
                ftDev.setDataCharacteristics(D2xxManager.FT_DATA_BITS_8, D2xxManager.FT_STOP_BITS_1, D2xxManager.FT_PARITY_NONE);
                ftDev.setFlowControl(D2xxManager.FT_FLOW_NONE, (byte) 0x0b, (byte) 0x0d);

                Thread t = new Thread() {
                    public void run() {

                        int i;
                        while(true){
                            rd=0;

                            while (rd==0){
                                    rd = ftDev.read(byt, 14);
                            }

                            for(i=0; i<rd; i++) 
                                outp += (char)byt[i];

                            From_Serial = new String(outp);
                            P = new _4DPoint(From_Serial);
                            temp = String.format("%s: %f %f %f %f %d\n", From_Serial, P.R, P.G, P.B, P.L, P.camera);
                            try {
                                outputStream.write(temp.getBytes());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            outp = "";

                            mHandler.post(updateResults);
                        }
                    }
                };

                t.start();
            }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (D2xxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color);
    // Show the Up button in the action bar.
    setupActionBar();
    Intent intent = getIntent();
    fileName = intent.getStringExtra("File Name");
    c = this;
    getData();
}

The set up should be fine since it's reading data from hardware, but the data read is incorrect. Also, I'm wondering why we need to create new thread while reading data. I tried not creating new thread and it didn't work well, but still have no idea why? I tried to contact the person who wrote the code to read data but no reply. Any help would be really appreciated :)

1 Answers1

0

You state that you receive data, therefor I think you should look at your ftDev settings. Try for example to set ftDev.setBaudRate(115200) (this worked for me) or try playing with your other ftDev Settings a little bit.

The settings I use in my programm are:

int baudRate = 115200;
byte stopBit = 1; /*1:1stop bits, 2:2 stop bits*/
byte dataBit = 8; /*8:8bit, 7: 7bit*/
byte parity = 0;  /* 0: none, 1: odd, 2: even, 3: mark, 4: space*/
byte flowControl = 1; /*0:none, 1: flow control(CTS,RTS)*/

If this won't work, it is wise to first check this data communication with a computer program e.g. or to analyse the incomming 'wrong' data.