Target to Host Transmission by Using UDP
This example shows how to use UDP blocks to send data from a target computer to a development computer. This example uses a target computer located at IP address 192.168.7.5 and uses a development computer located at IP address 192.168.7.2.
The transmit real-time application slrt_ex_target_to_host_UDP
runs on the target computer and send signal data to the UDP object that the script creates in MATLAB® on the development computer.
When using the UDP protocol for communicating data to or from the target computer, consider these issues:
The Simulink® model on the development computer runs as fast as it can. The model run speed is not synchronized to a real-time clock.
UDP is a connectionless protocol that does not check to confirm that packets were transmitted. Data packets can be lost or dropped.
On the target computer, UDP blocks run in a background task that executes each time step after the real-time task completes. If the block cannot run or complete the background task before the next time step, data may not be communicated.
UDP data packets are transmitted over the Ethernet link between the development and target computers. These transmissions share bandwidth with the Ethernet link.
For more information about UDP and Simulink Real-Time, see UDP Communication Setup.
Create Target Object and Connect
Create a Target object for the default target computer and connect to the target computer. In the Command Window, type:
tg = slrealtime; connect(tg);
Open Model, Build, and Load Real-Time Application
This model drives a first order transfer function with a square wave signal and sends the transfer function input and output signals to the development computer using UDP. To open the model, in the MATLAB Command Window, type:
model = 'slrt_ex_target_to_host_UDP'; open_system(model); modelSTF = getSTFName(tg); set_param(model,"SystemTargetFile",modelSTF);
Build the model and download to the target computer.
Configure for a non-Verbose build.
Mark the Byte Unpacking block output for data logging.
Build and download application.
Open the Simulation Data Inspector.
This code shows how to complete these steps and shows how to mark signals for data logging. You can also mark signals for data logging in the Simulink Editor. You can view the logged data in the Simulation Data Inspector.
set_param(model,'RTWVerbose','off'); set_param(model,'StopTime','20'); targetIP = '192.168.7.5'; set_param([model,'/UDP Receive'],'ipAddress',targetIP); hostIP = '192.168.7.2'; set_param([model,'/UDP Send'],'toAddress',hostIP) set_param([model,'/UDP Receive'],'fmAddress',hostIP) handle = get_param([model,'/Byte Unpacking '],'PortHandles'); Outport = handle.Outport(1); Simulink.sdi.markSignalForStreaming(Outport,'on'); evalc('slbuild(model)'); load(tg,model);
Close the model.
bdclose(model);
Create UDP object in MATLAB on Development Computer
uByte = udpport("IPV4","LocalHost",hostIP,"LocalPort",8002);
Run Model on Target Computer
start(tg);
Read Data and Write Development Computer
tic; while (toc<10) data = read(uByte,16); write(uByte,data,targetIP,25000); data = read(uByte,16); end
View Signals in Simulation Data Inspector
Simulink.sdi.view;
Disconnect UDP Object on Development Computer
clear uByte;