More serial open at a time

#1

Hello, does anyone knows if processing is able to communicate with two arduinos simultaneously? At the moment I don’t have two arduino to test the code but I’m trying to write it to progress with my work in the while.


import processing.serial.*;

Serial portDriver;
Serial portSampler;

public void setup()
{
  size(470, 350, JAVA2D);

  portDriver = new Serial(this,Serial.list()[0],1000000);
  portSampler = new Serial(this,Serial.list()[1],1000000);
  
  portSampler.bufferUntil('\n');
}

public void draw()
{
  background(230);
}

void serialEvent(Serial port)
{
  String inString = port.readStringUntil('\n');
  if (inString == null) { return; }
  
  // do stuff
}

So; there is an arduino which just receive commands and do stuff with motors, and communicates with processing through portDriver object.
The other arduino communicates through portSampler and receives, simultaneously, another command, and start sending strings to processing. You can see that since bufferUntil is called only for that one.

So one way is only out. The other is in-out.

Will such things ever work?

Thanks

#2

You can check out which Serial instance is the current caller of serialEvent(): :calling:

import processing.serial.Serial;

Serial portDriver, portSampler;

String data = "";

void serialEvent(final Serial s) {
  data = s.readString().trim();

  if (s == portSampler) {

  } else {

  }
}
1 Like
#3

Thank you very much! May I ask why you declare Serial s as final?

#4

B/c parameter s isn’t intended to be reassigned. But final is fully optional: :upside_down_face:
Processing.org/reference/final.html