Comments (11)
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.
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.
[deleted comment]
from android-serialport-api.
would need to check your code...
Original comment by [email protected]
on 20 May 2013 at 8:07
from android-serialport-api.
[deleted comment]
from android-serialport-api.
[deleted comment]
from android-serialport-api.
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.
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.
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.
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.
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)
- Delay between outputs from the serial port is more..
- i could not find the ttyUSB0 under 'dev/' folder when i connect my android tablet with a PL-2303 adapter line HOT 1
- Read data with non-blocking I/O HOT 1
- MCU can not receive the data HOT 1
- how to set 128000 baud rate HOT 1
- After clicking on the console button, only cursor moves, characters are not displayed.
- Lost bytes HOT 1
- There is random delay in sending data and some bytes of data are lost
- Tx & Rx on Froyo, Update to Gingerbread - No Rx! HOT 3
- INTERFACING ANDROID PHONE WITH EXTERNAL BLUETOOTH HOT 1
- byte is missing!! HOT 3
- help me~~ recv keep missing bytes
- Bytes are getting lost while receiving -- Urgent Help
- I found the reason(why keep byte missing?).
- add support for stop bits and parity
- Android BeagleBone Black
- Open Serial Port for whole project
- I receive only 4 byte in input when i send some bytes
- Has GPL code included but listed as Apache licensed
- Setup crashes, Android Lollipop, Moto X Gen 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-serialport-api.