Jump to content
NickB34

Streaming Commands to a Python Console

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Thanks for the response. I'm not a python programmer, but I may be able to get some help with this. I'll ask around and see if somebody here has some experience with this.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I use my own pipes library, it should be equivalent to the OpenG version, but I will install it and double check that it works. I have no problem using scipy and numpy all the time.

Share this post


Link to post
Share on other sites

Here is a picture of what happens when I send a string after I send the py directive. It gives me an error 56 on the pipe read, seems like a pretty generic error code.

post-46289-0-00127800-1384474802_thumb.p

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.


×
×
  • Create New...

Important Information

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