0

I'm trying to get a tow-way communication between a c# application and a python script that c# will call.

I have some input channels in c# that changes constantly at high frequency (5000-1000 data/s) for let's say a minute. On every change of those inputs,results are calculated and assigned to output variables. What i'm trying to do is to move the logic to a python script. For instance:

  • Inputs: double x,y
  • Output: double z

So the pyhton script should be capable of read the inputs, perform the logic and write the results at a symilar frequency.

Any recomendations? Has anyone did anything similar before?

First I tried to call the script on every change and read the console output. But the code in the script is not as simple as z=x*y and variables that store values are required in the pyhon script. For example, the script mught want to save the maximum value of x and y reached.

I had a look to ZeroMQ library for the communication, not sure how to use it though.

  • Please remember to accept and vote up the answer if your original issue has been solved and then ask a new question if you have another issue: https://stackoverflow.com/help/someone-answers – Frenchy Apr 28 '20 at 14:58

1 Answers1

1

Here is a solution:

Simple C# program: client which sends data and receive

using System;
using ZeroMQ;

namespace ZeroMQ_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var requester = new ZSocket(ZSocketType.REQ))
            {
                // Connect
                requester.Connect("tcp://127.0.0.1:5555");

                for (int n = 0; n < 10; ++n)
                {
                    string requestText = "Hello";
                    Console.Write("Sending {0}...", requestText);

                    // Send
                    requester.Send(new ZFrame(requestText));

                    // Receive
                    using (ZFrame reply = requester.ReceiveFrame())
                    {
                        Console.WriteLine(" Received: {0} {1}!", requestText, reply.ReadString());
                    }
                }
            }
        }
    }
}

python program, you have to install pyzmq:

#
#   Hello World server in Python
#   Binds REP socket to tcp://*:5555
#   Expects b"Hello" from client, replies with b"World"
#

import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s" % message)

    #  Do some 'work'
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World")
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • Thanks for the answer, that's a nice example. But im looking for something more asynchronous, so c# does not have to request the an answer. Maybe if i add another socket like you did on python it willsend requests to c# with the results data will work. – Diego Delgado Apr 26 '19 at 08:09
  • asynchronous process is just the way to program . after to communicate between c# and python, you have more solutions, sockets, named pipe. .... – Frenchy Apr 26 '19 at 08:20