Formatting of serial text output

edited November 2014 in Arduino

Hi I'm a processing beginner so please excuse my basic question if it's too obvious (I looked for an answer but couldn't find one before posting): I'm using Arduino uno to send IMU data to the serial port and would like to use process to output the data in text format. My problem is that the data in the text file is not following any formatting pattern - line breaks appear random. While it shouldn't be a problem to process the data in matlab, I am wondering why this happens. Any ideas? Thanks!

Arduino code (excerpt)

void setup()
{      
  int error;
  uint8_t c;


  Serial.begin(9600);
  // Initialize the 'Wire' class for the I2C-bus.
  Wire.begin();
.....
  // Send the data to the serial port
  Serial.print(F("DEL:"));              //Delta T
  Serial.print(dt, DEC);
  Serial.print(F("#ACC:"));              //Accelerometer angle
  Serial.print(accel_angle_x, 2);
  Serial.print(F(","));
  Serial.print(accel_angle_y, 2);
  Serial.print(F(","));
  Serial.print(accel_angle_z, 2);
  Serial.print(F("#GYR:"));
  Serial.print(unfiltered_gyro_angle_x, 2);        //Gyroscope angle
  Serial.print(F(","));
  Serial.print(unfiltered_gyro_angle_y, 2);
  Serial.print(F(","));
  Serial.print(unfiltered_gyro_angle_z, 2);
  Serial.print(F("#FIL:"));             //Filtered angle
  Serial.print(angle_x, 2);
  Serial.print(F(","));
  Serial.print(angle_y, 2);
  Serial.print(F(","));
  Serial.print(angle_z, 2);
  Serial.println(F(""));

  // Delay so we don't swamp the serial port
  delay(5);

Processing code: import processing.serial.*; Serial mySerial;

PrintWriter output;

void setup() {
   mySerial = new Serial( this, Serial.list()[5], 9600 );
   output = createWriter( "IMU_data.txt" );
            print(Serial.list());
            println(Serial.list()[5]);

}
void draw() {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readString();
         //println(value);
         //print(" ");
         output.println( value );
         //print(mySerial.available());
         //print(Serial.list());
         //print(" ");
    }}
    void keyPressed(){ //Press key to save data
    output.flush(); //Write remaining data
    output.close(); //Finish file
    exit(); //Stop program
}

Answers

  • Can anyone help me out with this please?

  • Answer ✓

    I did not understand how it should be,maybe

    String value = mySerial.readStringUntil('\n'); and trim off any whitespace with trim

    Arduino and enter various println in the right place

    :-?

  • Hi Camperos,

    Thanks for your help - I do realize that readstringuntil was one of the keys as well as other minor changes. Your help is much appreciated, considering that I forgot to post my desired format!

    See the current working code below:

    import processing.serial.*;
    Serial mySerial;
    
    
    PrintWriter output;
    String value = null;
    void setup() {
       mySerial = new Serial( this, Serial.list()[5], 9600 );
       value = mySerial.readStringUntil('\n');
       value = null;//discards first reading in case it starts in middle of string
    
       output = createWriter( "IMU_data.txt" );
                print(Serial.list());
                println(Serial.list()[5]);
    
    }
    void draw() {
        while (mySerial.available() > 0 ) {
             value = mySerial.readStringUntil('\n');
             if (value != null){
             //println(value);
             //print(" ");
             output.println( value );
             //print(mySerial.available());
             //print(Serial.list());
             //print(" ");
        }
      }
    }
        void keyPressed(){ //Press key to save data
        output.flush(); //Write remaining data
        output.close(); //Finish file
        exit(); //Stop program
    }
    
Sign In or Register to comment.