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
Problem with the read callback from Arduino #37
Comments
Hi,
|
Thank you for your fast reply! I had already been buffering but it was troublesome. I'll see with the sync version. |
It is the last thing I added so it would be nice to receive your feedback about if the sync version works correctly. |
I will work on that today and get to you after that. |
So after doing some tests with the sync version, it seems to be working but I get a lot of 0 in between my bytes when I (sync)read. I suppose that's because i limited my byte array to 13 bytes and i just receive a certain amount below 13. The thing is that I send from the arduino exactly 13 bytes. So I still get a truncated packaged received from the Arduino. I did a simple test where I write 13 bytes from android every 2 seconds in summary : I will look further to see how I can improve my code. |
Java arrays are set to 0 by default so probably you are receiving less than 13 bytes when the timeout raises. Are you using UsbSerial 4.1.1 because that release solved an issue with the synchronous read in FTDI devices. I would check the arduino code too. |
Yes I am using the final release. So yes i suppose that it was because i was receiving less than what i expected that I had the 0s. But the biggest problem comes from receiving multiple package when sending only one. And this is the case for both methods (sync and callback). The code on Arduino seemed fine. I went back with the callback version because it was simpler to work with and improved my buffering to get what I want. I suppose the only solution is to buffer the received package. |
Set timeout to 0 :) |
OdysseusU, did you get the synchronous api to work? i wanted to attempt using it since I only need to keep reading a string of 13 characters from the Arduino. |
Yes do you have any problem with it? I didn't really have any. The main problem was that my frames were cut but that is device related. If you want to use synchronous api just put a while in there with a syncRead and changing everything open by syncOpen and close by syncClose. This should be good. |
Seems i do..so i only get one reading and thats it..i put the syncRead in the broadcastReceiver...not in a while loop just in there and then used tvappend to append to display..1st error is decoding ..not converting to string, 2nd error is getting just 1 read and thats it |
The decoding is a bit strange with android. Look into other methods to convert into string (don't use the method .toString()) rather create your String and put your encoding ( new String(bytes, encoding UTF8 for instance). I don't really remember how to catch the string again but I think the .toString() method isn't good. And the reading problem, are you sure your Arduino sends the data multiple times? Did you put the timeout to 0? this caused me some problem since it waits until the byte arrives and sometimes stops completely the application if nothing is sent (even if it was). Put a short timeout to be sure. But generally the library works as it was intended to. So the problems you encounter comes from java or your arduino. |
So.i did that and it cuts my string..it produces the string but only a quarter of it...atrange..its still splitting up the data |
Idk why it still splits it up..but it does. I set the size of the array to 14, because the string im expecting is A: 000 B: 000...so idk what to do |
Yup i've got the same problem. Your solutions are :
I took the second solution because i couldn't afford to wait. This is not library related as every library that does the serial with arduino does the same. It's device related (or Android related, don't know). |
So you really could use the async..but i.noticed i was losiglng data..so you read until you have 13 bytes..what if you receive extra bytes so its the next set of data.. |
Well you have to know when to stop the reading and do buffering right. But I never lose the data (only 1 every minute or so when sending every 50 - 100 ms) |
So you do lose a byte every min? Can i see how you did it |
I won't put the whole code here it would be useless since it's quite specific what I do.
What I do is quite simple. |
okay i tried something similar to this algorithm. in your arduino code, do you do serial.println..so theres a new line in the string, or just serial.print? |
Actually i use serial.write to write byte code. But you could serial.println and find the ´\n' character to find the end of frame |
It is actually simpler since you just need to find if your received bytes contains the ´\n' character and if it does process the buffer you have. But it is more optimized to not search for the ´/n' character if you already know the size of the frame you should receive. |
So can i just stick to the way you did it with serial.println in arduino? |
Yes the serial.println all it does is serial.print +'\n' |
byte [] data=new byte[0];
what am i doing wrong??? |
It's my fault I didn't detailed my variables. so the code would be :
Maybe that would work. Check every passage with a Log.i() to see if everything's ok. |
hm..its still not working.. i mean its not displaying the string! |
for clearBytes..you mean to set the global variable buffer to new byte[13] right? |
Oh it looks like youre right..it never returns true for when byte==character..hmm im not sure how to do that conversion. |
Right i let a byte[] buffer you can erase byte[] it was an error. |
Do you know why I am getting this error? java.lang.ArrayIndexOutOfBoundsException: src.length=2 srcPos=0 dst.length=13 dstPos=13 length=2 |
so it seems as though its trying to append to the buffer greater than 13 bytes..do i need to add some sort of check before appendBytes is called? |
Right thats because my buffer I create it with a length of 1024 to be sure not to have this kind of problem. You could add a check to be sure it never adds up to 13 or you can create the buffer at the beginning to 1024. Then for clearing you can do an Array.fill(buffer,0) to clear the buffer. |
So all you need to change in your code is in the clearbuffer change the new byte by array.fill and at the beginning create just once your buffer byte[] buffer = new byte[1024] |
So once found 13...then the extra stuff in the buffer, which is the next set of data is just gone? That doesnt seem like a good idea... |
Ohh actually nevermind.i understand now..sorry. i wl try this now and let you know. Thanks a loy |
Lot* |
it works..wooo..thank you so much! |
One more question for ya...i go to a new activity, a new intent, and its still running in the background...how do i stop the usb input..send a command to arduino? |
In the activity you should have the onPause method to override. In their you put a serial.close and on the onResume method you put the serial.open |
Okay sweet thanks. And when you want to recalibrate arduino, which is at the start of the activity, you could just send a 1 to the arduino telling it to perform thag, right? |
What do you mean by recalibrating? You mean reset? I think you can setDTR off and on for that |
Update : Solved all this issues after Buffering the Data received. Hello, I am using this library to read data from arduino and split it to different textviews. I do not know from read call back it gives the whole string to tvAppend function or just each characters it received ? Question is:
this gives characters received or the whole string ? |
@felHR85 |
hi, |
Hello, I am facing problem while reading with the help of synchronous API, could someone please post the code for it. Thanks in advance. |
Will you please give more details about your issues.what kind of bugs.
Hello, I am facing problem while reading with the help of synchronous API,
could someone please post the code for it. Thanks in advance.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#37 (comment)>,
or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AXTW_HyCjMuW9dRNOkN09V9vAk0rtKW9ks5tYU6QgaJpZM4H2U5h>
.
|
There are multiple solutiouns. If you don't have high performance-critical code, you can code something like below. You can check start and stop characters or byte size etc.
|
Works like magic. Thank you. |
Hi,
I have an issue in the read callback when connecting the arduino. It seems that I receive my bytes twice and sometime the callback is triggered but the package received is empty.
I only send a byte array with the arduino every 2 seconds and I just print what we receive with android.
Also I don't understand how the package is received. Sometimes I can get the whole array in one simple array and sometimes it's received truncated and the callback is called several time to get the whole array.
Thank you
The text was updated successfully, but these errors were encountered: