Reputation: 11
I am trying to build a very basic prototype for communication between python and simulink, but I can't seem to be able to send the data correctly to simulink. The connection checks in both the receive as well as the send blocks work, and sending from simulink works fine. I have tried increasing the timeout, but even 20s didn't make a difference. I always get the error that no data was received: "The specified amount of data was not returned within the Timeout period. Please ensure that data is being sent to the specified port or specify a greater timeout value."
Simulink model and Simulink TCP-Receive block contents Python code:
#!/usr/bin/env python3
import socket
import struct
HOST = 'localhost'
PORT_IN = 65430
PORT_OUT = 6902
s_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_out = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s_in.bind((HOST, PORT_IN))
s_out.bind((HOST, PORT_OUT))
s_in.listen()
s_out.listen()
conn_in, addr_in = s_in.accept()
conn_out,addr_out = s_out.accept()
data = struct.pack('!d', 3.1415)
with conn_in:
print('Connected for input by', addr_in)
print('Connected for output by',addr_out)
while True:
data_in = conn_in.recv(16)
if not data_in:
break
print("Received data:",data_in)
conn_out.sendall(data_in)
s_in.close()
s_out.close()
PS: yes i am aware the code is not pretty/elegant right now, please forgive me i will fix that later
Upvotes: 1
Views: 543