Reading serial port data sent from Processing

Problem I am having is that arduino just states that serial port is in use and it cant do anything, well its supposed to be in use if processing is using it to send data? This puzzles me, I tried to google it but there was nothing even remotely similar to issue I am facing.

If I stop my processing sketch, arduino sketch runs just fine, but that`s rather pointless when I need it to receive data from Processing.

What am I missing here?

smikis:
Problem I am having is that arduino just states that serial port is in use and it cant do anything, well its supposed to be in use if processing is using it to send data? This puzzles me, I tried to google it but there was nothing even remotely similar to issue I am facing.

If I stop my processing sketch, arduino sketch runs just fine, but that`s rather pointless when I need it to receive data from Processing.

What am I missing here?

I think you are missing the fact that there is only one serial port that can access that Arduino.

If Processing is using the port, the IDE will not be able to upload the program. If the Serial Monitor is using the port, Processing will not be able to contact the Arduino.

When you say you stop the processing sketch, how do you know the Arduino sketch "runs just fine"? Please describe exactly what you see when you get the problem. Is the Serial Monitor running? Is it when you try to upload a sketch to the Arduino? What reports the error? And so on.

But you should be able to have communication between processing and arduino, its not exactly new, there are plenty of examples online (not that they help me here).

Now I cannot compile arduino sketch if processing sketch is running, if processing sketch isn't running I can compile arduino sketch, but I cant do both at the same time. There must be something obvious I am missing that would allow processing communicate with arduino using serial port. I mean there are arduino examples for serial port communications ( they dont work for me as well )

smikis:
But you should be able to have communication between processing and arduino, its not exactly new, there are plenty of examples online (not that they help me here).

Yes, you can have communication between Processing and Arduino.

Now I cannot compile arduino sketch if processing sketch is running,

Yes you can. You cannot, however, upload a sketch if Processing has the COM port open. You can only have ONE program accessing the COM port at any given time.

if processing sketch isn't running I can compile arduino sketch, but I cant do both at the same time.

That's right. only ONE program can be using the COM port. The Arduino IDE needs the COM port to upload your Arduino sketch. If Processing is running, the IDE is prevented by the Operating system from opening the COM port, because it is already opened by Processing.

There must be something obvious I am missing that would allow processing communicate with arduino using serial port.

There is something obvious you are missing, and that is that ONLY ONE PROGRAM AT A TIME CAN OPEN ANY GIVEN COM PORT.

I mean there are arduino examples for serial port communications ( they dont work for me as well )

Yes, of course there are, but there are no examples that show you how to upload an Arduino sketch at the same time that another program has the COM port open.

You want to upload a sketch? Close the COM port. If you can do that with Processing still running, fine (I'm not familiar with Processing, so I don't know if that's possible). But if you can't close the COM port and have Processing still running, then you'll need to close Processing to upload your sketch. Only then can you open the COM port with another program.

Processing is just one such program. You can also use the IDE's Serial Monitor, and if it's opened by the same instance of the IDE that you are uploading a sketch from, the IDE will close the Serial Monitor in order to upload the Arduino sketch. If a Serial Monitor is opened by another instance of the IDE, the upload will fail, because the uploading IDE instance does not know how to close the Serial Monitor because it didn't run it.

You can also use a terminal program to communicate with the Arduino, but while the terminal program has the Arduino's COM port open, you will not be able to upload a sketch.

I don't know how I can make it any clearer. Upload sketches work only when the COM port is NOT open in another program. Period.

aha, so how should I go about making arduino program that communicates with processing , or anything else for that matter, in realtime
Even if I would close port with processing, and lets say 30s latter would want to transmit data again, I would have to open port again to do it, which would close port on arduino and give out errors, or that is supposed to happen and if I close the port with processing arduino will get data and continue working even despite freaking out?

What I really want is to send data to Arduino from processing every 6seconds or when processing is triggered

this is killing me ... Connecting Arduino to Processing - SparkFun Learn sparkfun tutorial
on how to connect processing to arduino, exactly the way I have, yet for me it doesnt work, according to you it SHOULD NEVER WORK , so what on earth are all those tutorials ???

Try this
Close Processing on the PC
Upload the code to the Arduino
Run Processing on the PC
Send some data from Processing to the Serial port
Read the incoming data on the Arduino and display it

Does that process fail at any point ?

smikis:
aha, so how should I go about making arduino program that communicates with processing , or anything else for that matter, in realtime

Just like the tutorial says, but close the COM port before you upload to the Arduino.

Even if I would close port with processing, and lets say 30s latter would want to transmit data again, I would have to open port again to do it, which would close port on arduino and give out errors, or that is supposed to happen and if I close the port with processing arduino will get data and continue working even despite freaking out?

You don't have to re-upload your Arduino sketch every time you want to run your Processing sketch! You upload the Arduino sketch, then run your Processing sketch. Leave it running as long as you like. Close it as often as you like. Every time you run your Processing sketch, it will open the COM port, and start communicating with the sketch that's still in the Arduino.

You only need to remember that any time you want to upload a sketch to the Arduino, you must not have the COM port opened with ANY other program (including Processing).

So... the tutorial.

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/introduction
Introduction

Next page: you write your Arduino sketch and upload it.

Next page: you write your Processing sketch and run it.

Next Page: you change your Processing sketch to send 1s
Note that at this time, you are not receiving them on the Arduino
because your Arduino sketch only send "Hello World"
CLOSE YOUR PROCESSING SKETCH (or just close the COM port if you can)

Next Page: you change your Arduino sketch to receive data from Processing
Note that before you upload your new Arduino sketch, the COM port that
Processing has open, must be closed. I don't use Processing, so I don't
Know if you can do it without closing the Processing IDE.

Here's a suggestion for you.

Go to Connecting Arduino to Processing - SparkFun Learn
Write your Arduino sketch (don't forget to save it)
Upload it to the Arduino

Test to see that the Arduino sketch works as follows
Open the Serial Monitor
Set it to 9600 baud
Send a 1
Send a 0
Close the Serial Monitor

Go to Connecting Arduino to Processing - SparkFun Learn
Write your Processing sketch
Run it.

That should work.

I see two things wrong with that tutorial. The first is that the steps seem to me to be out of order, and secondly, they don't tell you to close the COM port on the PC end when you change and upload your Arduino sketch..

I got it working finally, pretty much the way both of you suggested, upload arduino sketch , then run processing sketch, dont have to close arduino sketch for it to work. Pretty sure I attempted this order before and would get error in processing that port is in use.
But now I don`t get any errors and its working perfectly, maybe because I had serial monitor open in processing(because I wanted to print serial data to see if its correct ), but I am glad its finally working.

Thanks for your help.