We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I would have a question concerning the Serial.read() command.
I am continuously getting bytes from an arduino (Serial.print) at rates ~10'000bps. (I would like to sample a 1kHz waveform).
I would like to put all theses bytes in a array, which will periodically be saved into a .txt file.
The code would look like:
void draw() {
if (SerialPort.available()>0) {
mybyte= SerialPort.read();
bufferArray[counter]=mybyte;
}
counter++
if (counter==9999) {
counter=0;
some_save_function;}
}
My question would be:
While looping in the draw() function, does Processing catch all the bytes (i.e without missing a single one) and puts them in the buffer array or is it too slow to do so ? In which case I am guessing processing would just "grab" the byte available when Serial.read() is called.
Answers
https://forum.Processing.org/two/discussion/16692/buffer-file-from-arduino-serial-bytes-total-newbie
I don't have the hardware. These are just my guesses. Be warned! 8-X
byte
received:https://Processing.org/reference/libraries/serial/serialEvent_.html
https://Processing.org/reference/libraries/serial/Serial_buffer_.html
byte
value instead of number of bytes via bufferUntil():https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html
byte
: https://www.Arduino.cc/en/Serial/Writebyte
array w/ your desired length.Thanks GoToLoop these were interesting options and solved my problems!
I tried to avoid buffer() and readBytes() because I had problems converting the byteArray into a usable intArray without using a for loop. It turns out that for loops are very fast in Java and this is actually okay (I come from Matlab where you try to avoid this!).
I am now using readBytes() and buffer() to get chunks of the buffer without loosing bytes.
Here is the code:
Thanks!
EDIT: Nevermind, Serial.write() is sending Bytes and not bits . So it seems to be working okay! My apologies. All solved thanks!
If I could ask an other question:
I am now reading the full buffer when its size reach a certain value:
Using:
Will call the serialEvent(Serial myPort) when the buffer has the size: bufferSize. Using this setup I unsure that serialEvent(Serial myPort) is called when the buffer contains 2seconds worth of data.
I then save the buffer into a text file using myPort.readBytes()
This is where I am lost: The buffer do not contain (38'400 bps1 seconds) elements but (38'400/8 bps1 seconds).
When the buffer reaches a size of x Bytes it should contain the 8*x bits that Arduino sent no ? I understand that readBytes() is probably sending Bytes and not bits but then where did all the information go?