Hi Archana,
You can get the Simulink scope data to Python, once the signal values are in python they can be plotted or streamed.
You can follow the below mentioned steps
1. Simulink Model setup
- Open Simulink model
- Add signal you want to stream to Byte Pack (Embedded Coder > Embedded Targets > Host Communication > Byte Pack)
- Connect output of Byte Pack to UDP Send (Instrument Control Toolbox > UDP Send)
- Configure Byte Pack by double clicking on it and set Data types to double
- Configure UDP Send by double clicking and set:
Remote IP address to 127.0.0.1
Remote IP port to 5005
- Set Stop Time to inf, this will make model run continuously until you manually stop it and live data can be fetched
2. Python Script
Create a new .py file, here is an example code snippet
# This IP and port MUST match the settings in the Simulink UDP Send block.
UDP_IP = "127.0.0.1" # IP address to listen on (localhost)
UDP_PORT = 5005 # Port to listen on
print(f"Starting UDP receiver on {UDP_IP}:{UDP_PORT}")
# AF_INET means we are using IPv4
# SOCK_DGRAM means we are using UDP
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the IP address and port
sock.bind((UDP_IP, UDP_PORT))
print("Receiver is running. Waiting for data from Simulink...")
data, addr = sock.recvfrom(1024)
# Unpack the received data.
# Simulink's Byte Pack block (set to 'double') sends 8 bytes.
value = struct.unpack('<d', data)[0]
# The [0] is because unpack always returns a tuple.
print(f"Received value: {value:.4f}", end='\n')
except KeyboardInterrupt:
print("\nReceiver stopped.")
You can run the Simulink model and the Python file, correspondingly you can see your terminal. You will now see a live stream of numbers from your Simulink model updating rapidly.
Some documentations for reference