将数据从 LabView 发送到 Python 并返回

发布于 2024-11-18 20:35:56 字数 39 浏览 1 评论 0原文

如何将数据从 LabView 发送到 Python 并返回结果?

How do I send data from LabView to Python and get a result back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

少女七分熟 2024-11-25 20:35:56

另一种解决方案是使用智能消息传递库 ZeroMQ,它附带了很多绑定,几乎适用于所有主要语言。

对于 Python/Labview 案例,sourceforge 上有一个很好的演示项目:

Python-LabVIEW Communication

客户端 ~LabVIEW
输入图片此处描述
+
服务器端部分(示例)

#-----------------------------------------# INFRASTRUCTURE for communication
context = zmq.Context()                   # I/O-DAEMON CONTEXT
socket  = context.socket(zmq.REP)         # ARCHETYPE for a Smart Messaging 
socket.bind( "tcp://127.0.0.1:5555" )     # PORT ready for LabView to .connect()
#-----------------------------------------# TRANSPORT-CLASS-es {ipc|tcp|+..}

while True:                               # LOOP & WAIT FOR REQ-calls
    #                                     # Wait for request from client
    message = socket.recv()
    print("Received request: %s" % message )

    try:
        r = eval( message )
        print(r )

        socket.send(bytearray(str( r ),
                             'utf-8' ))    # send returned value as bytearry to client
    except NameError:
        socket.send( b"Unknown command" )

    except:
        socket.send( b"Unknown error" )

One other solution is using the smart messaging library ZeroMQ, which comes with a lot of bindings, almost for all major languages.

For the Python/Labview case there is a nice demo project on sourceforge:

Python-LabVIEW Communication

Client-side ~LabVIEW
enter image description here
+
Server-side part (example)

#-----------------------------------------# INFRASTRUCTURE for communication
context = zmq.Context()                   # I/O-DAEMON CONTEXT
socket  = context.socket(zmq.REP)         # ARCHETYPE for a Smart Messaging 
socket.bind( "tcp://127.0.0.1:5555" )     # PORT ready for LabView to .connect()
#-----------------------------------------# TRANSPORT-CLASS-es {ipc|tcp|+..}

while True:                               # LOOP & WAIT FOR REQ-calls
    #                                     # Wait for request from client
    message = socket.recv()
    print("Received request: %s" % message )

    try:
        r = eval( message )
        print(r )

        socket.send(bytearray(str( r ),
                             'utf-8' ))    # send returned value as bytearry to client
    except NameError:
        socket.send( b"Unknown command" )

    except:
        socket.send( b"Unknown error" )
梦巷 2024-11-25 20:35:56

LabView 允许您用多种语言编写扩展,目前的主要技术是使用网络连接。避免在 Labview 进程本身内部运行本机语言工具包。

这里似乎有一个Python Labview 工具包,但它不再起作用了。

使用套接字服务器和套接字客户端在 Labview 和 python 之间进行通信。 (大多数跨平台解决方案,现在您不必让 python 和 labview 在同一台 PC 上或同一进程中运行)。

不幸的是,目前我无法提供示例,因为我没有安装labview,但我已经完成了从LabView到两三种语言的数十个应用程序的基于ole自动化的集成,但那是很多年前的事了。这些天我会使用网络套接字技术。

LabView allows you to write extensions in several languages, the primary technique these days is to use a network connection. Native language toolkits that run inside the labview process itself are avoided.

It seems there is a Python Labview toolkit here but it no longer works.

Use a socket server and socket client to talk between Labview and python. (Most cross-platform solution, and now you don't have to have your python and labview running on the same PC, or in the same process).

Unfortunately a sample is beyond me at the moment as I don't have labview installed, but I have done ole automation based integrations from LabView to dozens of apps in two or three languages, but that was many years ago. These days I would use the network socket technique.

季末如歌 2024-11-25 20:35:56

LabVIEW 2018 现在提供了一个“本机”解决方案,可以通过来回“发送数据”来从 LabVIEW 调用 Python 代码:

连接选项板包括新的 Python 子选项板,您可以使用它从 LabVIEW 代码调用 Python 代码。 Python 调色板包括以下功能:

  • 打开 Python 会话 - 使用特定版本的 Python 打开 Python 会话。
  • Python 节点 - 直接调用 Python 函数。
  • 关闭 Python 会话 — 关闭 Python 会话。

    注意您必须安装Python 2.7或3.6才能使用LabVIEW Python函数。
    尽管不受支持的版本可能适用于 LabVIEW Python 函数,但 NI 建议仅使用受支持的 Python 版本。

参考:LabVIEW 2018 功能和变更

Python 节点提供了模块路径、函数名称、输入参数和返回值的预期数据类型。这些输入和输出支持许多基本数据类型:数字、(多维)数组、字符串、簇;自动转换为相应的数据类型,即LabVIEW数组转换为Python列表,簇转换为元组(返回值反之亦然)。

LabVIEW 2018 now offers a "native" solution to calling Python code from LabVIEW with "sending data" back and forth:

The Connectivity palette includes the new Python subpalette, which you can use to call Python code from LabVIEW code. The Python palette includes the following functions:

  • Open Python Session — Opens a Python session with a specific version of Python.
  • Python Node — Calls a Python function directly.
  • Close Python Session — Closes a Python session.

    Note You must install Python 2.7 or 3.6 to use the LabVIEW Python functions.
    Although unsupported versions might work with the LabVIEW Python functions, NI recommends using supported versions of Python only.

Reference: LabVIEW 2018 Features and Changes

The Python Node is provided with the module path, function name, input parameters, and the expected data type of the return value. These inputs and output support a number of basic data types: numerics, (multi-dimensional) arrays, strings, clusters; with automatic conversion to corresponding data types, i.e. LabVIEW arrays are converted to Python lists, and clusters to tuples (and vice versa for the return value).

┊风居住的梦幻卍 2024-11-25 20:35:56

我使用 stdio 与 Python 进程进行通信有一段时间了,最​​近注意到 Python for .Net ( http://pythonnet.github. io/ )这对我有用。

只需将 .dll 复制到 LabVIEW 项目中,创建 .Net 构造函数,然后使用 LabVIEW .Net,您就可以快速了解如何使用该库,它基本上为您提供了非托管 Python 共享库,并封装了 .Net 的优点。

I was using stdio communication with a Python process for a while and recently noticed Python for .Net ( http://pythonnet.github.io/ ) which works for me.

Just copy the .dll in your LabVIEW project, create .Net constructors, and using the LabVIEW .Net you can quickly figure out how to use this library, which basically provides you with the unmanaged Python shared library, wrapped with .Net goodness.

鹿! 2024-11-25 20:35:56

使用 Python 的服务器端

import socket
server = socket.socket(2,1)
server.bind(('localhost',2000))
server.listen(1)

while True :
    (conn,addr) = server.accept()
    command = conn.recv(4)
    print (command)

    if 'INIT' in str(command):
    conn.sendall(b'INIT-DONE')
    elif 'PLAY' in str(command):
    conn.sendall(b'PLAY-DONE')
    elif 'QUIT' in str(command):
    conn.sendall(b'QUIT-DONE')
    break

server.close()

Labview 客户端

Server side with Python

import socket
server = socket.socket(2,1)
server.bind(('localhost',2000))
server.listen(1)

while True :
    (conn,addr) = server.accept()
    command = conn.recv(4)
    print (command)

    if 'INIT' in str(command):
    conn.sendall(b'INIT-DONE')
    elif 'PLAY' in str(command):
    conn.sendall(b'PLAY-DONE')
    elif 'QUIT' in str(command):
    conn.sendall(b'QUIT-DONE')
    break

server.close()

Labview Client Side

咿呀咿呀哟 2024-11-25 20:35:56

有一个新的 Python/LabVIEW 连接器,主要由您真正构建,称为 TestScript 。它是一个免费的、源代码发布的完全双向的 Python/LabVIEW 连接器。您可以在Python脚本中控制LabVIEW,也可以从LabVIEW调用Python脚本。它附带了几个示例,说明如何将数据从 LabVIEW 发送到 Python 并返回结果。特别是,简单脚本示例 - Add on Python Side.vi 展示了 TestScript 如何完成您的需要。

享受!

There is a new Python/LabVIEW connector out, built mostly by yours truly, called TestScript. It's a free, source-released Python/LabVIEW connector that is fully bidirectional. You can control LabVIEW from within a Python script, and you can call Python scripts from LabVIEW. It ships with several examples illustrating how you can send data from LabVIEW to Python and get a result back. In particular, the Simple Scripting Example - Add on Python Side.vi shows how TestScript accomplishes what you need.

Enjoy!

彻夜缠绵 2024-11-25 20:35:56

Python-LabVIEW-Interface (PyLVi) 是一个基于 ZeroMQ 库调用 Python 函数并将结果读回 LabVIEW。

它支持类方法和属性的调用,并且可以处理结构化数据类型(例如Python字典映射到LabVIEW集群)以及numpy数组。

Python-LabVIEW-Interface (PyLVi) is an open-source project based on the ZeroMQ library to call Python functions and read the results back to LabVIEW.

It supports call of class methods and properties, and can handle structured datatypes (e.g. Python dictionaries are mapped to LabVIEW clusters) as well as numpy arrays.

不再让梦枯萎 2024-11-25 20:35:56

您可以尝试以下操作: https:// /forums.ni.com/t5/LabVIEW-APIs-Documents/Open-Python-Interface/ta-p/3612981

它是 LabVIEW 的开放式 Python 接口工具包。

You can try this: https://forums.ni.com/t5/LabVIEW-APIs-Documents/Open-Python-Interface/ta-p/3612981

Its an Open Python Interface toolkit for LabVIEW.

方觉久 2024-11-25 20:35:56

这是一个很好的快速解决方案,但有点不优雅。只需使用 labview 和 python 都可以读取/写入的文本文件。 Labview 将命令写入文本文件,而 python 等待读取命令,反之亦然。您可以同时拥有一个读取文本文件和一个写入文本文件。然而,这是一个较慢的解决方案,因为读取和写入需要时间。

Here is a good quick solution but kind of unelegant. Just use a text file that both labview and python can read/write from. Labview writes commands to text file and python waits until a command is read or vice versa. you can have a read text file and a write text file at the same time. However this is a slower solution as it takes time to read and write.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文