Fast Serial Saving Data from Arduino
in
Core Library Questions
•
11 months ago
Hello,
I need to save incoming data from Arduino sent in this format at a serial speed of 57600:
The data are all int type.
Here is the problem I have:
First I run the processing program, then I press the push button. The TX light is on for a short time (~3.1 seconds).
Now my code says, if I press any key on keyboard, flush/close the file, and terminate the program, right?... This does not work, and I have to stop the program by clicking the Stop Button in the processing window.
Here is the weird thing: After I close the program, I only have 2.8 seconds of data in the output file. where did the other 300 ms go?
I really appreciate it if you can help me understand this, or you have some idea on how to fix this.
Thanks
I need to save incoming data from Arduino sent in this format at a serial speed of 57600:
The data are all int type.
- Serial.print(drive, DEC);
Serial.print(" ");
Serial.print(distanceAry[index], DEC);
Serial.print(" ");
Serial.print(pressureAry[index], DEC);
Serial.print(" ");
Serial.print(prSwitchAry[index], DEC);
Serial.print(" ");
Serial.println(tempAry[index], DEC);
- import processing.serial.*;
PrintWriter output;
Serial myPort;
String myData = null;
String[] Data = new String[5000];
int numData = 0;
int n = 0;
int m = 0; - void setup() {
println(Serial.list());
String portName = Serial.list()[0];
// change [0] for the Arduino serial port
myPort = new Serial(this, "COM5", 57600);
myPort.bufferUntil('\n'); // SerialEvent for new line
myPort.clear();
String Name = "Data "+str(month())+"-"+str(day())+"-"+str(year())
+"_"+str(hour())+"."+str(minute())+".txt";
// Create file to save the captured data
output = createWriter(Name);
}
void draw() {
while ( myPort.available () > 0 ) {
myData = myPort.readStringUntil('\n');
if ( myData != null ) {
Data[n] = myData;
n++;
}
}
while ( myPort.available() <= 0) {
if ( Data[m] != null) {
output.print(Data[m]);
m++;
}
}
} - void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
Here is the problem I have:
First I run the processing program, then I press the push button. The TX light is on for a short time (~3.1 seconds).
Now my code says, if I press any key on keyboard, flush/close the file, and terminate the program, right?... This does not work, and I have to stop the program by clicking the Stop Button in the processing window.
Here is the weird thing: After I close the program, I only have 2.8 seconds of data in the output file. where did the other 300 ms go?
I really appreciate it if you can help me understand this, or you have some idea on how to fix this.
Thanks
1