03-20-2017 08:26 AM
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:
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.
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.
This solution works, but it's not at all a satisfying solution.
Thank you in advance for all the help.
03-20-2017 08:37 AM - edited 03-20-2017 09:01 AM
03-20-2017 08:46 AM
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.
03-20-2017 08:55 AM
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.
03-20-2017 09:05 AM
I tried both advices, yours and Gerd's advice.
I changed the Fragment Data[] Array to a string component according to the image below.
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:
Just in case I put the according TypeDefs in the appendix.
03-20-2017 09:12 AM - edited 03-20-2017 09:12 AM
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…
03-20-2017 10:18 AM
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.
03-20-2017 11:37 AM
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.
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?
03-20-2017 12:38 PM
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.
03-20-2017 12:46 PM
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.