ksksue / PhysicaloidLibrary

Android Library for communicating with physical-computing boards (e.g.Arduino, mbed)
http://www.physicaloid.com/
357 stars 151 forks source link

just read a part of message of file name from SD Card on 3D printer #20

Open drinkingcode opened 8 years ago

drinkingcode commented 8 years ago

when the count of files from SD Card on 3D printer is few, i can get all the correct file name of SD Card, but when the count of files from SD Card is many, i just get a part of file name , do you meet the same problem ? if you know that, please tell me

drinkingcode commented 8 years ago

the bug is here:

private Runnable mLoop = new Runnable() {
    @Override
    public void run() {
        int len=0;
        byte[] rbuf = new byte[USB_READ_BUFFER_SIZE];
        for (;;) {// this is the main loop for transferring

            try {
                len = mConnection.bulkTransfer(mEndpointIn,
                        rbuf, rbuf.length, 50);
            } catch(Exception e) {
                Log.e(TAG, e.toString());
            }

            if (len > 0) {
                mBuffer.add(rbuf, len);
                for(int i=0; i<len; i++){
                    Log.v("rbuf",""+((char)rbuf[i]));
                }
                onRead(len);
            }

            if (mReadThreadStop) {
                return;
            }

                              //当数据量大时,有时mEndpointIn的最大缓存已满,而此时在sleep中,所以
            try {                  //就会覆盖一部分数据,所以这里的这个延迟需要去掉;
                Thread.sleep(50);
            } catch (InterruptedException e) {
            }

        }
    } // end of run()
}; // end of runnable

it should be modifid as following :

private Runnable mLoop = new Runnable() {
    @Override
    public void run() {
        int len=0;
        byte[] rbuf = new byte[USB_READ_BUFFER_SIZE];
        for (;;) {// this is the main loop for transferring

            try {
                len = mConnection.bulkTransfer(mEndpointIn,
                        rbuf, rbuf.length, 50);
            } catch(Exception e) {
                Log.e(TAG, e.toString());
            }

            if (len > 0) {
                mBuffer.add(rbuf, len);
                for(int i=0; i<len; i++){
                    Log.v("rbuf",""+((char)rbuf[i]));
                }
                onRead(len);
            }

            if (mReadThreadStop) {
                return;
            }

            /*                    //当数据量大时,有时mEndpointIn的最大缓存已满,而此时在sleep中,所以
            try {                  //就会覆盖一部分数据,所以这里的这个延迟需要去掉;
                Thread.sleep(50);
            } catch (InterruptedException e) {
            }
            */

        }
    } // end of run()
}; // end of runnable