UPD/OSC wait for data
in
Contributed Library Questions
•
2 years ago
Hello!
I'd like to ask for question. I'm sending data via UPD from processing to grasshoper3d and back. I need to processing wait for incoming data. I'm using while loop with with delay method for waiting, but there is problem with program stability. Stability is influenced by delay time and frameRate...
Is better way how to wait for data?
Thanks for yours reply.
I'd like to ask for question. I'm sending data via UPD from processing to grasshoper3d and back. I need to processing wait for incoming data. I'm using while loop with with delay method for waiting, but there is problem with program stability. Stability is influenced by delay time and frameRate...
Is better way how to wait for data?
Thanks for yours reply.
- import hypermedia.net.*;
UDP udp; // define the UDP object
ArrayList importMessage = new ArrayList(1); // arraylist for storing incoming message
void setup() {
frameRate(30);
// create a new datagram connection on port 6001
udp = new UDP( this, 6001 );
// first value in arraylist
importMessage.add("0");
}
void draw(){
// wait for incoming data
// if is delay small it can happens program is not stable (stop working after a few iterations)
// there is relation between frameRate and delay
boolean listenBitch = false;
//println(listenBitch);
while(listenBitch == false){
if( importMessage.size() == 0){
//wait loop
udp.listen( true );
}
else{
listenBitch = true;
//println(listenBitch);
}
delay(100); // Stops the program for 100 milliseconds
}
// send data to grasshopper
// 1 + last incoming message
dataSend( str( 1 + float((String)importMessage.get(0)) ));
println(importMessage.get(0));
//stop the program
if( float((String)importMessage.get(0)) > 2000){
noLoop();
}
//clear arraylist
importMessage.clear();
}
//void keyPressed(){
// dataSend( str( 1 + float((String)importMessage.get(0)) ));
// println(importMessage.get(0));
//
// importMessage.clear();
//}
void dataSend(String message) {
//String message = String.valueOf(mouseX) + " " + String.valueOf(mouseY);//str( "hello" ); // the message to send
//message = "10,10,10 "+"30,30,30 ";
String ip = "127.0.0.1"; // the remote IP address
int port = 6002; // the destination port
// send the message
udp.send( message, ip, port );
//println("Sending message: \""+message+"\"");
}
void receive( byte[] data, String ip, int port ) { // <-- extended handler
// get the "real" message =
// forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
data = subset(data, 0, data.length-2);
String message = new String( data );
// print the result
// println( "receive: \""+message+"\" from "+ip+" on port "+port );
importMessage.add(message);
}
1