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.

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.

  1. import os
  2. import subprocess
  3. import socket
  4.  
  5. class FlexSimConnection():
  6.  
  7.     def __init__(self, flexsimPath, modelPath, address='localhost', port=5005, verbose=False, visible=False):
  8.         self.flexsimPath = flexsimPath
  9.         self.modelPath = modelPath
  10.         self.address = address
  11.         self.port = port
  12.         self.verbose = verbose
  13.         self.visible = visible
  14.  
  15.         self._launch_flexsim()
  16.         
  17.     def _launch_flexsim(self):
  18.         if self.verbose:
  19.             print("Launching " + self.flexsimPath + " " + self.modelPath)
  20.  
  21.         args = [self.flexsimPath, self.modelPath]
  22.         if self.visible == False:
  23.             args.append("-maintenance")
  24.             args.append("nogui")
  25.         self.flexsimProcess = subprocess.Popen(args)
  26.  
  27.         self._socket_init(self.address, self.port)
  28.     
  29.     def _close_flexsim(self):
  30.         self.flexsimProcess.kill()
  31.  
  32.  
  33.     def _socket_init(self, host, port):
  34.         if self.verbose:
  35.             print("Waiting for FlexSim to connect to socket on " + self.address + ":" + str(self.port))
  36.  
  37.         self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38.         self.serversocket.bind((host, port))
  39.         self.serversocket.listen();
  40.  
  41.         (self.clientsocket, self.socketaddress) = self.serversocket.accept()
  42.         if self.verbose:
  43.             print("Socket connected")
  44.         
  45.         if self.verbose:
  46.             print("Waiting for READY message")
  47.         message = self._socket_recv()
  48.         if self.verbose:
  49.             print(message.decode('utf-8'))
  50.         if message != b"READY":
  51.             raise RuntimeError("Did not receive READY! message")
  52.  
  53.     def _socket_send(self, msg):
  54.         totalsent = 0
  55.         while totalsent < len(msg):
  56.             sent = self.clientsocket.send(msg[totalsent:])
  57.             if sent == 0:
  58.                 raise RuntimeError("Socket connection broken")
  59.             totalsent = totalsent + sent
  60.  
  61.     def _socket_recv(self):
  62.         chunks = []
  63.         while 1:
  64.             chunk = self.clientsocket.recv(2048)
  65.             if chunk == b'':
  66.                 raise RuntimeError("Socket connection broken")
  67.             if chunk[-1] == ord('!'):
  68.                 chunks.append(chunk[:-1])
  69.                 break;
  70.             else:
  71.                 chunks.append(chunk)
  72.         return b''.join(chunks)
  73.  
  74. def main():
  75.     con = FlexSimConnection(
  76.         flexsimPath = "C:/Program Files/FlexSim 2021 Update 2/program/flexsim.exe",
  77.         modelPath = "C:/Users/MyUser/Documents/FlexSim 2021 Projects/my_model.fsm",
  78.         verbose = True,
  79.         visible = True
  80.         )
  81.  
  82. if __name__ == "__main__":
  83.     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.