From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Byte array to specifier Type

Solved!
Go to solution

Hello everyone.

 

In my project I have a connection to an external device through an RS-232 connection. Now I read the bytes and want to convert these to a specified type, like I32 or U8.

 

When data arrives, i convert the string to a byte array. For one portion I have to convert a array of 4 bytes to an I32. This means that the bytes are litterary the data that has to be converted into the data type. 

 

I have no idea how to do this, could you guys please help me?

 

Kind Regards,

Jesse Bax

Swissdrones Operating AG

0 Kudos
Message 1 of 15
(4,062 Views)

Hi,

How I understand Your task, You want somethink like this

With best regards

Certified LabVIEW Architect (CLA)
0 Kudos
Message 2 of 15
(4,037 Views)

I would use the Unflatten From String to convert directly from the VISA Read's string output.  That function also lets you choose the Endianness of the I32, which is why I would use it over the Type Cast function.


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 15
(4,034 Views)

Yes,

Thank You, Like this

With Best Regards

Certified LabVIEW Architect (CLA)
0 Kudos
Message 4 of 15
(4,028 Views)

Hello,

 

Sorry I made my question a little vague. The string I get is not for just one number, but multiple. See attachment for contents. 

 

As you can see, there are 15 variables i have to convert from the string to data. In the 3rd column it says "type", this is the type where the data fits in. 

 

When i do it wrong, for example: 1000001bin 

When i convert this Byte to an U8 then the decimal number would be: 129

when i convert this Byte to an I8 then the decimal number would be: -127

 

now for my problem the data that i receive already represents the values of the specified type, only i want to make it so that labview knows what data type it is

 

Kind regards,
Jesse Bax

Swissdrones Operating AG

0 Kudos
Message 5 of 15
(3,999 Views)

This is exactly what the built-in function "join Numbers" is for.

Take a substring of the first 4 bytes and convert the bytes to I8 with a cast or string to byte array and then change to I8.

For the next 6 values, take groups of 4 bytes and change Join pairs to get two 16 bit numbers and join those to get 32.  The order they are joined depends on if the instrument is sending big endian or little endian values.  But you should be able to figure out from the data stream the order to combine things to get the value. Convert it to I32.

 

For the next 9 values just take pairs an "join Numbers" to make 16 bit integers.

 

Join Numbers is your friend here.  It is on the Numeric->Data Manipulation palette.

LabVIEW ChampionLabVIEW Channel Wires

0 Kudos
Message 6 of 15
(3,988 Views)
Solution
Accepted by topic author Feignedjesse2

@sth wrote:

This is exactly what the built-in function "join Numbers" is for.


That's too much work.  Just create a cluster of all of the data in the message and Unflatten From String will do everything for you.


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 7 of 15
(3,968 Views)

If and only if, the endian of the receiving machine is the same as the transmitted byte order.  Endian issues are still around.

LabVIEW ChampionLabVIEW Channel Wires

0 Kudos
Message 8 of 15
(3,959 Views)

@sth wrote:

If and only if, the endian of the receiving machine is the same as the transmitted byte order.  Endian issues are still around.


Yes, but "Unflatten from String" has an endianness input, so it can be switched while unflattening.

0 Kudos
Message 9 of 15
(3,954 Views)

Right, I had forgotten that.  I use the unflatten for my network communication, but got in the habit of join numbers for dealing with odd instruments.

LabVIEW ChampionLabVIEW Channel Wires

0 Kudos
Message 10 of 15
(3,950 Views)