TCP/IP returns empty bytes while connectred
Show older comments
I Tried to stream data from simulink to python. using tcp ip.
matlab model

python code is as below
import socket
# Set up a TCP server
PORT =31515
HOST='127.0.0.1'
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', PORT))
print("connected")
BUFFER_SIZE=1024
while True:
# receive data from Simulink
data = s.recv(BUFFER_SIZE)
#datanew=read(s)
print(data)
converteddata = bytes(data)
#print('received data:', converteddata)
s.close()
but it only returns empty bytes instead of values like this
b' '
how can we get the values to python, using tcpip from simulink?
and the delay data is printed is also long.
1 Comment
Surya
on 18 Apr 2023
Hi,
By reducing the buffer size, adding a small delay to your python may help.
Try:
import socket
import time
# Set up a TCP server
PORT = 31515
HOST = '127.0.0.1'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print("Connected")
BUFFER_SIZE = 256 # Reduced buffer size
while True:
# Receive data from Simulink
data = s.recv(BUFFER_SIZE)
# Process received data
if data:
# Convert data to appropriate format as needed
print(data)
# Add a small delay to reduce CPU usage
time.sleep(0.01)
s.close()
Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!