I've created an android application to read and display serial data sent from a computer via a USB FTDI cable plugged into the phone using a USB OTG adapter.
I've managed to write a piece of code to display the data sent from the terminal program on my computer, now this works fine if I send one character but if I send a long string of data then it will display it on screen in multiple parts.
for example:
the string sent from the terminal: "data from terminal program"
data shown on the phone screen in multiple toasts: "dat" then "a from t" then "erminal pr" then "ogram"
code for reading data in:
private Runnable mLoop = new Runnable() {
@Override
public void run() {
int len;
final byte[] buf = new byte[256];
for (;;) {
len = mSerial.read(buf);
buf[len] = 0;
if(len > 0) {
for (int i= 0; i < len; ++i) {
mText.append((char) buf[i]);
}
mHandler.post(new Runnable() {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, mText.toString(), Toast.LENGTH_SHORT).show();
mText.setLength(0);
}
});
}
});
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if (mStop) {
mRunningMainLoop = false;
return;
}
}
}
}
};
I would be very grateful if someone could shed some light on this, thanks