Arduino + Processing “Handshake” Issue

I have an issue getting Processing and the Arduino to talk to each other over the same Serial port. To condense my problem, I wrote a simple program where Processing sends "Processing: Hello!" every second, and the Arduino sends "Arduino: Hello there!" every second, both to the same Serial port. Here is the code:

PROCESSING CODE:

 import processing.serial.*; //import the Serial library
 Serial mySerial;  //the Serial port object

void delay(int time)
{
  int start = millis();
  while (millis() - start < time){}
}

void setup() 
{
  size(200, 200);
  println(Serial.list());
  mySerial = new Serial(this, Serial.list()[9], 9600);
  println(Serial.list()[9]);
  mySerial.bufferUntil('\n');
}

void serialEvent( Serial mySerial) 
{
  mySerial.write("Processing: Hello!");
  delay(1000);
}

ARUDINO CODE:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Arduino: Hello there!");
  delay(1000);
}

What I think I should be seeing in my Serial monitor on the Arduino sketch is:

"Arduino: Hello there!"
"Processing: Hello!"
"Arduino: Hello there!"
"Processing: Hello!"
"Arduino: Hello there!"
"Processing: Hello!"
...

What I actually see is:

"Arduino: Hello there!"
"Arduino: Hello there!"
"Arduino: Hello there!"
"Arduino: Hello there!"
...

Okay, so perhaps the Serial monitor only monitors Arduino output. Then, is there any other way to view the output from Processing on the Arduino side?

Answers


The serial monitor in Arduino is like a separate terminal program so it and your Processing sketch are competing for the same serial connection to the Arduino (and it looks like the serial monitor is winning.

The Arduino "Examples" includes an example named "SerialCallResponse." It includes Processing code to demo exactly what you are trying to do. The examples which ship with Arduino are very complete for these basic things; definitely worth checking out.

(Also, in your Processing sketch, you aren't reading the serial input when serialEvent is called - so you will never see any Arduino response even without the serial monitor conflict. See the example referenced above.)


Need Your Help

Swift - How to pre-select a table row at runtime using Swift?

swift ios8 xcode6

I want to pre-select a table row in a splitView master controller as the view loads, Just like Apple does with the General selection in the Settings App in an Ipad.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.