Hi guys, I was certain that someone has had this problem but couldn't find anything on the forum about it, so I apologize if this is a repeat.
I'm trying to record (and graph) separate serial streams coming from separate arduinos onto the same computer using the Processing/Java PrintWriter functions. I was planning to use different Processing scripts running at the same time, similar to the early version below, to log data into separate text files corresponding to the serial stream coming from a specific Arduino, specified by the COM port for that arduino. Each arduino is assigned a separate COM port when it is initially connected to the computer and the driver is acknowledged/loaded (WinXP).
This script works fine for logging serial data from each arduino when it is run alone for each COM port, but when multiple versions are run specifying different COM ports, the latest one that was run starts grabbing all of the serial information from both arduinos (despite the serial objects being theoretically connected to separate virtual serial ports).
There might be something else that I need to do in order to distinguish the streams but it seems that just setting each Processing script to read serial data from a different COM port is not enough...
("Whatever you do, don't cross the streams!!! - Ghostbusters)
Any help, hints, ideas, or alternative approaches would be greatly appreciated!
Thanks,
c
////////////////START///////////////////////////
import processing.serial.*; // import the Processing serial library
PrintWriter output; // initialize the PrintWriter Java object
Serial myPort; // the serial port
long time=0; // initialize variable for the current time since program started
String trigTime; // initialize variable for serial data from Arduino
void setup()
{
// Create a new file in the sketch directory
output = createWriter("testText.txt");
println(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.bufferUntil('\n');
}
void draw() { //draw() can be empty, everything's in SerialEvent
}
void serialEvent(Serial myPort) { // use serialEvent() because of intermittent Arduino data
time = millis(); // see how long it's been since program started
if (time < 1200000) // conditional for length of trial
{
if (myPort.available() > 0) { // when serial data is sent from Arduino
// read serial data of trigger times (since start of this script) from Arduino
trigTime = myPort.readStringUntil('\n'); // use readStringUntil() to make sure we get entire serial package
// Write the Arduino data to a file
output.print(trigTime); // write the output to .txt file as string (will just be number in text)
print(trigTime); // print trigger times to this display
}
}
else {
output.flush(); // Write the remaining data
output.close(); // Finish the file
println("END TRIAL");
exit(); // Stop the program
}
}
////////END//////////////