LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert byte array to specific data type

Hello everyone

 

In my project I'm communicating to a Datalogic Matrix 300 Scanner via Ethernet/IP protocol. From the scanner I receive an Array of Bytes and I'd like to convert it to different data types (USINT, UINT, BYTE, BYTE[]), so that the structure might be something like this:

 

DataArrayFromEIP.JPG

 

I already tried something similar to this topic with a cluster array, see image below. It would have been to easy if it works that way...  unfortunately the conversion doesn't work and I got en Error 74 in the "Unflatten From String" VI.

 

Conversion.JPG

 

Now I coded it the hard way. I got my byte array on the input and I always cut away either 8-bits, 16-bits or a variable length of bits for the fragment data - see image below.

 

SeparateDataArray.JPG

 

 

 

This solution works, but it's not at all a satisfying solution.

  • How would you approach this problem?
  • What would you suggest in order to get a re-usable code?

 

Thank you in advance for all the help.

 

 

0 Kudos
Message 1 of 21
(6,896 Views)

Hi wsteiner,

 

read in the array of 16 bytes and TypeCast it into a cluster containing the needed output datatypes. Don't use an array for the last 6 bytes:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 21
(6,891 Views)

You can try unflatten from string, but read the help file on that function and pay attention to all the other connections.

 

There is one connection called "Data Includes Array or String Size (T)"  which means it defaults to true when nothing is wired in.  Your flatten to string has a similar input called "Prepend Array or String Size (T)".   Try wiring a false into both of thos

 

But I would tend to go with Gerd's advice and try to work with typecast.

0 Kudos
Message 3 of 21
(6,884 Views)

RavensFan wrote:

There is one connection called "Data Includes Array or String Size (T)"  which means it defaults to true when nothing is wired in.  Your flatten to string has a similar input called "Prepend Array or String Size (T)".   Try wiring a false into both of thos


Unfortunately, it does not work for embedded arrays (ie: that array inside of the cluster).  That is what is causing the error.

 

In this case, I would unflatten all of the individual parts as well.


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 4 of 21
(6,878 Views)

I tried both advices, yours and Gerd's advice.

 

I changed the Fragment Data[] Array to a string component according to the image below. ClusterWithString.JPG

 

Now I tested it with the option "Data Includes Array or String Size(T)" on false, which unfortunately didn't work. 

I also tested the TypeCast proposed by Gerd, but somehow I'm not able to TypeCast it to the desired cluster. See as well the image below:

TypeCastFromCluster.JPG

 

Just in case I put the according TypeDefs in the appendix.

  • Any ideas why that doesn't work as expected?

 

0 Kudos
Message 5 of 21
(6,864 Views)

Hi wsteiner,

 

see may suggestion above: all unnamed constants in the cluster are U8, I only changed the named ones to U16.

I recommend to make a typedef cluster and provide unique names to each constant. And check for the endianness of the U16 data, maybe another ByteSwap is needed…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 21
(6,859 Views)

GerdW wrote:  And check for the endianness of the U16 data, maybe another ByteSwap is needed…

This is why I prefer to use the Unflatten From String; there is an input for Endianness.


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 7 of 21
(6,834 Views)

Thank you for your recommendation.

 

Now I took only the fixed part of the data array, used the TypeCast VI and did the ByteSwap where it was needed.

This works as expected and is much cleaner than the code I had before.

FixedPart.JPG

 

And for the variable part I just cut out the variable part by the "Delete from Array". This looks promising to me as well.

The only downside, I had to use a constant (10) in order to count the number of bytes in my fixed part. 

Does someone know a good way around this code smell?

VariablePart.JPG

 

 

 

0 Kudos
Message 8 of 21
(6,819 Views)

wsteiner wrote:

Now I took only the fixed part of the data array, used the TypeCast VI and did the ByteSwap where it was needed.

This works as expected and is much cleaner than the code I had before.

 

And for the variable part I just cut out the variable part by the "Delete from Array". This looks promising to me as well.

The only downside, I had to use a constant (10) in order to count the number of bytes in my fixed part. 

Does someone know a good way around this code smell?


I would go back to using the Unflatten From String and use the fixed part of the cluster as the data type.  You can specify the endianness to be Little Endian.  Also, there is an output for "Remaining String".  You can just use String To Byte Array on that remaining string to get your byte array.


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
Message 9 of 21
(6,806 Views)

Because you are using Bundle by Name, you do not need to wire straight across on the cluster elements that do not change.

 

Just delete those wires and the elements from the unbundle and bundle by name.  They will keep the same values they had based on the wire you have coming into the top of the Bundle by Name.

0 Kudos
Message 10 of 21
(6,800 Views)