question

J avatar image
0 Likes"
J asked Phil BoBo edited

C# or C++ or Python

Hello guys, below is my question.

I want to create an user interface to control FlexSim. Like pushing some button to add a task executer or dragging something to change the speed of task executer. Can I using c++, c# or python to construct an interface and join with FlexSim? Thank you.

FlexSim 21.2.0
pythonc++cshop
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Arun Kr avatar image Arun Kr commented ·
2 Likes 2 ·
J avatar image J Arun Kr commented ·
I know that. But it is not convenient for us. We want to use python or c++ to do the graphical user interface. But I'm not sure if python or c++ can work with FlexSim or not. Thank you!
0 Likes 0 ·
Phil BoBo avatar image
2 Likes"
Phil BoBo answered Phil BoBo edited

I recently wrote a Python script that used the FlexScript socket commands to control a model, and they worked really well. So you can use C++ with the DLL Maker or Module SDK if you want full control, but the FlexScript socket commands are quite effective for communicating between another language and FlexSim.

See the pdf in: how to use flexsim socket communication with server and client - FlexSim Community

Below are some snippets of my python code with socket communication that might help.

Most languages have functions for socket communication. You could use C# or something else if you wanted to.

I expect that trying to implement inter-process communication is not going to be more "convenient" for you to build UIs than using FlexSim's GUI Builder or Dashboard widgets, but you are welcome to try.

import os
import subprocess
import socket

class FlexSimConnection():

    def __init__(self, flexsimPath, modelPath, address='localhost', port=5005, verbose=False, visible=False):
        self.flexsimPath = flexsimPath
        self.modelPath = modelPath
        self.address = address
        self.port = port
        self.verbose = verbose
        self.visible = visible

        self._launch_flexsim()
        
    def _launch_flexsim(self):
        if self.verbose:
            print("Launching " + self.flexsimPath + " " + self.modelPath)

        args = [self.flexsimPath, self.modelPath]
        if self.visible == False:
            args.append("-maintenance")
            args.append("nogui")
        self.flexsimProcess = subprocess.Popen(args)

        self._socket_init(self.address, self.port)
    
    def _close_flexsim(self):
        self.flexsimProcess.kill()


    def _socket_init(self, host, port):
        if self.verbose:
            print("Waiting for FlexSim to connect to socket on " + self.address + ":" + str(self.port))

        self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.serversocket.bind((host, port))
        self.serversocket.listen();

        (self.clientsocket, self.socketaddress) = self.serversocket.accept()
        if self.verbose:
            print("Socket connected")
        
        if self.verbose:
            print("Waiting for READY message")
        message = self._socket_recv()
        if self.verbose:
            print(message.decode('utf-8'))
        if message != b"READY":
            raise RuntimeError("Did not receive READY! message")

    def _socket_send(self, msg):
        totalsent = 0
        while totalsent < len(msg):
            sent = self.clientsocket.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError("Socket connection broken")
            totalsent = totalsent + sent

    def _socket_recv(self):
        chunks = []
        while 1:
            chunk = self.clientsocket.recv(2048)
            if chunk == b'':
                raise RuntimeError("Socket connection broken")
            if chunk[-1] == ord('!'):
                chunks.append(chunk[:-1])
                break;
            else:
                chunks.append(chunk)
        return b''.join(chunks)

def main():
    con = FlexSimConnection(
        flexsimPath = "C:/Program Files/FlexSim 2021 Update 2/program/flexsim.exe",
        modelPath = "C:/Users/MyUser/Documents/FlexSim 2021 Projects/my_model.fsm",
        verbose = True,
        visible = True
        )

if __name__ == "__main__":
    main()
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Mischa Spelt avatar image
1 Like"
Mischa Spelt answered

I would use TCP/IP with a custom protocol. To a certain extent you can do this within FlexSim proper, but for maximum flexibility I'd write a C++ module that controls sockets directly.

Then you can write the UI program in whatever language you are most comfortable with and talk to FlexSim through the module.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.