Serial out to .txt

edited March 2015 in Arduino

Hi, I have got this code which reads serial out from an arduino and save it to a text file. Unfortunately, the file data.txt happens to have some garbage values in addition to the serial output data. I can't find what's wrong with the code. If i remove the processing and take the output on arduino ide's serial monitor i get the correct data. I would really like a helping hand here. And oh., the output is from an rfid reader connected to arduino. Here's the code

import processing.serial.*; PrintWriter output;

Serial myPort; void setup() { output= createWriter("data.txt"); myPort=new Serial(this,"COM1",9600); } void draw() { while(myPort.available()>0) { String rfid=myPort.readString(); if(rfid!=null) { output.println(rfid); } } } void keyPressed() { output.flush(); output.close(); exit(); }

Answers

  • I am so sorry about the formatting.. Will post the code again

  • import processing.serial.*;

    PrintWriter output;

    Serial myPort;

    void setup()

    { output= createWriter("data.txt"); myPort=new Serial(this,"COM1",9600);

    }

    void draw()

    {

    while(myPort.available()>0)

     {
    
       String rfid=myPort.readString();
    
         if(rfid!=null)
    
             {
    
               output.println(rfid);
    
             }
    
     }
    

    }

    void keyPressed()

    {

    output.flush();

    output.close();

    exit();

    }

  • GoToloop thanks for the help mate.. With the formatting.. Will let you know the details about the code soon.. :)

  • It works.. trim().. Solved the space and break line issue but couldn't solve garbages appearing

  • I don't see that additional character when i use the println() alone. Can see the rfid as such in the processing output.. But when i use it to write to a text file a square with an 'x' inside it appears at the end along with the rfid's serial number.

  • edited March 2015

    Why don't you code something like a superTrim()?
    More specifically an alphaNumFilter() or a more specific filter set function?

    static final String alphaNumFilter(CharSequence s) {
      StringBuilder sb = new StringBuilder(s);
    
      for (int i = sb.length(); i-- != 0; )
        if (!Character.isLetterOrDigit(sb.charAt(i)))  sb.deleteCharAt(i);
    
      return sb.toString();
    }
    
  • "Will post the code again"
    Simpler, cleaner and less noisy to edit your first message...
    Beside, apparently you haven't fully understood how to format code properly... If the article was unclear, I appreciate remarks on it, so I can improve it.

  • I've got a remark: the CTRL+O or CTRL+K thing should be the very 1st thing mentioned! :P

  • edited March 2015

    Phil, thanks mate.. I am really sorry about the formatting part.. Browsing on mobile and i am new to this.. Posted the code from a notepad file in my phone.. (it's not that high end device either :-( ) anyway. If you could help me with the issue, i would be very much delighted.. :-)

  • Well, you haven't tell us if GoToLoop's solution works or not.
    Beside, I don't understand the "a square with an 'x' inside it appears at the end" part. Where does it appear?

  • GoToloop's solution worked.. Previously the rfid's serial number displayed in different lines.. With breaks in between.. Like 1400 in one line B4 in next and the rest in the next line.. Using trim function reduced it all in a single line.. But at the end there is that particular character.. It looks similar to this [x]..

Sign In or Register to comment.