![Capture.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206099/Capture.png)
Simulink-Python Simultaneous Send-Receive using TCP-IP
55 次查看(过去 30 天)
显示 更早的评论
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 = '127.0.0.1'
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))
s.listen(1)
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
conn.close()
#Send to Simulink
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost',30001))
serversocket.listen(5)
(clientsocket, address) = serversocket.accept()
for i in range(50):
msg1 = struct.pack('>d', 1.1+i)
delay(0.2)
clientsocket.send(msg1)
clientsocket.close()
0 个评论
回答(1 个)
Thomas Hafkamp
2018-10-4
编辑:Thomas Hafkamp
2019-2-27
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:
![Capture.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206099/Capture.png)
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
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
print('Waiting for Simulink to start')
s.listen(1)
conn, addr = s.accept()
print('Connection address: ', addr)
for i in range(50):
# send data to Simulink
msg1 = struct.pack('>d', i)
conn.send(msg1)
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)
conn.close()
1 个评论
Soumil Chugh
2019-3-10
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Development Computer Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!