I have an FTDI UMFT201XB (I2C serial USB Interface) connected to an android 4.2 tablet through a OTG cable. The FTDI is connected to a PIC MCU. I have run the example TN-147 from FTDI and the device is recognized correctly.
I have modded the TN-147 example to read 12 bytes that the MCU has send to UMFT201XB.
The code is:
public void ReNew() {
byte [] databuf = new byte[5] ;
byte [] databuf1 = new byte[20];
device_time=0;
databuf1[0]= 0x64;
databuf1[2]= 0x64;
databuf[0]= 0x09;
if(ftdid2xx.createDeviceInfoList(Device_ControlFragmentContext) <= 0)
return;
ftDevice = ftdid2xx.openByIndex(Device_ControlFragmentContext, 0);
if (ftDevice == null) {
Toast.makeText(Device_ControlFragmentContext, "Not supported device",Toast.LENGTH_SHORT).show();
} else {
//ftDevice.setLatencyTimer((byte)240);
ftDevice.purge((byte) (D2xxManager.FT_PURGE_TX | D2xxManager.FT_PURGE_RX));
ftDevice.setBitMode((byte)0x33 , (byte)0x20);
iavailable1 = ftDevice.getQueueStatus();
ftDevice.write(databuf , 1);
ftDevice.restartInTask();
iavailable2 = ftDevice.getQueueStatus();
lenght_ = ftDevice.read(databuf1,iavailable2);
iavailable3 = ftDevice.getQueueStatus();
ftDevice.setBitMode((byte)0x32 , (byte)0x20);
ftDevice.close();
device_time=databuf1[0]+databuf1[1]+databuf1[2]+databuf1[3]+databuf1[4]+databuf1[5]+databuf1[6]+databuf1[7]+databuf1[8]+databuf1[9]+databuf1[10]+databuf1[11]+databuf1[12]+databuf1[13]+databuf1[14]+databuf1[16]+databuf1[17]+databuf1[18]+databuf1[15]+databuf1[19];
DEVICE_TIME.setText(String.valueOf(device_time));
days_view.setText(String.valueOf(iavailable1));
hours_view.setText(String.valueOf(iavailable2));
minutes_view.setText(String.valueOf(iavailable3));
seconds_view.setText(String.valueOf(lenght_));
SYSTEM_TIME.setText(String.valueOf(593));
/////////////////////////////////////////////////////////////////////////////////
}
ftDevice.close();
}
My problem is that on execution, device_time = 200 (dec)
, iavailable1 = 00
, iavailable2 = 12(Dec)
, and iavailable3 = 12(Dec)
.
Obviously, the 200 came from
databuf1[0]= 0x64;
databuf1[2]= 0x64;
The ftDevice.read(databuf1,iavailable2)
does not fetch any bytes to databuf1
. And the available bytes remains 12(Dec)
(iavailable3 = 12
).
Please help.
I had a similar problem. Try moving the read portion to a different thread. It worked for me when I passed the FT_Device
to an AsyncTask
's doInBackground()
method that read in the bytes.