udp print to variable

edited February 2014 in Programming Questions

Dear all,

I am busy with a project and I am sending data from arduino to processing with UDP. I want to store the received data in a variable on the processing side but the example just prints the data. How can I save the printed value in a variable instead of only printing it?

 void receive( byte[] data ) {          // <-- default handler
// void receive( byte[] data, String ip, int port ) {   // <-- extended handler

 for(int i=0; i < data.length; i++) 
 print(char(data[i]));  
 println();   
 }

This is the code used ^

kind regards

Rens

Answers

  • edited February 2014

    If you printed the variable... then can't you just use the same parameters to save the values to a variable?

    Kinda like this?:

    char[] store = new char[0];
    
    void receive( byte[] data ) {
      for (int i=0; i < data.length; i++)
        print(char(data[i]));
        store.append(char.data[i]);
      println();
    }
    

    I don't know whether you need a global variable or not, and whether your datatype is definitely char, but all of these are assumptions.

  • ah that seems easy enough i´ll give it a try! thanks for your help

  • huh if I use the variable i in the for loop twice e.g.

    void receive( byte[] data ) {
      for (int i=0; i < data.length; i++)
        print(char(data[i]));
        print(char(data[i])); //twice does not work??
        store.append(char.data[i]);
      println();
    }
    

    I get this error: Cannot find anything named "i"

    full code:

    import hypermedia.net.*;
    
     UDP udp;  // define the UDP object
    
    
     void setup() {
    
     udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
     //udp.log( true );         // <-- printout the connection activity
     udp.listen( true );           // and wait for incoming message  
      String ip       = "192.168.1.132"; // the remote IP address
     int port        = 8888;        // the destination port
    
     udp.send("Hello World", ip, port );   // the message to send
    
     String ip2      = "192.168.1.111"; // the remote IP address
     int port2      = 8888;        // the destination port
    
     udp.send("Hello World", ip2, port2 );   // the message to send
     }
    
     void draw()
     {
    
     }
    
    
    
     void receive( byte[] data ) {          // <-- default handler
    // void receive( byte[] data, String ip, int port ) {   // <-- extended handler
     for(int i=0; i < data.length; i++) 
     print(char(data[i])); 
      print(char(data[i])); 
     println();   
     }
    
  • edited February 2014

    You've forgotten to define the for loop block w/ curly braces! I-)
    It's necessary when we got more than 1 statement block! :-\"

    Anyways, I've tweaked your code. It's untested since I don't have that 3rd-party library! :-/
    But you can at least try it out for yourself:

    // forum.processing.org/two/discussion/3081/udp-print-to-variable
    
    import hypermedia.net.*;
    
    UDP udp;  // define the UDP object
    ArrayList<byte[]> storage = new ArrayList();
    
    void setup() {
      udp = new UDP(this, 6000);
      //udp.log(true);
      udp.listen(true);
    
      String ip = "192.168.1.132";
      int port  = 8888;
      udp.send("Hello World", ip, port);
    
      ip   = "192.168.1.111";
      port = 8888;
      udp.send("Hello World", ip, port);
    
      println();
      for (byte[] b: storage)  println(b);
    
      exit();
    }
    
    
    void receive(byte[] data) {
      //void receive(byte[] data, String ip, int port) {
      for (byte b: data)  print((char) b + " - ");
      storage.add(data);
    
      // Alternative in case storage.add(data); doesn't work correctly:
      //final byte[] dat = new byte[data.length];
      //arrayCopy(data, dat);
      //storage.add(dat);
    }
    
Sign In or Register to comment.