简体   繁体   English

将 XML 作为任务/集群读取到 LabVIEW,使用 Python 配置 DAQ

[英]Read XML to LabVIEW as task / Cluster using Python to configure DAQ

I am trying to load an XML file that will be used to configure my DAQ in LabVIEW.我正在尝试加载一个 XML 文件,该文件将用于在 LabVIEW 中配置我的 DAQ。

So when looking at the options I have:因此,在查看选项时,我有:

  1. my XML channel configuration is dynamic - meaning one XML may have 3 channels the other may have >100 channels.我的 XML 通道配置是动态的——这意味着一个 XML 可能有 3 个通道,另一个可能有 >100 个通道。
  2. would be much easier for me to pars the xml in Python and pass the data as structure/task to LabVIEW.对我来说,解析 Python 中的 xml 并将数据作为结构/任务传递给 LabVIEW 会容易得多。

eg for XML channel例如 XML 频道

<Channels>
        <Channel name="CH_PH1" enable="True" visible="False" virtual="False" units="A" physical_channel="PXI1Slot2/ai0" b_factor="0" m_factor="1033.221069" measurement_type="VOLTAGE" type="D" max_value="0.1" min_value="-0.1">
          <RollingAvg do_rolling_avg="False" rolling_avg_frame_sec="0" />
        </Channel>
        <Channel name="CH_PH2" enable="True" visible="False" virtual="False" units="A" physical_channel="PXI1Slot2/ai1" b_factor="0" m_factor="1073.772766" measurement_type="VOLTAGE" type="D" max_value="0.1" min_value="-0.1">
          <RollingAvg do_rolling_avg="False" rolling_avg_frame_sec="0" />
        </Channel>
        <Channel name="P_CH" enable="True" visible="False" virtual="True" units="V" formula="CH_PH1*CH_PH2" doTotalEnergy="False">
          <RollingAvg do_rolling_avg="False" rolling_avg_frame_sec="0" />
        </Channel>
        <Channel name="P_PH_Total" enable="True" visible="False" virtual="True" units="V" formula="CH_PH1+CH_PH2" doTotalEnergy="False">
          <RollingAvg do_rolling_avg="False" rolling_avg_frame_sec="0" />
        </Channel>
</Channels>

My code in Python to parse the xml:我的代码在Python解析xml:

def xml():
    import xml.etree.ElementTree as ET
    tree = ET.parse(r'C:\Users\LAB_PTG\Desktop\Cosmo_v0.93.6\nidaq.xml')
    root = tree.getroot()
    P={}
    C={}
    for Channel in root.iter('Channel'):
        z=(Channel.attrib)
        if z['virtual']=='False':
            P.update({z['name']:(z['physical_channel'],z['enable'],z['m_factor'],z['max_value'])})
        else:
            C.update({z['name']:(z['formula'])})
    print(P,C)

Help needed:需要帮助:

  1. how to execute the code in LabVIEW?如何在LabVIEW中执行代码?
  2. how to handle DICT in LabVIEW?如何在 LabVIEW 中处理 DICT? or is there a better solution to pass the data?还是有更好的解决方案来传递数据?
  3. use the paras data to dynamically configure the DAQmx channels使用参数数据动态配置 DAQmx 通道

I didn't manage creating a xml parser in LabVIEW as it enforces more hard coding and having a fixed stricture.我没有设法在 LabVIEW 中创建 xml 解析器,因为它强制执行更多的硬编码并具有固定的结构。

I am targeting the LV DAQmx continues sampling example and replace the configuration with the parsed xml.我的目标是 LV DAQmx 继续采样示例,并将配置替换为已解析的 xml。

在此处输入图像描述

Thanks for every one supporting Assaf Baker感谢每一位支持 Assaf Baker 的人

Parsing XML in Labview在Labview中解析XML

Parsing XML in LV is a bit tricky, but not that difficult.在 LV 中解析 XML 有点棘手,但并不难。 Maybe, you give it a try?也许,你试一试?

This code loops over all channels, extracts attributes name and virtual , and from their children RollingAvg , it extracts the attribute do_rolling_avg .此代码遍历所有通道,提取属性namevirtual ,并从它们的子RollingAvg中提取属性do_rolling_avg

在此处输入图像描述

May be, it is more handy to get a list of all attributes / values of a node first, and then work on the generated array:可能,首先获取节点的所有属性/值的列表,然后处理生成的数组会更方便:

在此处输入图像描述

Now, it is important to close all references at the end to avoid a memory leak, which is done quick and dirty, here.现在,重要的是在最后关闭所有引用以避免 memory 泄漏,这是快速而肮脏的,在这里完成。

Further more, if something is not found, the search VIs will throw an error.此外,如果未找到某些内容,搜索 VI 将抛出错误。 (Your Python will do so, too). (您的 Python 也会这样做)。 Some error handling should be added if necessary.如有必要,应添加一些错误处理。


If you insist on using Python如果你坚持使用Python

Today, there's an option to run Python directly from LV.今天,有一个选项可以直接从 LV 运行 Python。 I don't know this, my LV is from 2017. There, it is possible to use the System Exec.vi to run any command on the commandline, and read its output back into LV:我不知道,我的 LV 是 2017 年的。在那里,可以使用System Exec.vi在命令行上运行任何命令,并将其 output 读回 LV:

在此处输入图像描述

Finally, what Python prints, will be returned as string.最后,Python 打印的内容将作为字符串返回。 I'd work with JSON, which easily converts a dict to JSON:我会使用 JSON,它可以轻松地将字典转换为 JSON:

import json

a=[]
a.append({'name':'CH_PH1', 'virtual':False, 'RollingAvgFrame':3.141})
a.append({'name':'CH_PH2', 'virtual':True, 'RollingAvgFrame':42.0})
print(json.dumps(a))

Output: Output:

[{"name": "CH_PH1", "virtual": false, "RollingAvgFrame": 3.141}, 
{"name": "CH_PH2", "virtual": true, "RollingAvgFrame": 42.0}]

In LV, define a cluster according to the JSON structure, and use the Unflatten from JSON VI to fill it.在LV中,根据JSON结构体定义一个cluster,使用Unflatten from JSON VI进行填充。 The names of the cluster elements must match those in the JSON code.簇元素的名称必须与 JSON 代码中的名称匹配。

在此处输入图像描述

However, JSON knows only simple data types.然而,JSON 只知道简单的数据类型。 There is no way to transfer time or any other more complex datatype, unless it can be packed as something more simple like a string containing a timestamp.无法传输时间或任何其他更复杂的数据类型,除非可以将其打包为更简单的内容,例如包含时间戳的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM