DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Interpret a binary data channel

I have a TDMS file that contains a U64 data channel. There's a binary interpretation of each U64 like this: bit 0..7 is current; bit 8..15 is voltage; bit 16 is a status; ... 

 

I would like to split these bits into separate channels (and group them when appropriate, like for voltage and current) upon calling a script. However I couldn't find any function to do this interpretation (I'm on Diadem 2020, if that's relevant). Do you have an idea on how to accomplish this?

 

It would be possible to save as separate channels at the source, but the file size of the TDMS file rises very quickly when doing that; so that's not an option for me. I'd really like to do this in memory when required.

----------
Although I've been 10+ years long fan of LabVIEW, I started to discourage engineers to start new projects in a SaaS language. NI must first regain trust within its community.
0 Kudos
Message 1 of 3
(526 Views)

Hi Benjamin,

 

Here is a guide on how to create a script to split the 64bit values from your encoded channel into channels for current, voltage, etc.

 

1. Create the Script

First open the script tab in DIAdem, and create a new Python Script (File -> New Python)

 

2. Import the DIAdem API

The Python script should start with the following line:

from DIAdem import Application as dd

The "dd" object provides the API to interact with DIAdem.

 

3. Get the values from your 64bit encoded channel

Note: This assumes the group&channel are already loaded into your data portal on the right hand side. (See image)

Leonard_Gartner_0-1744372255054.png

You can access the values in the channel with the following code:

GROUP_NAME = "group_1"
CHANNEL_NAME = "channel_1"

channel = dd.Data.Root.ChannelGroups(GROUP_NAME).Channels(CHANNEL_NAME)

for i in range(channel.Size):
    value = channel[i + 1]
# do something with value

 

4. Split the 64bit values

The values in your 64bit channel are interpreted as floats in the Python script. To split them, you must first convert them to their binary representation. You can use this function:

def float_to_bin_str(float_val):
    return str(bin(int(float_val)))[2:].zfill(64)

The splitting is then done for each value in your 64bit channel (in a for loop) 

 

for i in range(channel.Size): 
    value = channel[i + 1]
    bin_str = float_to_bin_str(value)
    current_bin_str = bin_str[-8:]
    voltage_bin_str = bin_str[-16:-8]
    # ...

 

 

 

5. Convert binary current, voltage, ... to actual numbers

This depends on your data, whether it is int values, float values, etc. Hence I will not provide a script example here.

 

 

6. Create new channels for voltage, current, ... and add the new values

You create new channels like:

 

current_channel = dd.Data.Root.ChannelGroups(GROUP_NAME).Channels.Add("current", dd.DataTypeChnFloat64)
voltage_channel = dd.Data.Root.ChannelGroups(GROUP_NAME).Channels.Add("voltage", dd.DataTypeChnFloat64)
# add more channels if needed, e.g. "status"

 

You can add the values to the channel in the same for loop as before, after splitting the 64bit value:

for i in range(channel.Size):
    value = channel[i + 1]
    bin_str = float_to_bin_str(value)
    current_bin_str = bin_str[-8:]
current_value = custom_bin_str_to_current_val(curent_bin_str) current_channel.SetValues(current_value, i + 1)
# ...

 

 

... After running the script (according to the snippets above) the data portal will contain the additional channels with your data.

 

Let me know if these steps are aligned with your question! I am happy to share more information on this topic.

 

Best regards

Leonard 

Message 2 of 3
(28 Views)

Thanks for taking the time to reply. I had not considered using Python scripting for that, as I'm more used to VBscript. But great idea!

----------
Although I've been 10+ years long fan of LabVIEW, I started to discourage engineers to start new projects in a SaaS language. NI must first regain trust within its community.
0 Kudos
Message 3 of 3
(17 Views)