The following code makes Arduino and Processing pass an 'A' back and forth like hot potato:
PROCESSING SENDS 'A' TO ARDUINO
import processing.serial.*; //import the Serial library
Serial mySerial; //the Serial port object
void setup()
{
mySerial = new Serial(this, Serial.list()[7], 9600); //Pick whichever works
mySerial.bufferUntil('\n');
}
void serialEvent( Serial mySerial)
{
println("writing A...");
delay(1000); //How I defined this is not relevant. Trust me.
mySerial.write('A'); //Send 'A' to Arduino
}
}
ARDUINO SENDS 'A' BACK TO PROCESSING
void setup()
{
Serial.begin(9600);
}
void loop()
{
delay(1000);
Serial.println('A'); //Send 'A' back to Processing
}
What happens?
Well... the game works for about ten exchanges (how do I know? well I get the "writing A..." message five times, and my use of bufferUntil() tells me that each of those messages were prompted by receiving a Serial.println('A') from the Arduino) BUT then Processing interrupts with the following error:
Error, disabling serialEvent() for /dev/tty.usbmodem1421
null
java.lang.RuntimeException: Error reading from serial port /dev/tty.usbmodem1421: Port not opened
at processing.serial.Serial.serialEvent(Unknown Source)
at jssc.SerialPort$LinuxEventThread.run(SerialPort.java:1299)
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting.
Wait - but println(x) prints x and a new line automatically, right?
I guess not. Nice point. But Processing in fact does respond, but only for the first five times. Processing does not respond if I stop sending it 'A's.
Okay, I included the \n, and I get the same error, but it extends the life of the program by a few "writing A..." messages. So it helps, but the main problem is still there.
Good point.
However you never empty the buffer at either end, in other words there is never a read of the serial port at any end.
How do you know that 7 is the arduino anyway. How about printing out what Processing recieved.
was the keyword in the error message: The Serial Port randomly closes from time to time. The solution is to continually force it open in draw:
Serial serial;
boolean serialInited;
void draw () {
if (serialInited) {
// serial is up and running
try {
byte b = serial.read();
// fun with serial here...
} catch (RuntimeException e) {
// serial port closed :(
serialInited = false;
}
} else {
// serial port is not available. bang on it until it is.
initSerial();
}
}
void initSerial () {
try {
serial = new Serial(this, Serial.list()[0], BAUD_RATE);
serialInited = true;
} catch (RuntimeException e) {
if (e.getMessage().contains("<init>")) {
System.out.println("port in use, trying again later...");
serialInited = false;
}
}
}
The Serial Port randomly closes from time to time.
Glad you got it going. But ... it should not do this, I have never seen processing do this, what you have to find out is why it is doing this and cure it at source not put a sticking plaster over it.
droberts:
The following code makes Arduino and Processing pass an 'A' back and forth like hot potato:
No it doesn't. It makes them each spew out 'A' characters and ignore anything sent by the other. On the Arduino side, this would cause the received characters to be buffered until the buffer was full and the excess input would be discarded - no particular harm. I don't know what would happen on the Processing side, but I've seen other people describe behaviour which implied that the serial port interface failed once the incoming buffer overflowed.
In any case I suggest that if you want to show bidirectional communication as your description implies, you need to read the received characters at each side. Quite likely this will also fix your stability problem, but if it doesn't at least you have eliminated this as a potential cause.