Only first value is read correctly

edited April 2015 in Arduino

So, after the selfless help from you guys I finally managed to complete my first project which reads a couple of RFID tags and stores their key. I just wanted to see the keys in the console, but except for the first value, all others are scrambled, like in one or two lines etc. And I can't find what is wrong. Please do help me out..

import processing.serial.*;

PrintWriter output;

Serial myPort;

void setup()

{

output= createWriter("data.txt");

myPort=new Serial(this,"COM1",9600);

}

void draw()

{

if(myPort.available()>0)

 {


   String rfid=myPort.readString();


     if(rfid!=null)


         {


           trim(rfid); 


           output.println(rfid); println(rfid);


           logger.println(rfid);


           logger.println(" ");


                     output.flush();


                     output.close();



         }


 }

}

void keyPressed()

{

logger.flush();

logger.close();

exit();

}

Answers

  • Gotoloop I have not included your timestamp method... its a bit too much for a beginner... and Philo..,i guess I did the posting wrong again.. sorry.. :-P

  • Oook... Now that's strange.. I put a delay(50) before if(myPort.available>0) and after String=rfid myPort.readString() and note everything seems to be fine.. :-)

  • edited April 2015
  • edited April 2015

    I have not included your timestamp method... it's a bit too much for a beginner...

    That method merely returned a String to be used as the name for your log save files!
    So you avoid overwriting the fixed-name file by having a diff. filename based on current time!
    Here's a more direct approach, applying it to createWriter():

    // forum.processing.org/two/discussion/10200/only-first-value-is-read-correctly
    
    import processing.serial.Serial;
    Serial myPort;
    PrintWriter output;
    
    import java.util.Date;
    import java.text.SimpleDateFormat;
    SimpleDateFormat date = new SimpleDateFormat("yyyy-MMM-dd.HH_mm_ss");
    
    void setup() {
      frameRate(1);
    
      myPort = new Serial(this, "COM1", 9600);
    
      File logFile = dataFile(date.format(new Date()) + ".txt");
      output = createWriter(logFile);
      println(logFile);
    }
    
  • edited December 2017

    The original w/ a separate call to getTimeStamp() method for comparison: O:-)

    // Forum.Processing.org/two/discussion/10067/how-to-prevent-file-overwriting#Item_3
    // Docs.Oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
     
    static final String PATTERN = "yyyy-MMM-dd.HH_mm_ss", EXT = ".log";
    String timeStamp, logSavePath;
     
    void setup() {
      timeStamp = getTimeStamp(PATTERN);
      logSavePath = dataPath(timeStamp) + EXT;
     
      println(timeStamp);
      println(logSavePath);
     
      exit();
    }
     
    static final String getTimeStamp(String pattern) {
      return new java.text.SimpleDateFormat(pattern).format(new java.util.Date());
    }
    
  • hm.. ok .. thanks with that.. but how about this issue?? analysing the output data.txt i thought it would be an issue with the delays, that's why i wrote those inside draw().. never knew we shouldn't wrote delays inside draw.. but it kind of solved the problem..

  • by the way, is there any method to suspend arduino from executing commands in loop until it receives a serial in data from processing?

Sign In or Register to comment.