LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble running a VI asynchronously when called from Python via COM/ActiveX interface

For literally decades, on Windows platforms, I've been controlling LabVIEW vi's with Perl scripts using the COM/ActiveX interface. Now I am trying to control LabVIEW vi's from Python scripts, but I am having trouble running vi's asynchronously.

 

Below is a simple script which opens a vi, sets a control value, and reads a couple of indicators as well, and runs a simple vi, which has a while loop controlled with a boolean Stop button.


import sys
import win32com.client
lvRef = win32com.client.Dispatch("LabVIEW.Application")
viName = 'C:\\perforce\\PythonDev\\software\\LabVIEW\\pythonTest.vi'
viRef = lvRef.GetVIReference(viName)
viRef.SetControlValue('U16 Control', sys.argv[1])
value = viRef.GetControlValue('U32 Indicator')
print(value)
array = viRef.GetControlValue('U16 Array Indicator')
print(array)
viRef.Run
print("VI done")

 

By default, the vi will run synchronously. That is, the Python call, viRef.Run, will block until the vi execution is complete, via the boolean control to the while loop. This is all working as expected.

 

But I want to run the vi asynchronously. That is, I don't want the Python script to block after calling viRef.Run. I want the vi to keep running, and let the Python script continue execution.

 

The Run method actually takes an optional parameter, "async", which when set to TRUE, the vi should run asynchronously, and the Python script should continue executing, but it sill blocks. In fact, if I pass any parameter to Run, such as viRef.Run(0) or viRef.Run(1), the vi will still run synchronously, but now when I stop the vi, I get the following error:


C:\perforce\PythonDev\software\Python>LabviewTest.py 55
16
(0, 1, 2, 3)
Traceback (most recent call last):
File "C:\perforce\PythonDev\software\Python\LabviewTest.py", line 11, in <module>
viRef.Run(0)
TypeError: 'NoneType' object is not callable

 

I'm a Python newbie and don't really understand this error message in this context ...
For what it's worth, I had no problem with this when using Perl. I could pass a 1 to the Run method and it would run asynchronously, that is, the Perl script would continue executing, as expected.

 

Any ideas on what the issue is? I'm running LabVIEW 2017, 32 bit version. I've tried both 32 bit and 64 bit versions of Python 3.9.


In this NI forum, I did find a simple example of using Python to control a vi, (https://forums.ni.com/t5/Example-Code/Controlling-a-VI-using-Python-using-LabVIEW/ta-p/3536468?profi...) where the Call() method was used to run the vi, but that wouldn't run asynchronously either. (I did find a kludge using Popen to spawn another "shell-less" Python instance which runs the vi, but I would much rather figure out how to use the Run method)

 

Thanks,
Greg Mathis

0 Kudos
Message 1 of 4
(1,334 Views)

What if, instead of running the target VI directly, you just ran a "launcher" VI that does nothing but load a different VI by reference, start it running asynchronously inside of LabVIEW, and then exits?

 

https://zone.ni.com/reference/en-XX/help/371361R-01/lvconcepts/asynchronous_vi_calls/

 

 

0 Kudos
Message 2 of 4
(1,316 Views)

That is a good idea. I quickly whipped up a "launcher" VI just like you suggested and it does provide a simple solution, much better than spawning another Python instance to launch the VI. I would still like to figure out why passing the "async" parameter to the vi.Run method doesn't work with Python, but does work with Perl.  But in the meantime, I'm going with your suggestion.

 

Thanks!

Greg

0 Kudos
Message 3 of 4
(1,307 Views)

@gpmathis, did you ever get a solution to this problem, other than creating a launcher vi? I've come across the same problem as you described using LV 2023 Q3 with Python 3.x

0 Kudos
Message 4 of 4
(348 Views)