How to correctly save serial data to a text file

edited June 2015 in Library Questions

Hello!

The formatting of the saved text is a bit garbled.

Processing code:

void draw() {
  background(50);
  textFont(t, 20);
  fill(255);

  readSerial();
  drawBenchA();
}


void drawBenchA() {

}

void readSerial() {
  if (mySerial.available ()>0 ) {
    String myString = mySerial.readStringUntil('\n');
    if (myString!=null) {
      for (int i = 0; i<myString.length (); i++) {
        Str1 [i] = myString.charAt(i);
      }
    }
    mySerial.clear();
  }

  for (int i = 0; i < 15; i++)
  {
    output.print(Str1[i]);
  }

  textFont(t, 16);
  fill(255);
}


void keyPressed() {
  output.flush();  // Writes the remaining data to the file
  output.close();  // Finishes the file
  exit();  // Stops the program
}

Text file: Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Start Bench A velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-velope: 127546-Seconds 27546-Seconds 27546------

        27546------

        27546------

        27546------

        27546------

        27546------

        27546------

        27546------

        27546------

        27546------

        27546------

        27546------

How it should appear:

Start
Bench A
Envelope 127546-2
5 Seconds
----------------------------

I think this can be fixed with serialEvent, but not sure how.

Answers

  • Sorry the formatting got all buggy. Can't seem to figure out exactly how the code wrap works.

  • Thanks GoToLoop! Took me a second to realize that link was a tut on forum text formatting and not a solution to the serial data recording haha

  • Hi mgiara, try this

    void serialEvent(Serial mySerial) { 
      //void readSerial() {
      if (mySerial.available ()>0 ) {
        String myString = mySerial.readStringUntil('\n');
        if (myString!=null) {
          myString = trim(myString);
          for (int i = 0; i<myString.length (); i++) {
            Str1 [i] = myString.charAt(i);
          }
        }
        mySerial.clear();
      }
      output.print("Start    : "); 
      output.print("\r");
      output.print("Bench    : "); 
      output.print(Str1[0]);
      output.print("\r");
      output.print("Envelope : ");  
      output.print(Str1[1]);
      output.print(Str1[2]);
      output.print("\r");
      output.print("seconds  : ");
      output.print(Str1[6]);
      if (Str1[7]>= '0') { 
        output.print(Str1[7]);
      }
      if (Str1[8]>= '0') { 
        output.print(Str1[8]);
      } 
      output.print("\r");
      output.print("----------------");
      output.println();
    }
    
  • edited June 2015

    I sent for testing.....arduino

        char c = 'n';
        String bench = "A";
        int envelope = 12;
        String b = "Sf";
        int seconds = 0;
        void setup() {
          Serial.begin(9600);
        }
        void loop() {
       //cut
          delay(1000);
        }
    
  • edited June 2015

    Just an observation: There's no need for testing for available() within serialEvent() callback.
    Also setting up bufferUntil(ENTER) upon the Serial's instance makes readStringUntil(ENTER) redundant.
    1 more tip: Use noLoop() within setup() and redraw() at the end of serialEvent().

Sign In or Register to comment.