Jump to content

Streaming Commands to a Python Console


NickB34

Recommended Posts

I've posted this in the NI forums, but I thought I might get some more traction here...

 

Does anybody have any ideas on a way to stream commands to a python console?

 

A little background on the issue: I am able to control instruments in a system through a .dll that I call using python. I have already implemented a VI that can create a script "on the fly" and execute it to perform simple commands. The problem is that this creates a new instance of python every time I execute a command. It would be very advantageous for me to keep python running and only stop execution when I'm done sending commands.

 

I have tried accessing python directly from the python .dll through LabVIEW, but this got ugly very quick and I'm not a software engineer  :D

I have also tried using labpython, but it doesn't seem to be compatible with python 3.3 which is a requirement in the system.

 

 

Please let me know if you have any suggestions on what path I should head down, Thanks.

Link to comment

If you can handle some Python programming...

 

The simplest way I've found to do this is to build a socket listener in Python.  It opens a TCP port, and waits for commands to come in.  You can see the basic idea at http://docs.python.org/3.3/howto/sockets.html.

 

On the LV side, you send commands to the Python socket using the TCP functions (http://www.ni.com/white-paper/2710/en/).

 

When the Python side receives a command, it breaks it down, and routes it to the right dll call.  When the dll call finishes, Python might send a message back to LV with results, if needed.

 

There are always a lot of little details to handle, but if you get the basics of this down, it works very well.

  • Like 1
Link to comment

I use pipes myself to pump commands to stdin and read from stdout of the python process.  You have to do the string parsing if you want to make sense of the python output in LV, but it is simple to pass commands as if you were typing away at the keyboard.

 

I am mobile right now, but you should be able to dig up the old OpenG pipes library.  If you are on a linux box then LV has the pipe library built in.

  • Like 1
Link to comment
I use pipes myself to pump commands to stdin and read from stdout of the python process.  You have to do the string parsing if you want to make sense of the python output in LV, but it is simple to pass commands as if you were typing away at the keyboard.

 

I am mobile right now, but you should be able to dig up the old OpenG pipes library.  If you are on a linux box then LV has the pipe library built in.

Darin, please elaborate if you can.... I would love to see an example of this. I'm not on a Linux box, using windows here.

Link to comment

Todd, thanks for the links.

 

I was able to install using VIPM, but for some reason the palettes didn't show up. I've never used pipes before so I will have to try and figure out how the VI's work.

 

Yes, I did cross post this. I will update both posts if I am able to come up with a working solution.

Link to comment

I've been able to download the pipes package and have been able to execute the test VI's that are included. Whenever I send the py directive to a command console, I can see it change to python mode. Any command that I send after that causes an error. I see the same behavior when I open python.exe using pipes (instead of cmd.exe py). Any ideas?

System Command (cmd.exe) Test.vi

Edited by NickB34
Link to comment

I think I may travel down the python socket server road with this one. It is probably the best way to implement what I need since I won't have to install any NI stuff on the server side. If only I knew more about programming in python... I'll update if I ever come up with a solution.

Edited by NickB34
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Similar Content

    • By TDF
      TDF team is proud to propose for free download the scikit-learn library adapted for LabVIEW in open source.
      LabVIEW developer can now use our library for free as simple and efficient tools for predictive data analysis, accessible to everybody, and reusable in various contexts.
      It features various classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to interoperate with the Python numerical and scientific libraries NumPy and SciPy from the famous scikit-learn Python library. 
       
      Coming soon, our team is working on the « HAIBAL Project », deep learning library written in native LabVIEW, full compatible CUDA and NI FPGA.
      But why deprive ourselves of the power of ALL the FPGA boards ? No reason, that's why we are working on our own compilator to make HAIBAL full compatible with all Xilinx and Intel Altera FPGA boards.
      HAIBAL will propose more than 100 different layers, 22 initialisators, 15 activation type, 7 optimizors, 17 looses.
       
      As we like AI Facebook and Google products, we will of course make HAIBAL natively full compatible with PyTorch and Keras.
       
      Sources are available now on our GitHub for free : https://www.technologies-france.com/?page_id=487
    • By kpaladiya
      I would like to build a model using image data and NI-cRIO-9063 and NI 9264 for voltage control.
      for image, I made a script in python using OpenCV libraries that detecting some points . For voltage control, I use cRIO-9063 with NI 9264 voltage controller.
      My question is, I am new in LabVIEW and I don't have any idea how can I make a loop for voltage control in python. Is there any library available in python that directly connect cRIO and NI 9264 devices? if not then how can I combine my image data(which is in python) with cRIO device? I need argent help.
    • By drjdpowell
      I am just starting on trying to be able to use Python code from a LabVIEW application (mostly for some image analysis stuff).  This is for a large project where some programmers are more comfortable developing in Python than LabVIEW.  I have not done any Python before, and their seem to be a bewildering array of options; many IDE's, Libraries, and Python-LabVIEW connectors.  
      So I was wondering if people who have been using Python with LabVIEW can give their experiences and describe what set of technologies they use.
    • By David Akinpelu
      I am implementing a TCP connection between MyRio(client) and Python(server). The goal is to send data from the server to the client, perform some calculations, and send the result to the server. I need to keep changing the data being sent to the client. I noticed the first data works fine and the result returned to the server, but the client could not read subsequent data. I debugged and was able to detect that the error is coming from the first read function is the "Reading values subvi"  but I don't know how to correct it. How do I go about it?
      I have attached the files below.
      Reading Unknown Bytes from TCP.vi Second_client.vi SimpleServer.py
    • By Diegodemf
      Hello, I'm trying to send data from my RPI b+ to labview and store the information in a spreadsheet, right now the data I'm using is just a sawtooth generated in the python code but eventually it will be sensor data adquired via spi, the data comes at =~ 6-7khz but the time it takes to send the data to labview is much greater, I fear the circular buffer I intended to use to store the data before sending it via tcp will get filled pretty fast. I'm not very experienced in either python nor Labview, I was wondering if i could get some advice in the optimization of the code.

      I attached the VI of the client and a VI that graphs the data on the spreadsheet, also the python code.
      Thanks in advance, sorry for the mess.
      Waveform Grapher.vi
      Client.vi
      serverSerial.py
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.