cancel
Showing results for 
Search instead for 
Did you mean: 

bit array binary string (from TCP Read) to LV boolean array (and back)

SOLVED
Highlighted
Blokk
Trusted Enthusiast
Solved!

bit array binary string (from TCP Read) to LV boolean array (and back)

I would like to ask what is the most straightforward way for the following conversion? We use aTCP Read function to read an incoming binary string from a S7 PLC using LabVIEW. We know that LabVIEW stores a Boolean in 8 bits. The incoming binary string contains 14 bits (representing 14 status flags). What is the best way to convert these 14 bit values into LabVIEW Boolean array?

 

Also, we need to send a LAbVIEW Boolean array into to the PLC in a form of data string (to TCP Write) containing status bits.

Thanks very much!

11 REPLIES 11
crossrulz
Knight of NI

Re: bit array binary string (from TCP Read) to LV boolean array (and back)


@Blokk wrote:

The incoming binary string contains 14 bits (representing 14 status flags). What is the best way to convert these 14 bit values into LabVIEW Boolean array?


So do you read 2 bytes in the TCP message?  Or are you recieving 14 bytes?  How exactly is the data formatted?  Examples of inputs and what you want for outputs often help here.



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
Blokk
Trusted Enthusiast

Re: bit array binary string (from TCP Read) to LV boolean array (and back)

Message contains an image

@crossrulz wrote:

@Blokk wrote:

The incoming binary string contains 14 bits (representing 14 status flags). What is the best way to convert these 14 bit values into LabVIEW Boolean array?


So do you read 2 bytes in the TCP message?  Or are you recieving 14 bytes?  How exactly is the data formatted?  Examples of inputs and what you want for outputs often help here.


The Siemens PLC sends a specially structured binary string. We use a single TCP Read function to read out 32 bytes in form of binary string. The first part of the 32 byte structure is a Word (16 bits). This part stores the size of the rest: value 30 (bytes). We use the Unflatten From String function, since when we unflatten first the first 2 bytes, it outputs the rest: 30 bytes. This 30 bytes contains some more data, including 14 bit flags (size is 14 bits). Since the PLC can only put "together" Words (16 bits) at its side before sending the TCP data, the 14 bits is padded with 2 bits not holding any valueable info.

 

The question is that, how should we use the "Unflatten from string" function in this case? So the PLC send 16 bits, we need to get the values of the first 14 bits: and convert them into a LV Bool array containing 14 Boolean values.

 

rrrrrrr.png 

crossrulz
Knight of NI
Solution

Re: bit array binary string (from TCP Read) to LV boolean array (and back)


@Blokk wrote:

This 30 bytes contains some more data, including 14 bit flags (size is 14 bits). Since the PLC can only put "together" Words (16 bits) at its side before sending the TCP data, the 14 bits is padded with 2 bits not holding any valueable info. 


Then the solution is simple.  Take your 2 bytes and unflatten into a U16.  Then use Number To Boolean Array.



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
Blokk
Trusted Enthusiast

Re: bit array binary string (from TCP Read) to LV boolean array (and back)

Message contains an image

Haha, it is indeed simple solution! Thanks! Smiley Happy

This is what is in my mind based on your info, this should work in both direction, yes?

 

rrreerfedfger.png

 

crossrulz
Knight of NI

Re: bit array binary string (from TCP Read) to LV boolean array (and back)

That is mostly what I was thinking.  Some quick notes:

1. No need for the FALSE constants with the Flatten/Unflatten To/From String since you are not using arrays at that point.

2. The PLCs I have had to talk to like this used Little Endian data.  So just be aware of that.



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
Blokk
Trusted Enthusiast

Re: bit array binary string (from TCP Read) to LV boolean array (and back)

About the False input for the Unflatten/Flatten from/to String functions: regarding to the Help of the functions, they use the Bool input to decide if there is size header in case of strings too, not only for arrays. No?

 

And yes, we will test it the byte orders, and check whether we need to reverse the byte order due to "endianess" differences...

 

Thanks again!

altenbach
Knight of NI

Re: bit array binary string (from TCP Read) to LV boolean array (and back)

Message contains an image Message contains an attachment

On a side note, you can also typecast the string directly to a boolean array using 4.x mode. (Same result here)

 

crossrulz
Knight of NI

Re: bit array binary string (from TCP Read) to LV boolean array (and back)


@Blokk wrote:

About the False input for the Unflatten/Flatten from/to String functions: regarding to the Help of the functions, they use the Bool input to decide if there is size header in case of strings too, not only for arrays. No?


That only matters if the unflattened data is a string, which you do not.



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
Blokk
Trusted Enthusiast

Re: bit array binary string (from TCP Read) to LV boolean array (and back)

Message contains an image

@altenbach wrote:

On a side note, you can also typecast the string directly to a boolean array using 4.x mode. (Same result here)

 


Thanks altenbach! The only difference here is that, if the binary string has more data after the starting 16 bits. In this case I imagine the typecast will try to convert ALL the string into Boolean array, yes? The "Unflatten from String" has the benefit that it gives me the rest ("rest of the binary string") so I can make a conversion "chain" through the special PLC data structure:

uuuuuuu.png