problems with serial reading ...

hi all ;))

e*

hey eloi,

the problem is that the arduino is sending data faster than the OF app can read it.

because serial works like a buffer the data starts to build up and the OF app is then reading older and older data.

The thing to do is if you need to read say 3 bytes: in OF put the read in a while loop, reading 3 bytes at a time, until the buffer is empty and then use the last 3 bytes as your values - this will be the most recent data.

You can actually see an example of the is the serialExample in examples/ of 005

Hope that helps!
Theo

hi theo

thanks for your comment …

well yes, the behaviour it’s totally like a buffer overrun … BUT … the fact is that arduino has a delay of 70ms between sent data (the code i’m using is from the book HowToMakeThings).

how is possible that u get a buffer overrun if OF it’s running at 60 fps (16 ms x loop) and Arduino is sending at arround 14 fps (70ms) … ¿?

i can’t figure out why there’s a buffer overrun …

i already tried the while option … what i’ve made is to send an ‘x’ as final message char . then in update i made a while (!=x) read … but in this configuration, the framerate of the OF app lowers to 15fps as expected (because of the Arduino 70 ms delay!) … ?¿

so … something strange is going on … mmmm

any other ideas ?

e+

Hey Eloi,

while (!=x) would not work so well because say there is 50 messages in the queue then it would be

973x
973x
922x
922x
922x

and the while would stop on the first x

you really need to read till the buffer is empty and then grab the last message that is the right size.

you might also try sending data at 9600 and see if that is an issue.

one helpful thing to do is if you are sending a 0-1024 value is to convert the number into two bytes which represents the full range of the number being sent.

that way if you are sending the value 5, 120 or 1024 you are always reading the same amount of data (two bytes). this makes things a lot easier as you don’t have to then send a terminating character.

also zach is a big fan of call and response where OF says to arduino give me a value and arduino only sends back a value when it has received this message.

the serialExample has an example of this behavior and it does help with a lot of the issues you are having related to timing.

cheers,
theo

hi theo !

ok cool. Thanks for your answer . We’re working with Alvaro Cassinelli on a virtual Arduino class to be able to deal nicely with arduino … so a threaded with hand-shaking and command queries …

i’ll keep on it and we’ll code that and post it soon …

thanks for your info !!!:wink:

e*