Serial Port Error - Arduino and Processing can't run together

I've read that this issue is known, and comes up in different forms on this forum.

I'm sending data from my Arduino Uno over the serial monitor so it can be read and displayed by my Processing program. They are both using the same serial port (Com4 in my case), the arduino is sending, the processing is receiving. This worked for me once before, when I was doing this tutorial:

http://www.youtube.com/watch?v=g0pSfyXOXj8

It gives me an error now (albeit a different program is running).

I can't start the processing program while Arduino is still running. If I upload the arduino sketch, open the serial monitor to confirm there is data, and then shutdown the arduino program (and serial monitor along with it), then start my processing sketch, I don't have a problem. This is because the arduino is sending data (3axis accel) over the serial with the arduino program needing to run, the board is doing the job.

When my Processing gui starts I see the solid Tx led on my Arduino uno board start blinking, transmitting and I can see the data on my screen.

I found a forum post that I thought would be promising which said it might be a java extension that's causing the headache. http//forum.arduino.cc/index.php/topic,26118.0.html

I looked for RXTX library or rxtx or comm.jar like the post said but didn't find anything. I am using windows 7, processing 2.0.1 (32bit, need serial) and arduino 1.0.5

My program can't execute the line that reads: myPort = new Serial(this, "COM4", 9600); Port exception, already in use, go suck a lemon, go fly a kite, something to that effect.

Any thoughts?

Thanks.

People are going to need/want to see your code. (all of it)

Sounds like there is a confliction on the port usage between the processing program and the Arduino though.

When the IDE has the com port resource, what do you expect the PC to tell you when you start anther program that asks for the same resource?

Load the program to Arduino, either shut the IDE off or change the serial port it uses in Tools, start up you other program (Processing) and get on with it.

AliH:
They are both using the same serial port (Com4 in my case), the arduino is sending, the processing is receiving. This worked for me once before, when I was doing this tutorial:

It seems unlikely that it would ever have worked when you tried to access the same serial port in two applications simultaneously, and more likely that you have mis-remembered what you did.

Only one application can open the serial port at a time. If you want to be able to start the Processing application while the Arduino IDE has the port open then you could implement some retry logic in the Processing application so that it waits and retries opening the serial port until it succeeds. That wouldn’t enable you to access the serial port in both applications at the same time but would be a bit more tolerant if you started the Processing application too early by mistake.

Obviously, while the Processing application has the serial port open you will also have the reverse situation where the Arduino IDE is unable to open the port to upload a new sketch or display the serial monitor, because that serial port is already being used by the Processing application. The restriction that the serial port can only be accessed by one process at a time is pretty fundamental, and you’ll just have to get used to working round it.

Actually COM has two ends. That makes it easier to get Arduino and ( Processing or some other app like the IDE ) to communicate. But you could open Processing and the IDE & Serial Monitor then when Arduino plugs in it should get assigned another COM port.

Serial for Processing has functions to check port availability.

When you can check, don't assume. If you find yourself relying on assumption then it's time to learn how to check.

GoForSmoke: Actually COM has two ends. That makes it easier to get Arduino and ( Processing or some other app like the IDE ) to communicate. But you could open Processing and the IDE & Serial Monitor then when Arduino plugs in it should get assigned another COM port.

Would that work? I would only expect the virtual COM port to exist while the device was plugged in; I don't see how you could open the serial port associated with an Arduino until it was plugged in.

If Processing opens a COM port then serial monitor could connect to it. If the COM port is one that an Arduino usually uses, the Arduino should get a different COM port. Processing can watch for the Arduino to plug in and connect to that as well.

So, I was trying to use Processing (For OpenCV) before finding out about RoboRealm. So much better :) Anyway's, I was confused about how serial ports worked, at first. But another forum told me to think of it as a phone line, busy / not busy metaphor. Anyway's, yes, only one process can be using the same port. So, you wont be able to use the Serial Monitor in Arduino when Processing is connected to your Arduino (COM3 most likely). What I have to do with RoboRealm is program the Arduino first, and then open RoboRealm and connect to the COM3 - Arduino. I can't monitor using the Arduino Serial Monitor, so I have to have everything printed to Serial so the program using COM 3 (Processing, Roborealm) can read the results. I just echo everything back basically for testing. So yes, you can't use both. And don't need to.

I've concluded that I can't run Arduino and processing at the same time, thanks for the lighting fast responses that helped me confirm this. I'm uploading the sketch and then shutting down arduino and starting my processing GUI. I run into a problem here though.

When I start processing and read from the Arduino (serial) sometimes the program crashes because of the serial content that it is reading. I read with the code myPort = new Serial(this, "COM4", 9600); //looks for Arduino's baudrate myPort.bufferUntil('\n');

I know why this is happening, it's because when it looks at the serial monitor it gets some junk in the first line and then starts printing my X,Y,Z gravity values from my accelerometer http://imageshack.us/photo/my-images/24/67j9.png

The serial exception looks like this: http://imageshack.us/f/802/4bmp.png/

The error says: Caused by: java.lang.NumberFormatException: For input string: "0.34VË-0.27X0.35"

I think the capital E with the umlaut (or trema if you prefer) is causing the problem (the serial monitor and processing error are taken at different times, that's why they don't match.

Is there a way to delay the read in my loop or just ignore the first line. I'm just starting my Arduino/Processing experience. I hope the images are visible, I think chrome is blocking them, or I'm just using the image tags incorrectly.

It sort of looks almost like your Arduino is sending scientific notation.....

You might write a Processing sketch that does nothing but log and display what the Arduino is sending, including some representation of unprintable characters (lately I go for " xHH" where HH are two hex digits giving the value.

Do this to isolate the problem. Keep reducing until there can only be one answer, then see if it is that or something you didn't think of.. yet.

I've concluded that I can't run Arduino and processing at the same time,

You are wrong, I have been using them at the same time for years.

When ever processing opens the serial port it resets the arduino. The arduino then looks to see if it is being fed code for up loading, if not it runs the sketch. So if you try and send it stuff too early it might think it is being given code to upload, or if you simply rrequest stuff from the buffer it will containing the bytes of the arduino asking if there is any code to upload. The solution is to delay a short time after the port is open and then clear out the input buffer before attempting to communicate. Adding a delay is a bit tricky because processing does not work like the arduino and the delay function doesn't work the same. I sometimes add this line at the start of the draw function to get a delay:-

  void draw() {// enable arduino to start up
    if(startDelay){ delay(1500); startDelay = false;
     // other arduino initialization stuff
 }

You can't put the delay in setup but this will work. startDelay is initialized as true so that delay is only called on the first ever time draw is called.

If you want to see what the arduino is sending back then simply print it at the processing end and you see it at the bottom of the sketch. There is never any need to use the arduino's serial monitor when using processing.

It might depend on what you call Arduino. I call the Arduino IDE the IDE but someone might call it Arduino.

Grumpy_Mike:

I've concluded that I can't run Arduino and processing at the same time,

You are wrong, I have been using them at the same time for years.

I think in this context it would be more accurate to say that you can't use Processing and the Arduino IDE's Serial Monitor to connect to a given Arduino's serial port at the same time. And I believe it's true that you can't, unless you're willing to consider much more complicated solutions to replicate the data passing to/from the serial port. However, given a Processing application that you have control over, I don't see why you would need to use the serial monitor at the same time. Surely, whatever you wanted to do with the serial monitor could be done by the Processing application instead?

Grumpy_Mike:

I've concluded that I can't run Arduino and processing at the same time,

You are wrong, I have been using them at the same time for years.

When ever processing opens the serial port it resets the arduino. The arduino then looks to see if it is being fed code for up loading, if not it runs the sketch. So if you try and send it stuff too early it might think it is being given code to upload, or if you simply rrequest stuff from the buffer it will containing the bytes of the arduino asking if there is any code to upload. The solution is to delay a short time after the port is open and then clear out the input buffer before attempting to communicate. Adding a delay is a bit tricky because processing does not work like the arduino and the delay function doesn't work the same. I sometimes add this line at the start of the draw function to get a delay:-

  void draw() {// enable arduino to start up
    if(startDelay){ delay(1500); startDelay = false;
     // other arduino initialization stuff
 }

You can't put the delay in setup but this will work. startDelay is initialized as true so that delay is only called on the first ever time draw is called.

If you want to see what the arduino is sending back then simply print it at the processing end and you see it at the bottom of the sketch. There is never any need to use the arduino's serial monitor when using processing.

I've tried the delay this way, right at the start of my draw(). Seems to crash less often.

void draw()
{
  if (startDelay)
  { 
    delay(500);
    myPort.clear();    //clear the port buffer

      startDelay = false;
  }
//Program begins below
  background(255);
  strokeWeight(1);
  fill(23, 19, 19);
...

I've tried different delay values in processing and in my arduino code, to vary how fast I output to serial. Hit and miss. I'll look into other ways of clearing the buffer. Thanks to all of you.

this is the only error. i can not find the cause for which it is occuring.

rajiv13531: this is the only error. i can not find the cause for which it is occuring.

What are you talking about? Quoting something might have helped.

AliH:

Grumpy_Mike:

I've concluded that I can't run Arduino and processing at the same time,

You are wrong, I have been using them at the same time for years.

When ever processing opens the serial port it resets the arduino. The arduino then looks to see if it is being fed code for up loading, if not it runs the sketch. So if you try and send it stuff too early it might think it is being given code to upload, or if you simply rrequest stuff from the buffer it will containing the bytes of the arduino asking if there is any code to upload. The solution is to delay a short time after the port is open and then clear out the input buffer before attempting to communicate. Adding a delay is a bit tricky because processing does not work like the arduino and the delay function doesn't work the same. I sometimes add this line at the start of the draw function to get a delay:-

  void draw() {// enable arduino to start up
    if(startDelay){ delay(1500); startDelay = false;
     // other arduino initialization stuff
 }

You can't put the delay in setup but this will work. startDelay is initialized as true so that delay is only called on the first ever time draw is called.

If you want to see what the arduino is sending back then simply print it at the processing end and you see it at the bottom of the sketch. There is never any need to use the arduino's serial monitor when using processing.

I've tried the delay this way, right at the start of my draw(). Seems to crash less often.

void draw()
{
  if (startDelay)
  { 
    delay(500);
    myPort.clear();    //clear the port buffer

      startDelay = false;   } //Program begins below   background(255);   strokeWeight(1);   fill(23, 19, 19); ...




I've tried different delay values in processing and in my arduino code, to vary how fast I output to serial. Hit and miss. I'll look into other ways of clearing the buffer. Thanks to all of you.

How about you let processing open the serial and reset Arduino which in setup() sends Processing text that tells it "I'm ready"? Then Processing sends back something to say "go". Just a little protocol.

EternalForge: So, I was trying to use Processing (For OpenCV) before finding out about RoboRealm. So much better :) Anyway's, I was confused about how serial ports worked, at first. But another forum told me to think of it as a phone line, busy / not busy metaphor. Anyway's, yes, only one process can be using the same port. So, you wont be able to use the Serial Monitor in Arduino when Processing is connected to your Arduino (COM3 most likely). What I have to do with RoboRealm is program the Arduino first, and then open RoboRealm and connect to the COM3 - Arduino. I can't monitor using the Arduino Serial Monitor, so I have to have everything printed to Serial so the program using COM 3 (Processing, Roborealm) can read the results. I just echo everything back basically for testing. So yes, you can't use both. And don't need to.

I found a solution in other forum. If you move the "size()" to the beginning of "void setup()", the connection will work.

At least, worked with me