2

I have a GUI in VB.Net that needs to interface with algorithms written in Python, all to run on Windows 10 Professional. The IDE is Visual Studio 2017.

How can the VB.Net GUI and Python communicate? Data needs to be communicated back and forth. The GUI will accept user commands, and then command Python to execute. Python will have events that it needs to communicate to the GUI.

To clarify the architecture, the GUI app and the Python app are two different programs running simultaneously.

The GUI has two major functions: 1) take user commands and tell Python to execute them. 2) Monitor/poll the Python app for data or events, and update (tell) the user via the GUI controls (textboxes, etc.).

Python mainly does two things: 1) runs the continuous control algorithm, subject to management by the GUI. 2) Reports data and events to the GUI.

A TCP socket is 100% reliable, but I have to write and debug a proprietary messaging scheme, which I've done a lot and it's fun, but I thought maybe I can accomplish same thing in substantially less time by some other equally reliable method... such as COM, as those below suggest.

Example scenario of asynchronous usage:

1:

The Python app is running a robotic process. Meanwhile, user presses GUI command to see latest metrics: GUI's VB.NET calls a COM function within Python which returns an array of metrics data.

2:

Python app running robotic process completes a step, and now needs a manual action. Python app calls COM function within VB.NET and passes an argument with requirements of manual action.

Community
  • 1
  • 1
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • might not be the best solutions as i'm not that familiar with python but couldnt you make a small local server on python or vb and send data through tcp. Or even worse you could use a file. or even local database with like commands to execute and executed and results. – Mederic Aug 18 '17 at 16:03
  • What I would do is wrap a Python COM server around the Python code and use COM to call the Python functions from VB. That may not be the easiest way to do it, but I would do it that way because I've done it before. Comprehensive cookbook in Hammond & Robinson: *Python Programming on Win32*, O'Reilly. – BoarGules Aug 18 '17 at 17:23
  • Mederic: Yes, I'm considering a TCP socket connect, also a good old text file method, but I'm intrigued by Microsoft's COM. Whichever is fastest and reliable. – Doug Null Aug 18 '17 at 20:41
  • BoarGules: I was hoping someone would give a thumbs-up to COM. – Doug Null Aug 18 '17 at 20:41

2 Answers2

4

MSDN Python Script Integration

Here is what I would suggest you can read this it's a guide on the MSDN about communicating from C# to Pyhton

Basically calling a python script and reading output.

https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee


IronPython

IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily.

http://ironpython.net/


Python for .NET

Python for .NET (pythonnet) is a package that gives Python programmers nearly seamless integration with the .NET 4.0+ Common Language Runtime (CLR) on Windows and Mono runtime on Linux and OSX.

However this one limits you to a certain version of python.

http://pythonnet.github.io/


Other solutions

  • Using socket with client (python) server (c#) or vice versa.
  • Using a local database or database file like mdf.
Mederic
  • 1,949
  • 4
  • 19
  • 36
  • So, do any of these solutions support asynchronous communication/calls between to simultaneously running apps (the VB.NET GUI and the Python control app)? – Doug Null Aug 18 '17 at 20:51
  • @DougNull define a simple asynchronous scenario please – Mederic Aug 18 '17 at 23:11
1

I found answer here. This guy shows consisely how VB.NET calls IronPython scripts functions and also passes and retrieves VB data. Sweet.

I believe the value here of IronPython vs. regular Python is that IronPython is compatible with any type of .NET data that VB passes to it.

Now, I can create a GUI with the supreme quality and reliability of VB.NET that uses the supreme development efficiency/productivity of Python.

halfer
  • 19,824
  • 17
  • 99
  • 186
Doug Null
  • 7,989
  • 15
  • 69
  • 148