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 & HelpOther Libraries › Processing, OSC and Flash
Page Index Toggle Pages: 1
Processing, OSC and Flash (Read 1401 times)
Processing, OSC and Flash
Jun 4th, 2008, 12:33am
 
Hi, i'm new at Processing and I'm trying to make a communication between Arduino and Flash, using Processing and OSC.

I've a very basic circuit on a BreadBoard with two buttons, when pressed each button sends a String on Serial Port: for example "H00:123123x" (I'm building an Interface for an already existing project, so I'm using the Strings composed in this way by another guy). Second button sends "H01:123123x".

I want to read this string in Processing, and build a OSC command to send via Flosc to Flash. The OSC command must contain a general command "/buttonPressed" (for example) and a value composed by just the first three characters of the String that Arduino sends to Processing: "H00" - for example.

OK, I've done it and it's working BUT sometimes the OSC command doesn't work as supposed and Flash trace me an incomplete received value.

I'm quite sure that Flash part is OK because I'm quite experienced, I don't know if this kind of mistake can happen due to the situation (you know: Arduino+processing+flosc+flash:) or I'm doing something wrong with Processing (I'm a newbee)

This is my code:



import processing.serial.*;
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

String portname="/dev/tty.usbserial-A9003ZDD";
Serial myPort;

void setup() {
myPort = new Serial(this, portname, 9600);
oscP5 = new OscP5(this,3000);
myRemoteLocation = new NetAddress("127.0.0.1",1234);
}


void draw() {

while (myPort.available() > 0) {

  String myString = myPort.readStringUntil(58); //58 is for ":"
  if (myString != null) {

    OscMessage myMessage = new OscMessage("/buttonPressed");
    myMessage.add(myString.substring(0,2));  //take just the character before ":"
    oscP5.send(myMessage, myRemoteLocation);
  }
  myPort.clear();
}
}

Can somebody help me please??
Thanks and sorry for my english :))

Richie


Re: Processing, OSC and Flash
Reply #1 - Jun 4th, 2008, 11:36am
 
hi richie,
your communication chain is quite long and you are using 2 network protocols to transfer data from arduino to flash. osc is sent via udp which means messages either arrive or or get lost and don't arrive at the reciever's side. tcp on the other hand always makes sure that a message gets transfered to its receiver, even the sequence is kept in sync. what can happen though is that too many osc messages are sent to flosc ending up flooding the flosc server. what i would do is to limit the amount of messages sent from arduino to 5-10 per second. if you are using processing only to transfer data, maybe this arduino-flash-interface might give you better results.
i suspect that the amount of messages sent to flosc congest the data flow which results in incomplete values.
hope this helps,
andi

Re: Processing, OSC and Flash
Reply #2 - Jun 4th, 2008, 1:52pm
 
Thanks andi,
but unfortunately the loss of data happens event at slow rate of sent messages.
I've tried to press my buttons every two seconds, and Flash still receives some incomplete message. If I try to press buttons very very quickly, the error appears more frequently in the Flash output - but I don't know if its for the congestion of messages or just for statistic reasons (you know, more messages, more errors).
About your suggested alternatives, I've already used Firmata and Tinker Proxy for other projects/exercises - but what I'm trying to do right now is having a Processing "serial-to-OSC" interface. What I'm doing right now in Flash is just my visual Output, but probably Flash will not be the final destination of the OSC packets (I still don't know which software we will use).
Are you sure that my Processing sketch is 100% ok?
I feel that the error might be in the processing part in which I read the serial message....
Thanks a lot

Richie
Re: Processing, OSC and Flash
Reply #3 - Jun 4th, 2008, 2:31pm
 
this line seems to be a bit suspicious
Code:

myMessage.add(myString.substring(0,2)); //take just the character before ":"


it takes the first and second character of myString. if you want to take the characters before ":" your code would only work correctly for exactly 2 characters before the : but not if there are 3 or more.
to check if there is a loss of data, add
Code:

println(myString.substring(0,2));

to the processing code to check if the values are equal on both sides, processing and flash.
if the number of characters in front of the ":" varies, you might use split() instead of substring

Code:

String list[] = split(myString, ':');
myMessage.add(list[0]); // list[0] = all chracters before ":"


hope this helps,
andi




Re: Processing, OSC and Flash
Reply #4 - Jun 4th, 2008, 6:07pm
 
Andi,
i've modified the sketch just to print what arrives on the Serial Port.
Basically the draw function is now:

void draw() {

while (myPort.available() > 0) {
   String myString = myPort.readString();
   println (myString);    
}
myPort.clear();
}

Now look at my Processing output:

H01:FF00D0x
H00:FF00D0x
H00:FF00D0x
H00:FF00D0x
H00:FF0
0D0x
H00:F
F00D0x
H00:FF00D0x
H
00:FF00D0x
H00:FF00D0x
H00:FF00D0x
H00:FF00D0x
H00:FF
00D0x


Look at every "broken" line, for example 5th, 6th and 7th. I think here is the problem.
The linefeed between them means that the initial condition
myPort.available() > 0
is considered false by Processing, so it considers a single string as different serial message, isn't it?
I'm quite confused :(

I don't know if it matters, but I'm on a new MacBook Pro (Intel) with 10.5.3, and this is my first week on Mac platform....
Re: Processing, OSC and Flash
Reply #5 - Jun 4th, 2008, 6:58pm
 
The problem is that you're checking for data whilst the board is still in the process of sending it when you try to read...
So the board sends H then 0 then 0 and then you try to read the data, and since H00 is all that's been sent so far, that's the entirety of the string that processing sees. There's no way for the serial library to know if a message has finished being sent.

So what you need to do is check how long the received message is and if it's too short, wait a small amount of time, then try to read some more data until the message is the right length.
Re: Processing, OSC and Flash
Reply #6 - Jun 4th, 2008, 7:11pm
 
Thanks JohnG and Andi
meanwhile I've done something similar to what JhonG just suggested me
here's the solution :))
I have just to wait until the last character in the String :)
Thx  guys :))



//////////////////////////////////

import processing.serial.*;
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

String portname="/dev/tty.usbserial-A9003ZDD";
Serial myPort;

String command = "/buttonPressed";
String inString;

void setup() {
 myPort = new Serial(this, portname, 9600);
 oscP5 = new OscP5(this,3000);
 myRemoteLocation = new NetAddress("127.0.0.1",1234);
}

void draw() {

 while (myPort.available() > 0) {    
   char inByte = myPort.readChar();
   if (inByte=='x'){
     composeOSCMessage (command, inString);
     //println(inString);
     inString="";
   } else {
     inString+=inByte;
   }    
 }
}

void composeOSCMessage (String commandToSend, String valueToSend) {
 OscMessage myMessage = new OscMessage(commandToSend);
 myMessage.add(valueToSend);
 oscP5.send(myMessage, myRemoteLocation);
}

Page Index Toggle Pages: 1