parse a tcp message

hi guys, i need some expert advice on this.

I have successfully created the tcp connection between processing and matlab. I am using oscp5 library for that. I am sending an array containing x and y coordinates from processing and my intent is to draw a sine wave in matlab with these received x & y values.

My question is how i can separate the coordinates after receiving in matlab? Also, i think i need to alter some setting in tcpip object because i am not receiving the same values in matlab, which i am sending.

You can see the code and their values below.

-------------Processing Code-------------
import oscP5.*;
import netP5.*;

OscMessage myMessage;
OscP5 oscP5tcpClient;

int xspacing = 16;   // How far apart should each horizontal location be spaced
int w;              // Width of entire wave
float theta = 0.0;  // Start angle at 0
float amplitude = 75.0;  // Height of wave
float period = 500.0;  // How many pixels before the wave repeats
float dx;  // Value for incrementing X, a function of period and xspacing
float[] yvalues;  // Using an array to store height values for the wave
int x;

void setup() {
  size(640, 360);
  oscP5tcpClient = new OscP5( this, "141.44.219.161", 1234, OscP5.TCP);
  w = width+16;
  dx = (TWO_PI / period) * xspacing;
  yvalues = new float[w/xspacing];
}

void draw() {
  background(0);


  calcWave();
  OscMessage myMessage = new OscMessage("/test");
  myMessage.add(new float[] { x*xspacing, height/2+yvalues[x]});  =====>>> These values are sent to Matlab. 
  oscP5tcpClient.send(myMessage);
  print(x*xspacing, height/2+yvalues[x]);
}

void calcWave() {
  // Increment theta (try different values for 'angular velocity' here
  theta += 0.02;

  // For every x value, calculate a y value with sine function
  float x = theta;
  for (int i = 0; i < yvalues.length; i++) {
    yvalues[i] = sin(x)*amplitude;
    x+=dx;
  }
}

void renderWave() {
  noStroke();
  fill(255);
  // A simple way to draw the wave with an ellipse at each location
  for (int x = 0; x < yvalues.length; x++) {
    ellipse(x*xspacing, height/2+yvalues[x], 16, 16);
  }

}

--------------------------MATLAB Code----------------------------
>> tcpipServer = tcpip('141.44.219.161',1234,'NetworkRole','Server');
>> fopen(tcpipServer)
>> data =fread(tcpipServer)

data =

     0
     0
     0
    20
    47
   116
   101
   115
   116
     0
     0         ===>>> these are values i am receving in matlab, which is completely different from what i am sending.
     0
    44
   102
   102
     0
     0
     0
     0
     0
    67
    53
   127
   249

>> 
------------------------------------------------------------------

Values which i am sending from processing is look something like below

230.19330 229.068510 227.924120 226.760530 225.578230 224.37770 223.159410 221.923840 220.671510 219.40290 218.118520 216.818880 215.504520 214.175930 212.83370 211.47830 210.110320 208.730290 207.338760 205.936280 204.523440 203.100770 201.668870 200.228290 198.779620 197.323430 195.860320 194.390850 192.915620 191.435230 189.950270 188.461320 186.968980 185.473860 183.976550 182.477630 180.977740 179.477450 177.9773

Answers

  • edited August 2016 Answer ✓

    Dunno much about OscP5 library and much less about MatLab.
    But you should be aware that each Java float value is made outta 4 bytes.
    Perhaps you need to look for some way to convert a 4-byte sequence back to 1 float in MatLab. :-@

    http://www.h-Schmidt.net/FloatConverter/IEEE754.html

  • Answer ✓

    I would suggest to send two messages, one containing the X values and another one containing the Y values. Then you can retrieve them easier in matlab. I agree with GoToLoop that you have to watch for the way the data is being packed in processing and it is being unpacked in matlab. I will not assume right away that float representation in your system are equivalent. They are OS dependent. On the other hand, there is one more thing you need to consider regarding packages sent using oscP5 library. When you create your message, you assign it a label "/test". When matlab's server gets the message, it will read this as well as the data in your package as assembled by oscP5. You have to teach matlab how to interpret that data. You will need to go to the oscP5 website and review java docs and the source code to understand the packaging format. OscP5 works great if you create messages and read messages using this same library. However if you use it to send data and have another software to read data, there is more work involved in understanding how the sending software works. Here is the link:

    http://www.sojamo.de/libraries/oscP5/

    On the other hand, I will suggest using processing net libraries

    https://processing.org/reference/libraries/net/index.html

    The advantage of this library is that you can stream out (aka. write to the port) your data in direct byte format. You know exactly what you are sending. Then in matlab you process the byte stream directly. By the way, you will notice that oscP5 uses this library as well. I hope this helps,

    Kf

  • Thank you both for your input. kfrajer@ could you please explain a bit more, how i can i implement the decapsulation of TCP packet in matlab , which i am sending from processing.

    I am sorry, if i sound stupid, i am completely new to this thing.

  • Answer ✓

    @gavanpreet28 Your data makes sense. If you check the data that you received against an ASCII table, you will be able to decode your data. Code 47 is '/'. Codes 116,101,115,116 are t,e,s,t which corresponds to the label associated to the oscP5 msg that you sent initially. To fully decode the message, I suggest sending some constants of known values and then study the received data in matlab. You should be able to decode the information after you understand the data package format from oscP5. I hope this helps,

    Kf

Sign In or Register to comment.