Off-by-one Problem with TCP/IP Client Receive Block and Python Server Sender

조회 수: 12 (최근 30일)
Simon
Simon 2023년 10월 16일
댓글: Daniel 2024년 5월 7일
When I send the first value from my py server to the Simulink TCP/IP Client Receive block (non-blocking-mode), nothing happens and the data and status outputs both remain 0. When I send the second value, the first value is received. When I send the third, the second is received. And so on.
I have no idea why this is happening. Does anyone know how to fix this, or do I need to work around this phenomenon?
[The if-block and data store memory are for handling the non-blocking mode]
[The if-block and data store memory are for handling the non-blocking mode]
Python-Server:
import socket
import struct
def start_tcp_server_v3():
# Initialize socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.settimeout(100) # Set timeout to 100 seconds as specified in Simulink
# Bind the socket
host, port = 'localhost', 50000
server_socket.bind((host, port))
while True:
try:
# Listen for incoming connections
server_socket.listen(1)
print("Server is listening...")
# Establish connection
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address}")
# Reset the timeout for the client socket
client_socket.settimeout(None)
while True:
try:
# Get user input for the data to send
user_input = input("Please enter a double value to send: ")
try:
double_value = float(user_input)
except ValueError:
print("Invalid input. Please enter a valid double value.")
continue
# Prepare data
data = struct.pack('>d', double_value) # Big-endian byte order, type double
# Send data
client_socket.sendall(data)
print("Just sent: [ "+str(double_value)+" ]")
except (BrokenPipeError, ConnectionResetError, ConnectionAbortedError):
print("Client disconnected. Returning to listening mode.")
client_socket.close() # Close the client socket
break # Break the inner loop to go back to listening for new connections
except socket.timeout:
print("Server timeout. No client connected.")
continue
except KeyboardInterrupt:
print("Server is shutting down.")
break
finally:
# Close client socket if it exists
if 'client_socket' in locals():
client_socket.close()
# Close the server socket
server_socket.close()
print("Server has been closed.")
# -----------------------------------------
start_tcp_server_v3()

답변 (1개)

Ayush
Ayush 2024년 1월 5일
편집: Ayush 2024년 1월 5일
Hi Simon,
I understand that you want to fix your issue of receiving the data over a TCP/IP connection via the Simulink TCP/IP Client Receive block in real-time and not encounter a delay of one time-step every time.
I was able to reproduce the same error on my end with MATLAB R2023b release. It looks to be an issue with the condition that checks the number of bytes in the buffer with the output size set in the block parameters. This is a known bug and expected to be resolved in the upcoming MATLAB R2024a release.
You can achieve the same functionality by downloading the MATLAB R2024a prerelease or wait for the full release if you have the valid license for the same. Please refer to the below link for more information about MATLAB R2024a prerelease.
Hope it helps,
Regards,
Ayush Misra

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by