Giter Site home page Giter Site logo

Comments (11)

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
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 [email protected] on 6 Nov 2012 at 12:39

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
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 [email protected] on 21 Jan 2013 at 1:25

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
[deleted comment]

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
would need to check your code...

Original comment by [email protected] on 20 May 2013 at 8:07

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
[deleted comment]

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
[deleted comment]

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
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 [email protected] on 28 May 2014 at 8:04

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
Can you please send me your code to check?

Original comment by [email protected] on 29 May 2014 at 6:45

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
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 [email protected] on 29 May 2014 at 5:10

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
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 [email protected] on 30 May 2014 at 2:39

from android-serialport-api.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 21, 2024
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 [email protected] on 4 Jun 2014 at 5:24

from android-serialport-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.