简体   繁体   English

java:接收UDP数据包以及如何保存它们

[英]java: receiving UDP packet and how to save them

I receive udp data in my array (byte[] data) from simulink udp block . 我从simulink udp块接收数组中的udp数据(字节[]数据)。 the data is packed as int32 , so first I need to unpack that . 数据打包为int32,所以首先我需要将其解压缩。

I don't know how can I save this data to be able to use that . 我不知道如何保存这些数据才能使用它。 these data are positions and I want to visualize them using OpenGL ES. 这些数据是位置,我想使用OpenGL ES对其进行可视化。 I want to save the data into an array and be able to add the next packets to that array in next iteration, not rewrite the whole array (because of the loop ) 我想将数据保存到数组中,并能够在下一次迭代中将下一个数据包添加到该数组中,而不是重写整个数组(由于循环)

the size of the data is 1200 * 96 for now . 数据的大小现在是1200 * 96。 is array a good option ? 数组是一个好选择吗?

       int j = 0 ;
  float[] bin1 = new float[(data.length/2)];
  while (j < data.length ) {
    if ( data[2*j+2] >= 0  ) {

      String unhx =(binary(data[2*j+3])+binary(data[2*j+2])+binary(data[2*j+1])+binary(data[2*j]));
      float unbin = ((float)unbinary(unhx)/100);
      bin1[j/2] = unbin;
      print(bin1[1]);
    }

    else if  ( data[2*j+2] < 0 && data[2*j+3] < 0 ) {
      data[2*j] = (byte)(-data[2*j]);
      data[2*j+1] = (byte)(-data[2*j+1]);
      String unhx =(binary(data[2*j+1])+binary(data[2*j]));
      float unbin = ((-1)*(float)unbinary(unhx)/100);
      bin1[j/2] = unbin;
      print(bin1[1]);
      }
      j = j + 2;
  }

now the problem is that each time the new packet comes it rewrites the whole bin1 array , how could I add the new packets to the bin1 not rewrite the whole thing? 现在的问题是,每次收到新数据包时,它都会重写整个bin1数组,我怎么能将新数据包添加到bin1中而不重写整个东西呢?

One problem what i see here is in while loop you are using counter as variable int j=0 but no where you are incrementing your counter variable j that may be the problem. 我在这里看到的一个问题是,在while循环中,您将counter用作变量int j=0但是没有在增加计数器变量j情况下出现问题。

j = 0 ;
while (j < data.length){
float[] array = new float[] {myData};
j++;
}

Again Melisa, you have to declare your array(s) BEFORE you enter the while loop. 同样,Melisa,您必须在进入while循环之前声明数组。 So that they will remain in scope once you leave the loop. 这样,一旦您退出循环,它们便会保留在范围内。 This means, you'll still be able to access the array after you leave the loop. 这意味着,退出循环后,您仍然可以访问阵列。 :) Hope this helps. :) 希望这可以帮助。

Reply to your edited question: 回复您已编辑的问题:

Melisa, you might want to consider using a List of floats rather than an array of floats. Melisa,您可能要考虑使用浮点数列表,而不是浮点数数组。 Otherwise, you might find yourself resizing and copying over the array to add on more data. 否则,您可能会发现自己需要调整大小并在阵列上进行复制以添加更多数据。 A list/linked list theoretically has unlimited length (you really don't have to concern yourself with the length) and you can just keep adding on floats. 理论上,列表/链接列表的长度是无限的(您实际上不必担心长度),您可以继续添加浮点数。

List 清单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM