2

I want to save data received serially on arduino to a text file using processing. Right now my code is for processing is as shown below.

import processing.serial.*;
Serial myPort;
int val;
void setup()
{
myPort = new Serial (this, "COM3", 1200);

}   

void draw()
{
if ( myPort.available() > 0)
{
val = myPort.read();
print((char)val);
}
}

The arduino receives aaaaaaaaa and it's also received by processing like that. What do I do to save it into a text file? Can someone modify my code? I'm a bit new to processing.

Also, if my received text was : aaaaaaaaaa bbbbbbbbbb

then how can i save the new paragraph as a new paragraph in the text file?

All help appreciated. Thanks!

Follow up:

import processing.serial.*;
Serial mySerial;
//import java.text.*;
//import java.util.*;
PrintWriter output;  
String[] list;
void setup() {
size(720,700);
background(255);
mySerial = new Serial( this, "COM3", 1200 );
output = createWriter("test5.txt");
}
void draw() {
if (mySerial.available() > 0 ) {
     String value = mySerial.readString();
     if ( value != null ) {
        fill(50);
        text(value,10,10,700,700);
        output.println(value);
      saveStrings("test5.txt", value);
     }
  } 
}

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

I tried this code for saving data into a text file and got an error that said "The method saveString(String, String[]) in the type PApplet is not applicable for the argument (String, String) "

2
  • Where else have you looked? I see documentation and examples in both the application and Google. Commented Mar 18, 2014 at 0:15
  • Yes I did see. Right now I tried using saveStrings but got some error which said "The method of saveStrings(Sting, String[]) in PApplet is not applicable...etc" Commented Mar 18, 2014 at 0:53

1 Answer 1

2

Before reading on, make sure that you are successfully reading the data from your Arduino into String value. You can confirm that by simply doing the following:

println(value);

The above line will print out the String to the command line of the PDE. If you see nothing, that means your Arduino is not feeding anything through. In that case you need to fix other things.

Also, to make sure the above works without any other things getting in the way, comment out the lines giving you problems or doing any file I/O.

If you have confirmed that you are receiving inputs from your Arduino then read on.

You are using the method saveStrings() incorrectly. According to the documentation here: http://www.processing.org/reference/saveStrings_.html you should convert your String input into an array and then feed it into the method.

Right now you have the following line:

String value = mySerial.readString();

Add the following after that:

String[] valueArray = split(value, ' ');

The above is directly from the documentation and splits the input String at every space and saves it in an array. After this just pass the array into the saveStrings() method and you should be good to go, like so:

saveStrings("myTextFile.txt", valueArray);

From the documentation, this "[w]rites an array of Strings to a file, one line per String".

The error message that you get:

"The method saveString(String, String[]) in the type PApplet is not applicable for the argument (String, String) "

very clearly tells you that you are feeding saveString() method two Strings (saveString(String, String)) whereas what it expects is a String and a String array (saveString(String, String[])).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.