Export to GitHub

android-serialport-api - issue #33

Not Receving bytes are missing


Posted on Oct 17, 2012 by Happy Horse

Hi Friends

Using this API am communicating with one Embedded device, here my problem is whenever i'll send 20 bytes of data that device send it back 20 bytes sometimes am receiving properly but some time it is not coming please provide any solution for this

Comment #1

Posted on Nov 6, 2012 by Massive Horse

I have a similar problem which only seems to occur when sending bytes. I send (int)0-255 in bytes and receive a random amount of data between 0-255 in length. It is missing many integers. Sometimes it gets all the data but most of the time it does not.

Comment #2

Posted on Jan 21, 2013 by Helpful Hippo

Library itself works without any problem (tested on Android 4). What is probably causing you problems is your code.

onDataReceived(...) gives you only couple of bytes at once, so you need to use an input buffer to store data until you get all/enough of them. What is important here is that there is no guarantee on which thread onDataReceived(...) is executed. So it can happen that part of input is saved in one thread and another part on another thread... This is often cause of data leakage. The solution here is to make your input buffer static and so force all threads to share the same instance of it:

static byte[] input = new byte[0]; ..... protected void onDataReceived(final byte[] buffer, final int size) { input = concatArrays(input, subarray(buffer, 0, size)); ..... }

P.S. Please check that you do not have any other serial port app running as it could steal your data.

Comment #3

Posted on May 20, 2013 by Happy Elephant

Comment deleted

Comment #4

Posted on May 20, 2013 by Helpful Hippo

would need to check your code...

Comment #5

Posted on May 20, 2013 by Happy Elephant

Comment deleted

Comment #6

Posted on May 28, 2014 by Helpful Ox

Comment deleted

Comment #7

Posted on May 28, 2014 by Helpful Ox

I tried this code but it says the method subarray is undefined for the type runnable because I have my input initialization in a runonuithread method.

Comment #8

Posted on May 29, 2014 by Helpful Hippo

Can you please send me your code to check?

Comment #9

Posted on May 29, 2014 by Helpful Ox

Sure, here it is:

static byte[] input = new byte[0];

@Override
protected void onDataReceived(final byte[] buffer, final int size) {
    runOnUiThread(new Runnable() {
        public void run() {
            if (mReception != null) {
        // line below gives me the error
                        input = concatArrays(input,subarray(buffer, 0, size));
        //  mReception.append(new String(buffer, 0, size));
            }
        }
    });
}

Comment #10

Posted on May 30, 2014 by Helpful Ox

Ok. I implemented two methods subarray and concatArrays. Here is the following code

static byte[] input = new byte[0];

@Override
protected void onDataReceived(final byte[] buffer, final int size) {
    runOnUiThread(new Runnable() {
        public void run() {
            if (mReception != null) {
            //  String next = new String(buffer,0,size);
                input = concatArrays(input,subarray(buffer,0,size));
            //  mReception.append(new String(buffer, 0, size));
                mReception.setText(new String(input));
            }
        }


    });
}


public final static byte[] subarray(final byte[] array, final int offset, final int size) {
      final int realSize = array.length;
      final int demandedStart = offset;
      final int demandedEnd = demandedStart + size;

      if (demandedStart < 0 || demandedStart >= realSize) {
       throw new IllegalArgumentException(
         "Subarray start index is invalid");
      }
      if (demandedEnd > realSize) {
       throw new IllegalArgumentException("Subarray end index is invalid");
      }

      final int demandedSize = demandedEnd - demandedStart;
      final byte[] result = new byte[demandedSize];
      int j = 0;
      for (int i = demandedStart; i < demandedEnd; i++) {
       result[j] = array[i];
       j++;
      }

      return result;

}
public final static byte[] concatArrays(final byte[] first, final byte[] second) {
    final byte[] result = new byte[first.length + second.length];
      int j = 0;
      for (int i = 0; i < first.length; i++) {
       result[j] = first[i];
       j++;
      }

      for (int i = 0; i < second.length; i++) {
       result[j] = second[i];
       j++;
      }
      return result;

}

My question is: 1) I do have to press enter after typing in a character. I am not able to pass a sequence of characters. But while sending data to the terminal from the android device I am able to send a sequence of 8 bytes. 2) What does the 0 signify in the statement below: static byte[] input = new byte[0];

Comment #11

Posted on Jun 4, 2014 by Helpful Ox

I got the code to work. Didn't have to add the static keyword for input. I was just connected to the debug port of my android device instead of the rs232 port. Now everything works flawlessly. This is a great piece of software.

Status: New

Labels:
Type-Defect Priority-Medium