Link |Read data from the multiple Serial Ports

I am looking for how to get data from both analog0 and analog1. I found information as below at processing.org. But so far it’s not working well.

http://processing.org/learning/library/serialmultiple.html

import processing.serial.*;

Serial[] myPorts = new Serial[2];  // Create a list of objects from Serial class
int[] dataIn = new int[2];         // a list to hold data from the serial ports

void setup()  {
  size(400, 300);
  // print a list of the serial ports:
  println(Serial.list());

// get the ports' names:
  String portOne = Serial.list()[0];
  String portTwo = Serial.list()[1];
  // open the ports:
  myPorts[0] = new Serial(this, portOne, 9600);
  myPorts[1] = new Serial(this, portTwo, 9600);
}

void draw() {
  // clear the screen:
  background(0);
  // use the latest byte from port 0 for the first circle
  fill(dataIn[0]);
  ellipse(width/3, height/2, 40, 40);
    // use the latest byte from port 1 for the second circle
   fill(dataIn[1]);
  ellipse(2*width/3, height/2, 40, 40);
}

void serialEvent(Serial thisPort) {
  // variable to hold the number of the port:
  int portNumber = -1;

  // iterate over the list of ports opened, and match the
  // one that generated this event:
  for (int p = 0; p < myPorts.length; p++) {
    if (thisPort == myPorts[p]) {
      portNumber = p;
    }
  }
  // read a byte from the port:
  int inByte = thisPort.read();
  // put it in the list that holds the latest data from each port:
  dataIn[portNumber] = inByte;
  // tell us who sent what:
  println("Got " + inByte + " from serial port " + portNumber);
}
Advertisements

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s