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.
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.
08-25-2016 10:34 AM - edited 08-25-2016 10:35 AM
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!
Solved! Go to Solution.
08-25-2016 11:48 AM
@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.
08-25-2016 01:09 PM
@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.
08-25-2016 01:26 PM
@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.
08-25-2016 01:48 PM
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?
08-25-2016 02:07 PM
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.
08-25-2016 02:20 PM
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!
08-25-2016 02:26 PM - edited 08-25-2016 02:28 PM
On a side note, you can also typecast the string directly to a boolean array using 4.x mode. (Same result here)
08-25-2016 03:26 PM
@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.
08-25-2016 10:59 PM
@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: