We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › using processing and usb hub
Page Index Toggle Pages: 1
using processing and usb hub (Read 3304 times)
using processing and usb hub
Sep 20th, 2006, 2:59pm
 
Hi,

we need to read multiple ports (USB hub) from inside the Processing environment. We've got multiple sensors plugged into the hub and need to be able to read data from them individually. Any advice on how to go about that?

Many thanks!

Regards,
    Manuela and Rudi
Re: using processing and usb hub
Reply #1 - Nov 18th, 2006, 8:46pm
 
Hi,

I read your message and I have the same question.  I've been trying to find out whether there was a way of altering the serialEvent function (code) so that you can read two serialEvents instead of just one.  I'm currently working on a school project that uses two ultrasonic sensors (Arduino) and I've managed to get processing to differentiate between the two.  However, I can't run both at the same time in processing.  Any advice you can offer is greatly appreciated.

code:
import processing.serial.*;

// The serial ports
Serial port, port2;
int xpos, ypos;

void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn

// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;

// Print a list of the serial ports, for debugging purposes:
println(Serial.list());

//Serial Ports
port = new Serial(this, "COM3", 9600);//Controls Y direction
//port2 = new Serial(this, "COM5", 9600);//Controls X direction

}


void draw() {  
 
background(0);


// Draw the shape
fill(0,255,0);
ellipse(125, ypos, 20, 20);

fill(255,0,0);
ellipse(xpos, 125, 20, 20);


println("ypos " + ypos);
println("xpos " + xpos);

}


//Y Axis
void serialEvent(Serial port) {
ypos = port.read();
}
Re: using processing and usb hub
Reply #2 - Nov 19th, 2006, 12:38am
 
Hello,
I dont understand why you need to card, you have 14 digital entries on the arduino card, sending it by bytes, you obtain series of numbers, easy to differenciate. If you need more you can use the wiring card (32 entries). You can find some exemple for 3 sensors in the arduino site.
Re: using processing and usb hub
Reply #3 - Dec 19th, 2006, 12:20pm
 
Hi,

I am currently dealing with a similar problem; i am building a n interaction prototype with a wired and wireless part, the wired part is controlled via an arduino and data sent to processing over usb. the wireless part is controlled by and arduino mini, and communication is done via bluetooth.

As a result it actually do need to read two serial ports; the bluetooth serial port and the usb serial port.

at the moment I have solved the problem by avoiding the serialEvent() method and am constantly calling two methods in void draw(). this is however not an ideal solution..
so...any other options on getting serialEvent() to differentiate 2 serial ports and reading data from them?

thanx

current code:
/**BLOW - COMPUTER APPLICATION
*
* Bram Steevens - 2006
*
*
*
*
*/

import processing.serial.*;
//  base side data variables
Serial portBlue;
int blueRxA;  // incomming bluetooth serial
int blueRxB;
int blueRxC;
int blueRxD;
int sumBlue;
int[] blueRxArray = new int[3];
int blueRxCount;
char blueTx = 0;  // outgoing bluetooth serial

// disk side data variables
Serial portUsb;
int usbRxA;  // incomming usb serial
int usbRxB;
int usbRxC;
int usbRxD;
int sumUsb;  
int [] usbRxArray = new int[3];
int usbRxCount;
char usbTx;  // outgoing usb serial


//  other variables
PFont century14;
int i = 0;
int serialCount = 0;

void setup() {

 size(400, 200);
 frameRate(30);
 // List all the available serial ports:
 println(Serial.list());
 
 // initiate 2 serial connections, one for the base of BLOW
 // on usb and one over bluetooth for the disk of BLOW
 portBlue = new Serial(this, Serial.list()[2], 9600);
 portUsb = new Serial(this, Serial.list()[0], 9600);

 // text settings
 century14 = loadFont("CenturyGothic-14.vlw");
 textFont(century14, 14);  
}

void draw() {
 
 background(255, 205, 0);
 text("blueSent: " + blueTx, 100, 100);
 text("timer value: " + sumBlue, 200, 100);
 usbEvent();
 blueEvent();
 
 //delay(100);
}

void blueEvent() {
 
 while (portBlue.available() > 0){
   blueRxArray[blueRxCount] = portBlue.read();
   blueRxCount++;
 
   if (blueRxCount > 2) {
   blueRxA = blueRxArray[0];
   blueRxB = blueRxArray[1];
   blueRxC = blueRxArray[2];
//  blueRxD = blueRxArray[3];
//  sumBlue = blueRxArray[4];
   sumBlue = blueRxA + blueRxB + blueRxC;
   blueRxCount = 0;
   }
//  println(blueRx);
 }
}

void usbEvent() {
 while (portUsb.available() > 0){
   usbRxArray[usbRxCount] = portUsb.read();
   usbRxCount++;
 
   if (usbRxCount > 2) {
   usbRxA = usbRxArray[0];
   usbRxB = usbRxArray[1];
   usbRxC = usbRxArray[2];
//  usbRxD = usbRxArray[3];
//  sumUsb = usbRxArray[4];
   sumUsb = usbRxA + usbRxB + usbRxC;
   usbRxCount = 0;
   }
//  println(blueRx);
 }
}
void serialEvent(Serial ) {
 serialInArray[serialCount] = portUsb.read();
 serialCount++;  
}*/

void keyPressed() {
 blueTx = key;
 // Send the keystroke out:
 portBlue.write(key);
}

Re: using processing and usb hub
Reply #4 - Dec 19th, 2006, 12:31pm
 
http://processing.org/reference/libraries/serial/serialEvent_.html
"The which parameter contains the name of the port where new data is available, but is only useful when there is more than one serial connection open and it's necessary to distinguish between the two."

Code:
Serial port1, port2;

public void serialEvent(Serial which) {
if (which == port1) {
// do one thing to read from port1
} else if (which == port2) {
// do something else to read from port2
}
}
Re: using processing and usb hub
Reply #5 - Dec 19th, 2006, 12:33pm
 
ah, really... my bad, understood the documention a bit differently...
i'll try it out right away

thanx Smiley
Page Index Toggle Pages: 1