How to read FTDI usb data continuously? - (FTDI works and is tested in the app)
I have this java code, when I click a button, I get my USB serial data using an FTDI chip (FT232R), I can display on my application screen the values ββthat I read, the FTDI chip works fine.
package com.application.i;
import java.io.UnsupportedEncodingException;
import jp.ksksue.driver.serial.FTDriver;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class FTDI extends Activity{
// [FTDriver] Object
FTDriver mSerial;
// [FTDriver] Permission String
private static final String ACTION_USB_PERMISSION = "jp.ksksue.tutorial.USB_PERMISSION";
Button btnRead;
TextView Monitor,Monitor2,Monitor3;
TextView val,val2,val3;
TextView indicator;
StringBuffer mText1 = new StringBuffer();
int i,len;
String Text=null, d1=null, d2=null, d3=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ftdisub);
btnRead = (Button) findViewById(R.id.btnRead);
Monitor = (TextView)findViewById(R.id.onitor);
Monitor2 = (TextView)findViewById(R.id.onitor2);
Monitor3 = (TextView)findViewById(R.id.onitor3);
val = (TextView)findViewById(R.id.dec);
val2 = (TextView)findViewById(R.id.dec2);
val3 = (TextView)findViewById(R.id.dec3);
indicator = (TextView)findViewById(R.id.indicator);
// [FTDriver] Create Instance
mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));
// [FTDriver] setPermissionIntent() before begin()
PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
mSerial.setPermissionIntent(permissionIntent);
// [FTDriver] Open USB Serial
if(mSerial.begin(FTDriver.BAUD115200)) {
btnRead.setEnabled(true);
Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy() {
super.onDestroy();
// [FTDriver] Close USB Serial
mSerial.end();
}
public void onReadClick(View view) throws UnsupportedEncodingException {
// [FTDriver] Create Read Buffer
byte[] rbuf = new byte[27]; // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte
// [FTDriver] Read from USB Serial
len = mSerial.read(rbuf);
for(i=0; i<len; i++) {
mText1.append((char) rbuf[i]);
}
Text = mText1.toString(); //Text=string with the buffer value.
String[] Splited = Text.split("#");
Long r = Long.parseLong(Splited[1], 16);
Float f1 = Float.intBitsToFloat(r.intValue());
d1 = f1.toString();
val.setText(d1);
Long r2 = Long.parseLong(Splited[2], 16);
Float f2 = Float.intBitsToFloat(r2.intValue());
d2 = f2.toString();
val2.setText(d2);
Long r3 = Long.parseLong(Splited[3], 16);
Float f3 = Float.intBitsToFloat(r3.intValue());
d3 = f3.toString();
val3.setText(d3);
String[] Fields = {d1,d2,d3};
rbuf = null;
}
}
Poroblem: My application needs to constantly read (Background Service
) USB data, My indicator needs to leave it as Activity
to see all values ββread all the time, the problem is I can't get the code from onReadClick
. I tried to put it in the method onStart
, onResume
, FTDI()
. Didn't work yet.
To be clear, my goals are: 1. Make FTDI readable when I activate FTDI Activity
, without a button. 2. Create a "read loop" that receives data from the FTDI chip all the time.
source to share
SOLUTION: Second time on my request. Everything about the timing you need to set onReadClick
in onResume
and set the sleep time to initialize FTDIdriver
. I changed the code and now it works:
package com.application.i;
import java.io.UnsupportedEncodingException;
import jp.ksksue.driver.serial.FTDriver;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class FTDI extends Activity{
// [FTDriver] Object
FTDriver mSerial;
// [FTDriver] Permission String
private static final String ACTION_USB_PERMISSION = "jp.ksksue.tutorial.USB_PERMISSION";
Button btnRead;
TextView Monitor,Monitor2,Monitor3;
TextView val,val2,val3;
TextView indicator;
StringBuffer mText1 = new StringBuffer();
int i,len;
String Text=null, d1=null, d2=null, d3=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ftdisub);
btnRead = (Button) findViewById(R.id.btnRead);
Monitor = (TextView)findViewById(R.id.onitor);
Monitor2 = (TextView)findViewById(R.id.onitor2);
Monitor3 = (TextView)findViewById(R.id.onitor3);
val = (TextView)findViewById(R.id.dec);
val2 = (TextView)findViewById(R.id.dec2);
val3 = (TextView)findViewById(R.id.dec3);
indicator = (TextView)findViewById(R.id.indicator);
// [FTDriver] Create Instance
mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));
// [FTDriver] setPermissionIntent() before begin()
PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
mSerial.setPermissionIntent(permissionIntent);
// [FTDriver] Open USB Serial
if(mSerial.begin(FTDriver.BAUD115200)) {
// btnRead.setEnabled(true);
Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy() {
super.onDestroy();
// [FTDriver] Close USB Serial
mSerial.end();
}
public void onResume() {
super.onResume();
// [FTDriver] Create Read Buffer
byte[] rbuf = new byte[27]; // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// [FTDriver] Read from USB Serial
len = mSerial.read(rbuf);
for(i=0; i<len; i++) {
mText1.append((char) rbuf[i]);
}
Text = mText1.toString(); //Text=string with the buffer value.
String[] Splited = Text.split("#");
Long r = Long.parseLong(Splited[1], 16);
Float f1 = Float.intBitsToFloat(r.intValue());
d1 = f1.toString();
val.setText(d1);
Long r2 = Long.parseLong(Splited[2], 16);
Float f2 = Float.intBitsToFloat(r2.intValue());
d2 = f2.toString();
val2.setText(d2);
Long r3 = Long.parseLong(Splited[3], 16);
Float f3 = Float.intBitsToFloat(r3.intValue());
d3 = f3.toString();
val3.setText(d3);
String[] Fields = {d1,d2,d3};
rbuf = null;
}
}
PS: The most important thing I learned from this attempt is adb wifi debugging, when using USB Host, you cannot debug your phone because the USB port is occupied by the accessory. A simple solution is found here: Debug at the end of the link. After adb connect <device-ip-address>:5555
when entering the IP address of the phone, press enter and after confirming the message, disconnect the USB port from your phone and click run
on Eclipse. Just follow the steps. If you need to set your phone to USB cable debugging again, turn your phone off and on again until you can deal with adb. Adb prevents the phone from recognizing your computer. Restarting your phone will force everything back to normal, if you are not already familiar with adb functions just close and power on.
Hope this helped someone.
source to share