开发者

send data from LabView to Python and get back

How do I sen开发者_开发技巧d data from LabView to Python and get a result back?


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

send data from LabView to Python and get back

+
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" )


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.


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


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.


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


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!


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜