0

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

Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
  • why have you a thread.sleep(50)? you may also want to look into having a timeout, and also think of a different way to achieve the 'for(;;)' loop ( a while(true) might be better) –  Oct 21 '14 at 12:23
  • 1
    What you are seeing sounds expected - you will get the data in pieces, and must put them back together yourself. – Chris Stratton Oct 21 '14 at 13:24

0 Answers0