Raspberrypi5-simulink TCP/IP problem transfering data

19 ビュー (過去 30 日間)
Nadjib
Nadjib 2024 年 4 月 18 日
編集済み: Shaunak 2025 年 2 月 18 日 10:56
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)

回答 (1 件)

Shaunak
Shaunak 2025 年 2 月 18 日 10:41
編集済み: Shaunak 2025 年 2 月 18 日 10:56
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!

カテゴリ

Help Center および File ExchangeDevelopment Computer Setup についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by