LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Typecasting byte array to cluster

Situation: I'm communicating with a device, sending and receiving blocks of data. The communication primitives take and give byte arrays: array of U8. When sending out commands, I convert my command cluster into a byte array and send it; this works:Cast from Cluster.PNG

But when I go to do the reverse operation and take the response data to an appropriate cluster, I get an error. If I click on it, the message says "Polymorphic terminal cannot accept this datatype." The cluster is a typedef, and its file is attached. All elements are numeric but not all of the same length – some are 16-bit and some are 32-bit.

 

Cast to Cluster.PNG

I really need this functionality; do I have to write an extension in C just to tell LabVIEW that a certain pointer really should be viewed as pointing to my cluster?

 

I sense that something might be wrong having to do with the color of the wires. What distinguishes a "pink cluster" from a "brown cluster" anyway? (The Swap functions are there because the device is little endian but my Intel computer is big endian.)

Download All
0 Kudos
Message 1 of 7
(4,632 Views)

Brown = all numeric, fixed-size elements.  Can do math operations on it.

Pink = Not brown

 

Basically as soon as you put in strings or arrays it goes pink, or any other data type that can change size.

 

You should be able to typecast a U8 array to the cluster if it's brown...

 

If you need to use a pink cluster, you can still manually assemble it.  Use the "Join numbers" node in the same palette as the Type Cast to push two U8s into a U16, and 2 U16s into a U32.

Message 2 of 7
(4,628 Views)

Your Response Cluster image does not match what you attached.  Do you have the update?

 

Furthermore, do you have an example of read data and what you expect the cluster data to be?  That is the only way we will really be able to figure a work around for you.

 

At the top of my head, I would use the Unflatten From String instead of the Type Cast since it will give you a lot more options for how the data is to be formatted.  You will need to use the Byte Array To String first.

 

Another option is the Coerce To Type, which became official in LabVIEW 2018.  You can use a snippet in the linked idea to use it.

Never mind.  The Coerce To Type will not work.


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 3 of 7
(4,611 Views)

The first 7 elements of your two clusters need to match in type and cluster order. They do not. (e.g. the third element is U16 in the command and U32 in the response and even if the remaining were correct (they are not!), you'll get a frame shift due to that mismatch alone)

 

Once you fix that, your attempt will work just fine (no broken wires, correct result)

 

ClusterCast.png

 

Remember that a flattened cluster only contains the raw bits of the cluster elements appended in cluster order. There are no delimiters and no information about element names.

 

Obviously, your reponse probably has nothing to do with the command cluster, so you simply need to ensure that the order and type is correct. Can you attach your code containing a typical response string (or U8 array)? What does the documentation say about the structure of the bytes coming back from the instrument? Why are you keeping only 20 bytes? What else would be in there?

0 Kudos
Message 4 of 7
(4,574 Views)

Hi team , 

 

Sorry for opening the old thread again . But i found that no solution over this thread . 

 

i had 1 query too ! 

 

What if , the cluster had a mix up of Numeric , boolean and Strings. 

In my case , i need to bind all these data's into cluster and will send to receiver side using TCP ip protocol . ( cluster - with all above mentioned  datatypes)

 

my requirement  : i had around  30 different controls (mix up of above datatypes ) , i need to send to the receiver side using TCPIP .

Kindly post your suggestions . it would be really helpful for me . 

 

Thanks in advance ! 

0 Kudos
Message 5 of 7
(2,399 Views)

When using in TCP/IP or UDP communication, we usually just use Flatten To String and Unflatten To String to convert from cluster to string for TCP Write or from string to type def cluster for TCP Read.

-------------------------------------------------------
Control Lead | Intelline Inc
Message 6 of 7
(2,396 Views)

@Mrtechie wrote:

Sorry for opening the old thread again . But i found that no solution over this thread . 


Probably because this thread is about typecasting a "byte array to a cluster" while your question deals with sending cluster data over the network where you have control over the code on both sender and receiver end. A completely different topic!

 


@Mrtechie wrote:

i had 1 query too !  


Then you should start a new thread (after searching the forum for existing solutions) and clearly formulate the question, even attach some simplified code (VI, not pictures!), including the typedef of your cluster (I really hope it is a typedef and all elements are named well!).

 

As has been said, network traffic is always just string data and if the length is not known, you should first send size information separately. Have a look at the TCP/IP examples that ship with LabVIEW. For the cluster data, all you need is flatten it to a string for sending, then unflatten it at the receiver using a diagram constant of the typedef cluster as type.

 

See how far you get.

Message 7 of 7
(2,383 Views)