LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unflatten string to cluster, cluster includes a string

I am reading in a binary string written by a C++ application.  For example, let's say the string is made up of the following 3 data types:
 
1.  16 bit signed integer
2.  string (10 characters long)
3.  16 bit unsigned integer
 
This is a simplification, but it exposes the problem with unflattening a binary string to a cluster with a string in it.
 
I use the Unflatten From String function and the type is a cluster of the above listed elements.  The problem is that Labview has no way of knowing how long the string is.  One work around that I an not very fond of is to insert the string length into the binary string immediately after the 16 bit signed integer.
 
I attached a simple example.
 
Can anyone suggest any other ideas?
 
Thanks,
Chris
Chris Reyerson
Optical Systems Engineer
Arete Associates
Tucson, AZ
0 Kudos
Message 1 of 9
(4,882 Views)
Pascal uses the first byte of a string to define its length.  C inserts a NULL at the end of the string.  I like the C method of using a NULL (ASCII code 0) to mark the end of a string.  Just read in characters until you see the NULL.  If you used either method, you would not have to add another element to your cluster.
- tbob

Inventor of the WORM Global
Message 2 of 9
(4,866 Views)

One way or another, LabVIEW needs to know the string lenght.

One possibility would be to dissect the raw string manually with some custom code instead of using a plain unflatten operation. The attached (LabVIEW 8.0) would be one possibility).

(BTW: The data types of the first and last element were reversed in your sample compared to their label)

Message Edited by altenbach on 01-26-2006 12:13 PM

Message 3 of 9
(4,861 Views)
If you don't know the length of the string then you can do something that combines the above two suggestions. See the attached VI.

I included a creation of a simulated binary string.
Message 4 of 9
(4,841 Views)

Thanks for the suggestions, I did not know that C++ placed a null character at the end of a string.

The assumption is that I fully know the definition of the binary string that comes from the C++ app.  The problem, fundamentally, comes from the fact that strings (and arrays) are of indeterminate length.  The preferred solution is for the Unflatten From String VI to "ask" the user for the length information when populating a cluster with an indeterminate data type such as a string or array. 

Up till now, I have been inserting a 4 byte integer containing the length of the string into the binary string immediately before the output string (Labview stores strings in memory with a four byte header indicating the length).  This allows Labview to properly load the binary string into the cluster (if the header is not there, labview assumes the first four bytes of my string are the header).  This is awkward.  I maintain a cluster definition (a custom control that is a typedef).  I just want to unflatten the incoming binary string using the typedef cluster.  We are frequently revising the cluster and this means that in addition to modifying my custom control, I have also to modify the insert string code.

Any more suggestions?

Chris Reyerson
Optical Systems Engineer
Arete Associates
Tucson, AZ
0 Kudos
Message 5 of 9
(4,836 Views)
If the flattened string is the following C structure:

struct {
   int I16;
   char Str10[10];
   unsigned int U16;
}

then the string lenght is always 10, padded after the zero end char. The attached VI shows how to unflatten it.
It also show how to unflatten when the string is preceded by a 4byte length.

Message Edité par Jean-Pierre Drolet le 01-26-2006 11:41 PM



LabVIEW, C'est LabVIEW

Message 6 of 9
(4,820 Views)
Oops, I'm losing my edge it was late... In the second example with 4byte length, the string can be unflatten to a cluster with a single unflatten node because the string is already flatten with the format LabVIEW uses.


LabVIEW, C'est LabVIEW

0 Kudos
Message 7 of 9
(4,797 Views)

Hi Jean Pierre,

 

Thanks for your response.  Labview 8 offers and even easier way to unflatten a string than what you show in your second example.  One of the inputs for the Unflatten from String VI is, “Data Includes Array or String Size (T)”.  Wire up a false to this node and the string (or array) does not need the four byte length header. 

 

Also, Labview 8 asks for the endian type, this is a major improvement for me since previous version of Labview assumed big endian and all my data was little endian.  I can't tell you how long I had to work to find a solution to the problem.

 

Thank you,

Chris

Chris Reyerson
Optical Systems Engineer
Arete Associates
Tucson, AZ
0 Kudos
Message 8 of 9
(4,786 Views)
If you wire False to "Unflatten from String" the whole input string is returned as output. That's because there is no length indication and it doesn't recognize any termination character.
This is essentialy a noop.


LabVIEW, C'est LabVIEW

0 Kudos
Message 9 of 9
(4,766 Views)