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?
def start_tcp_server_v3():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.settimeout(100) # Set timeout to 100 seconds as specified in Simulink
host, port = ‘localhost’, 50000
server_socket.bind((host, port))
# Listen for incoming connections
print(“Server is listening…”)
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)
# Get user input for the data to send
user_input = input(“Please enter a double value to send: “)
double_value = float(user_input)
print(“Invalid input. Please enter a valid double value.”)
data = struct.pack(‘>d’, double_value) # Big-endian byte order, type double
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
print(“Server timeout. No client connected.”)
except KeyboardInterrupt:
print(“Server is shutting down.”)
# Close client socket if it exists
if ‘client_socket’ in locals():
# Close the server socket
print(“Server has been closed.”)
# —————————————–