LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to efficiently read network data into struct and correct endianess

Solved!
Go to solution

Go and compare your diagram with that from Nathan!

You can not place an array in a cluster and hope that LabVIEW Unflatten will treat that as an embedded fixed size array. Instead LabVIEW expects in the data stream a 32 bit integer that contains the number of array elements in that case. Make a cluster of 3 floating point values instead of the array and you will be fine!!

Rolf Kalbermatter
My Blog
0 Kudos
Message 11 of 16
(649 Views)

Ah, I did read that, but I forgot when I switched back to my code.  I have made the change and everything works.  However, this leads me to a new question.  What if instead of 3 or 20 values, I have 1000?  How does one make such a cluster (typdef)?  

0 Kudos
Message 12 of 16
(639 Views)

@tysonl wrote:

 

What if instead of 3 or 20 values, I have 1000?  How does one make such a cluster (typdef)?  


Not really! There are tricks but they are cumbersome. Instead you process the byte buffer in multiple Unflatten operations.

First the part that is simple to represent as a cluster/struct. Then you get the bytes from the string at the correct offset and with the correct length and then pass it to Unflatten with an array constant as data type but set the input "data includes array size or string size (T)" set to false.This tells the function to simply take as many elements from the binary string buffer as possible, without trying to read first an int32 that indicates the number of array elements.

Rolf Kalbermatter
My Blog
0 Kudos
Message 13 of 16
(634 Views)

@tysonl wrote:

What if instead of 3 or 20 values, I have 1000?  How does one make such a cluster (typdef)?  


That is why I said what I did here: https://forums.ni.com/t5/LabVIEW/How-to-efficiently-read-network-data-into-struct-and-correct/m-p/36...


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 14 of 16
(620 Views)

I have tried your suggestion rolfk and it works.  Some code is attached below.  The next issue is handing data that contains a mix of values and arrays.  I could configure my device to transmit the array sizes before the arrays.  That way I could receive the size, flatten it, append it onto the front of the array, and then unflatten the array with the appended size flag turned on.  I will probably experiment with this later. 

tcp_client_some_data.pngUnflattenSomeData.viUnflattenSomeData.vi

EDIT:  @crossrulz - I did see your post.  I used your post and rolfk's post to develop the above.  

 

THANKS!

0 Kudos
Message 15 of 16
(616 Views)

@tysonl wrote:

 

The next issue is handing data that contains a mix of values and arrays.  I could configure my device to transmit the array sizes before the arrays.  That way I could receive the size, flatten it, append it onto the front of the array, and then unflatten the array with the appended size flag turned on.  I will probably experiment with this later.  


You mean with data arrays that are not fixed size?

 

You could then do something like this:

 

struct
{
      int32_t counter;
      int32_t status;
      int32_t length;
      float array[256];
} DataStruct;

DataStruct data;

data.counter = something;
data.status = something_else;
data.length = sizeof(data.array) / sizeof(data.array[0]); // or some other dynamic size of elements in the data array;

send(s, &data, offsetof(DataStruct, array) + data.length * sizeof(data.array[0]));

And now you could define a cluster in LabVIEW that contains two int32 in the beginning and then an actual single precision array and wire this directly to a single Unflatten function, since you added the additional int32 length value that LabVIEW expects to find in the data stream when unflattening arrays inside a cluster.

Rolf Kalbermatter
My Blog
0 Kudos
Message 16 of 16
(611 Views)