We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Testing for active serial connection
Page Index Toggle Pages: 1
Testing for active serial connection (Read 1183 times)
Testing for active serial connection
Sep 28th, 2009, 4:51am
 
Hi,

I'm hoping some of the more seasoned processing / java people can help me on the correct course of action:

I've got a situation where I need to communicate with an external serial device (an arduino in this case) over an extended period of time. This is an autonomous setup, and isn't actively monitored. I've got a problem where it seems at times the arduino is dropping the serial connection briefly, and the processing sketch subsequently crashes.

I can't stop the arduino from doing this, so I'm attempting to make processing handle the problem gracefully.

Frustratingly I don't see an easy way of testing if a connection is currently active. Serial.list seems to list all ports regardless of if one of them is already in use (by processing). I can't test for a lack of the port being available, as by the time it tests, the arduino will very likely be back up and running.

What I've so far managed is to get working is:
Code:
  
serialPort = new Serial(this, "/dev/tty.usbserial-A9006LiY", 9600);
 try {
   serialPort.write('1');
 }
 catch(RuntimeException e){
   println("serial not present");
   serialPort = new Serial(this, "/dev/tty.usbserial-A9006LiY", 9600);
 }


--note the hard coded serial port would be inserted based on user selection at program start
--note I can't start and stop the serial connection each time I use it (at times i need to communicate once per second, and my testing tells me stopping and starting a connection can take up to 2 seconds)

Here I'm catching the exception at serial.write time. I'd prefer to be able to check for an active port rather than attempt to use it and fail. Is this possible?

serial.stop() seems to know if a connection is active, so is there some way to test it?

any other comments?

cheers!
Re: Testing for active serial connection
Reply #1 - Oct 2nd, 2009, 8:04am
 
I have a similar problem as well.. and I;m using Arduino....
Any help would be appreciated!
http://mycoverthypnosis.com
Re: Testing for active serial connection
Reply #2 - Oct 3rd, 2009, 10:19pm
 
Looking at the source to the Serial library, stop() calls dispose() which in turn checks if the i/o streams are null.

From the code you provided, the Processing sketch sends a command/request and expects a response.

Based on that, I would suggest using a timeout variable. Assuming it repeats by the draw() method, you could count the number of frames that have occurred since sending the request. If the count has gone more than a second, reset the state of the your sketch.

Is it possible to post a working example of what you are trying to do.
Page Index Toggle Pages: 1