Simulink-Python Simultaneous Send-Receive using TCP-IP

54 views (last 30 days)
I wanna send/receive data from Python (inside Visual Components software) to Simulink using TCP-IP. I have two parts of code, one for sending and other for receiving.
When I use only send part or receive part, it works! But when I try to use them both, sending and receiving at the same time, I get error in Simulink like connection refused etc and upon re-running, the Simulink displays the received data. Also I don't receive the data in Python when using both at the same time.
So how to solve it and send/receive data at the same time?
Screenshot of Simulink is attached. Python code is below.
from vcScript import *
import socket
import struct
def OnRun():
#Server TCP Communication
TCP_IP = ''
TCP_PORT = 5002
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
conn, addr = s.accept()
print 'Connection address:', addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print "received data:", data
conn.send(data) # echo
#Send to Simulink
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
(clientsocket, address) = serversocket.accept()
for i in range(50):
msg1 = struct.pack('>d', 1.1+i)

Answers (1)

Thomas Hafkamp
Thomas Hafkamp on 4 Oct 2018
Edited: Thomas Hafkamp on 27 Feb 2019
Your code probably doesn't work because you didn't specify the Block priorities correctly.
According to this Simulink tutorial, for some reason Simulink needs to send a TCP/IP message first before receiving a TCP/IP message. To do so, you need to set the priority of the TCP/IP Send block to 1 and of the TCP/IP Receive block to 2:
"Step 8: Specify the Block Priorities To run the simulation correctly, specify the order in which Simulink should process the blocks. Right-click the block and select Properties. Enter the priority number in the Priority field. In this case, set the priority of TCP/IP Send to 1 and TCP/IP Receive to 2."
In the tutorial, a MATLAB tcpip echo server is used. However, when using Python as the echo server with the code below, I found out that the order should be reversed. So first Simulink needs to receive, and then Simulink needs to send.
Simulink scheme:
This Simulink scheme runs for 4.9 [s] with a fundamental sample time of 0.1 [s]. A linear ramp with slope 10 [1/s] is sampled. This way, the values 0 to 49 are sent to Python.
Python code:
import socket, struct
TCP_IP = 'localhost'
TCP_PORT = 30001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
print('Waiting for Simulink to start')
conn, addr = s.accept()
print('Connection address: ', addr)
for i in range(50):
# send data to Simulink
msg1 = struct.pack('>d', i)
print('sent data:', i)
# receive data from Simulink
data = conn.recv(BUFFER_SIZE)
converteddata = int.from_bytes(data, byteorder='big', signed=False)
print('received data:', converteddata)
  1 Comment
Soumil Chugh
Soumil Chugh on 10 Mar 2019
I am trying to run a simulation model in MATLAB which takes input via TCP/IP from python code and sends back some data to the python code. I am unable to sync both the times. My TCP/IP is sending data with a different time interval(0.02) while my simulation model runs with a different interval(0.08). I tried to fix the step size in simulation model configuration but nothing helps. Can anybody explain the same?

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!