MATLAB: How to send data from one computer running MATLAB to a second computer running Simulink

simulink

If I have one computer running MATLAB, and a second computer running Simulink, how can I communicate between these two using ethernet and UDP? Data transfer rate is not a concern in this application.

Best Answer

The UDP Receive block from the Instrument Control Toolbox (<http://www.mathworks.com/help/instrument/udpreceive.html>) can read signals sent from either a Simulink UDP Send block or a signal sent from a UDP object sent from a MATLAB script (https://www.mathworks.com/matlabcentral/answers/97014-how-do-i-send-data-between-two-computers-using-the-udp-function-from-the-instrument-control-toolbox). However, the major difference between the UDP Receive block and the read method in MATLAB ("fscanf") is that the block expects _numeric _data. Thus, when sending signals from MATLAB, one should use the "fprintf" command, rather than "fwrite".
The following example shows how to set up a communication between one computer sending data via MATLAB, and a second computer, connected via ethernet, receiving signals with the UDP Receive block. As in the linked example above, the IP addresses and ports listed below should be changed to match those of the computers being used.
Step 1: Set up the connection in MATLAB:
% Define computer-specific variables
ipA = '144.28.137.160'; portA = 9090; % Modify these values to be those of your first computer.
ipB = '144.31.165.65'; portB = 9091; % Modify these values to be those of your second computer.
% Create UDP Object
udpA = udp(ipB,portB,'LocalPort',portA);
% Connect to UDP Object
fopen(udpA);
Step 2: Open the Simulink model that receives data. 
Open the attached model "basicUDPReceive.slx". This model contains a UDP Receive block, a scope, and a display block. Note that the "Remote address" in the UDP Receive block should have the same value as the sending computer, i.e. "ipA". The ports need to match as well.
Step 3: Send the data from MATLAB.
The following code sends a simple sequence of 2, 1, 0 over the UDP connection for 30 seconds total.
j=0;
seq = [0 1 2];
tStart = datevec(now);
while etime(datevec(now),tStart)<30 % stop after 45 seconds
j = mod(j,3)+1;
fwrite(udpA,uint8(seq(j)));
j=j+1;
pause(1)
end
Step 4: Receive the data in Simulink.
Run the open model to receive the data. In the attached example, the scope should show the received data.
Step 5: Clean up connections when finished.
fclose(udpA);
clear ipA portA ipB portB udpA
Note: "Blocking mode" has been disabled in the UDP Receive block in this example. This makes it such that the simulation is continuously receiving, and at each time step it will check for a signal. The timesteps are governed by the sample time in the model, as set by the Model Configuration Parameters. If you find that you are losing data, you may be able to adjust the sample time or else you model will require blocking mode. Please review the documentation linked above for more information on blocking.