Raspberrypi5-simulink TCP/IP problem transfering data

6 views (last 30 days)
Hi, i am trying to establish a TCP/IP communication between simulink model and my raspberry pi 5 board, i am able to connect them but when it's the time to receive data from simulink to raspberry pi i am not able to receive something, although the ip adress and port are correct, you find attached a part of the simulink model responsable of sending data to raspberrypi and u will find the code of receving data in raspberrypi
and this is my code:
import socket
import numpy as np
def process_data(received_data):
a, b, c = [float(val) for val in received_data.split(',')]
command = simulationat(a, b, c)
return command
def setup_server(host='0.0.0.0', port=25000):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(1)
print(f"Listening on {host}:{port}")
return server_socket
def accept_connections(server_socket):
while True:
client_socket, addr = server_socket.accept()
print(f"Connected by {addr}")
try:
while True:
# Receiving data from client (Simulink)
data = client_socket.recv(1024).decode().strip()
if not data:
break
print(f"Received data: {data}")
# Process the data
response_command = process_data(data)
# Send the response command back to client (Simulink)
client_socket.sendall((response_command + '\n').encode())
except ConnectionResetError:
print(f"Connection lost with {addr}")
finally:
client_socket.close()
server_socket = setup_server(host='192.168.x.x', port=25000)
accept_connections(server_socket)

Answers (1)

Shaunak
Shaunak on 18 Feb 2025
Edited: Shaunak on 18 Feb 2025
Hi Nadjib,
It is my understanding that you are trying to troubleshoot the cause for the data not being processed even though it is not null. This may be caused by the misaligned code after the ‘break’ statement in the ‘accept_connections’ function which prevents any code you have written after it from being executed.
Here is the correct implementation of the ‘accept_connections’ function:
def accept_connections(server_socket):
while True:
client_socket, addr = server_socket.accept()
print(f"Connected by {addr}")
try:
while True:
# Receiving data from client (Simulink)
data = client_socket.recv(1024).decode().strip()
if not data:
break
print(f"Received data: {data}")
# Process the data
response_command = process_data(data)
# Send the response command back to client (Simulink)
client_socket.sendall((response_command + '\n').encode())
except ConnectionResetError:
print(f"Connection lost with {addr}")
finally:
client_socket.close()
Hope this helps!

Community Treasure Hunt

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

Start Hunting!