How to send data from Simulink using the "UDP Send" block and receive the data in MATLAB?

10 views (last 30 days)
I would like to send data from a Simulink model and receive data in MATLAB with a script in the same MATLAB session. How can I achieve that using the "UDP Send" block (from the Instrument Control Toolbox) in Simulink and the "udpport" object in MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 11 Apr 2023
To receive data in MATLAB, the local IP and local port number set in the "udpport" object have to match the remote IP and remote port number of the send block in Simulink respectively. To run the Simulink model and the MATLAB code on the same computer, you could use "127.0.0.1" for both, which is the loopback address.
The following procedure demonstrates the workflow of reading data asynchronously from an input buffer using the "BytesAvailableFcn" event callback, such that data is only read when a specified number of bytes become available. Please note that the files attached are needed in order to run this example, which has been verified in MATLAB R2022b.
1) Run the MATLAB script "data_receive_callback.m", which creates a "udpport" object bound to a UDP socket and starts listening.
2) Run the Simulink model "data_send_model.slx" to send data. The MATLAB Command Window should then display the data received.
3) When you finish, disconnect and clear the "udpport" connection through the following:
clear u;
Please be aware that the "udpport" object is introduced since MATLAB R2020b. More information on the object can be found here:

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!