Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not Receving bytes are missing #33

Open
GoogleCodeExporter opened this issue Jun 24, 2015 · 11 comments
Open

Not Receving bytes are missing #33

GoogleCodeExporter opened this issue Jun 24, 2015 · 11 comments

Comments

@GoogleCodeExporter
Copy link

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

Original issue reported on code.google.com by vmpid...@gmail.com on 17 Oct 2012 at 5:07

@GoogleCodeExporter
Copy link
Author

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. 

Original comment by david.li...@cirrusresearch.co.uk on 6 Nov 2012 at 12:39

@GoogleCodeExporter
Copy link
Author

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.

Original comment by i...@eurotronik.si on 21 Jan 2013 at 1:25

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

would need to check your code...

Original comment by i...@eurotronik.si on 20 May 2013 at 8:07

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

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.

Original comment by susheel....@gmail.com on 28 May 2014 at 8:04

@GoogleCodeExporter
Copy link
Author

Can you please send me your code to check?

Original comment by i...@eurotronik.si on 29 May 2014 at 6:45

@GoogleCodeExporter
Copy link
Author

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));
                }
            }
        });
    }

Original comment by susheel....@gmail.com on 29 May 2014 at 5:10

@GoogleCodeExporter
Copy link
Author

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];




Original comment by susheel....@gmail.com on 30 May 2014 at 2:39

@GoogleCodeExporter
Copy link
Author

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. 

Original comment by susheel....@gmail.com on 4 Jun 2014 at 5:24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant