Go Down

Topic: Two Arduinos talking via Bluetooth to one Processing sketch (Read 2072 times) previous topic - next topic

Plusea

i've been trying to read two serial streams, via bluetooth, into one processing sketch, and have been successful. but only once!
most of the time the sketch either won't run or will run with only one bluetooth port connected (the other generates a port in use error).

my setup: i have two arduino mini pros with one sparkfun bluetooth mate mounted on each. for testing i have each arduino continuously print "LEFT" or "RIGHT" over the serial port. the processing sketch determines which port it is receiving data from, reads the data and prints it out for debugging.

when i run the same code, but connect the arduinos via FTDI breakouts directly to my computer, i get no such problems.

because i've been able to connect to both bluetooth modules and receive data, i think that the problem is with the initialization of the bluetooth ports and/or with timing. i know very little about how the bluetooth connection is established and what goes on while it is active. if anybody has experience and can detect an obvious error in my setup, thought process or code, i would very much appreciate any help or advice i can get.

the code i have is pieced together from the "many serial ports" processing example and tom igoe's arduino graph code.

here is the error message i get:
Code: [Select]
    gnu.io.PortInUseException: Unknown Application
        at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
        at processing.serial.Serial.<init>(Serial.java:136)
        at processing.serial.Serial.<init>(Serial.java:102)
        at TwoBluetooth.setup(TwoBluetooth.java:64)
        at processing.core.PApplet.handleDraw(PApplet.java:1402)
        at processing.core.PApplet.run(PApplet.java:1327)
        at java.lang.Thread.run(Thread.java:680)
    Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.<init>()
        at processing.serial.Serial.errorMessage(Serial.java:588)
        at processing.serial.Serial.<init>(Serial.java:148)
        at processing.serial.Serial.<init>(Serial.java:102)
        at TwoBluetooth.setup(TwoBluetooth.java:64)
        at processing.core.PApplet.handleDraw(PApplet.java:1402)
        at processing.core.PApplet.run(PApplet.java:1327)
        at java.lang.Thread.run(Thread.java:680)


here is my Arduino code, one prints LEFT, the other RIGHT:
Code: [Select]
void setup()
{
  Serial.begin(115200);
}

void loop() {
  //Serial.println("RIGHT");
  Serial.println("LEFT");
  delay(10);
}


processing sketch:
Code: [Select]
import processing.serial.*;

Serial rightPort, leftPort;

void setup()  {
  size(100, 100);  //size of application window
  background(255);    // set inital background

  //////////// SERIAL PORT ///////////
  println(Serial.list());  // prints a list of the serial ports

  //////// LEFT ////////
  leftPort = new Serial(this, "/dev/tty.RN42-059C-SPP", 115200);  ///// BLUETOOTH /////
  //leftPort = new Serial(this, "/dev/tty.usbserial-A6007x5s", 9600);  ///// FTDI /////
  leftPort.clear();
  leftPort.bufferUntil('\n');  // don't generate a serialEvent() until you get a newline (\n) byte

  //////// RIGHT ////////
  rightPort = new Serial(this, "/dev/tty.RN42-0592-SPP", 115200);  ///// BLUETOOTH /////
  //rightPort = new Serial(this, "/dev/tty.usbserial-A700ewYh", 9600);  ///// FTDI /////
  rightPort.clear();
  rightPort.bufferUntil('\n');  // don't generate a serialEvent() until you get a newline (\n) byte
}

void draw() {
}

void serialEvent(Serial thisPort) {
  if (thisPort == rightPort) {
    String rightString = rightPort.readStringUntil('\n');  // get the ASCII string
    if (rightString != null) {  // if it's not empty
      println("right? " + rightString);
    }
  }
 
  if (thisPort == leftPort) {
    String leftString = leftPort.readStringUntil('\n');  // get the ASCII string
    if (leftString != null) {  // if it's not empty
      println("left? " + leftString);
    }
  }
}

PaulS

Code: [Select]
  println(Serial.list());  // prints a list of the serial ports
So, what's in this list?

Specifically, are these
  leftPort = new Serial(this, "/dev/tty.RN42-059C-SPP", 115200);  ///// BLUETOOTH /////
  rightPort = new Serial(this, "/dev/tty.RN42-0592-SPP", 115200);  ///// BLUETOOTH /////
in the list?
The art of getting good answers lies in asking good questions.

Plusea

yes, they appear in the list every time. hmmm

Code: [Select]
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "/dev/tty.usbserial-A6007x5s"
[1] "/dev/cu.usbserial-A6007x5s"
[2] "/dev/tty.usbserial-A700ewYh"
[3] "/dev/cu.usbserial-A700ewYh"
[4] "/dev/tty.Bluetooth-Modem"
[5] "/dev/cu.Bluetooth-Modem"
[6] "/dev/tty.Bluetooth-PDA-Sync"
[7] "/dev/cu.Bluetooth-PDA-Sync"
[8] "/dev/tty.RN42-0592-SPP"
[9] "/dev/cu.RN42-0592-SPP"
[10] "/dev/tty.RN42-059C-SPP"
[11] "/dev/cu.RN42-059C-SPP"

Go Up