This repository demonstrates the method I used in my work to build a TCP/IP communication channel between a Simulink model and Python. UDP will be similar.
Training in reinforcement learning requires running thousands of simulations, requiring as fast speeds as possible. TCP/IP is an efficient and reliable way to establish communication between Simulink and Python.
The Simulink model is compiled into a standalone executable, although this is not a necessary step if speed is not a concern. The communication layer is a TCP/IP interface containing the Simulink client and the Python server. This channel uses socket communication to handle low-level data transfer between the two processes and uses file I/O to pass parameters into the model. If communication accuracy is not critical, then UDP will work as well.
This example uses a very simple setup of a Python server that echos a clock input from a Simulink client. We use an S-Function that calls C code to perform the data transfer. A more detailed step-by-step guide is here.
This repository includes:
simulink.slxis a bare-bones Simulink model that callsclient.mexw64.client.cis compiled intoclient.mexw64and acts as the client.server.pyacts as the server.port.txtcontains the port number to demonstrate passing parameters into Simulink using file I/O.
- Install python3 and MATLAB/Simulink. The Python standard library is sufficient.
- If required, compile
simulink.slxinto a standalone executable using i.e. Simulink Coder. - First run the server side (
server.py). - Then, run the client side (
simulink.slxor its executable).
You should see the clock output being printed.

