Question on Seriel.read() (timing)

edited May 2016 in Arduino

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.

Tagged:

Answers

  • edited May 2016

    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

  • edited May 2016

    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:

    import processing.serial.*;
    
    Serial myPort;
    int bps=1000;                           //Baudrate of Arduino.
    int seconds_of_data=2;                  //Size of the buffer in Seconds of data
    int bufferSize=(bps/8)*seconds_of_data; //in BYTES, here: set to get 2seconds of data
    
    int tic=1;                //used to change the name of the output FILE.TXT so outside program can read.
    String filename="1.txt";  
    
    byte[] Temp_buffer = new byte[bufferSize]; //temporary buffer of bytes
    int[] Temp_IntArray = new int[bufferSize]; //converted bytes to int
    PrintWriter output;                     
    
    
    void setup() {
      printArray(Serial.list());
      myPort= new Serial(this, Serial.list()[0], bps); //in BITS/s
      myPort.buffer(bufferSize);                       //serialEvent will wait until buffer reach desired size
      println("start");
    }
    
    void draw() {}
    
    void serialEvent(Serial myPort) {
    
      //filename goes from 1.txt to 10.txt
      tic++;  
      if (tic==11) {tic = 1;}
      filename = String.valueOf(tic) + ".txt";
      output = createWriter(filename); 
    
    
      while (myPort.available() > 0) {
        Temp_buffer = myPort.readBytes(); //fills the Temp_buffer
    
        if (Temp_buffer != null) {
    
          for (int i=0; i<bufferSize; i++) { //converts Temp_buffer of bytes to int array
            Temp_IntArray[i]=Temp_buffer[i];
            output.println(Temp_IntArray[i]);
          }
    
    
          output.flush(); // Writes the remaining data to the file
          output.close(); // Finishes the file  
          myPort.clear(); // Clears Processing buffer.
        }
      }
    
      //println(millis());
    }
    

    Thanks!

  • edited May 2016

    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:

    int bps=38400;                          //Baudrate of Arduino.
    int Bps=bps/8;                          //Byte rate
    int seconds_of_data=1;                  //Size of the buffer in Seconds of data
    int bufferSize=(Bps)*seconds_of_data;   //in BYTES, here: set to get 1seconds of data
    

    Using:

    `  myPort.buffer(bufferSize);                       //serialEvent will wait until buffer reach desired size (in Bytes)
    

    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?

Sign In or Register to comment.