TCP/IP returns empty bytes while connectred
Afficher commentaires plus anciens
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 commentaire
Surya
le 18 Avr 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()
Réponses (0)
Catégories
En savoir plus sur Call Python from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!