Skip to content

Instantly share code, notes, and snippets.

@kongmunist
Created May 20, 2023 19:55
Show Gist options
  • Save kongmunist/9e2836ffe7164d37ab3906833f88707c to your computer and use it in GitHub Desktop.
Save kongmunist/9e2836ffe7164d37ab3906833f88707c to your computer and use it in GitHub Desktop.
Serial Logging class written for Java/Processing, integrates cleanly on UNIX/MacOS since it relies on unix style system commands
class SerialLogger {
public String portname;
public String filename;
public Process p;
SerialLogger(String portname, String filename) {
this.portname = portname;
this.filename = sketchPath() + "/data/" + filename + ".csv";
}
void beginRecording() {
ProcessBuilder builder = new ProcessBuilder("cat", portname);
builder.redirectOutput(new File(this.filename));
try {
p = builder.start(); // may throw IOException
}
catch (IOException e) {
e.printStackTrace();
}
}
void endRecording() {
p.destroyForcibly();
}
void beginStreaming(){
exec("sh", "-c", "echo a > " + portname);
}
void endStreaming(){
exec("sh", "-c", "echo a > " + portname);
}
void begin(){
beginStreaming();
beginRecording();
}
void end(){
endStreaming();
endRecording();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment