Not Receving bytes are missing
Opened this issue · 11 comments
GoogleCodeExporter commented
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 commented
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 commented
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 commented
[deleted comment]
GoogleCodeExporter commented
would need to check your code...
Original comment by i...@eurotronik.si
on 20 May 2013 at 8:07
GoogleCodeExporter commented
[deleted comment]
GoogleCodeExporter commented
[deleted comment]
GoogleCodeExporter commented
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 commented
Can you please send me your code to check?
Original comment by i...@eurotronik.si
on 29 May 2014 at 6:45
GoogleCodeExporter commented
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 commented
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 commented
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