LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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

Solved!
Go to solution

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!

0 Kudos
Message 1 of 12
(5,749 Views)

@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.


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 2 of 12
(5,720 Views)

@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 

0 Kudos
Message 3 of 12
(5,702 Views)
Solution
Accepted by topic author Blokk

@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.


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 4 of 12
(5,696 Views)

Haha, it is indeed simple solution! Thanks! 🙂

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

 

rrreerfedfger.png

 

0 Kudos
Message 5 of 12
(5,689 Views)

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.


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 6 of 12
(5,679 Views)

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!

0 Kudos
Message 7 of 12
(5,673 Views)

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

 

Message 8 of 12
(5,672 Views)

@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.


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 12
(5,656 Views)

@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

 

 

0 Kudos
Message 10 of 12
(5,627 Views)