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.
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:
mdl = 'slrt_ex_target_to_host_UDP'; mdlOpened = 0; systems = find_system('type', 'block_diagram'); if ~any(strcmp(mdl, systems)) mdlOpened = 1; open_system(fullfile(matlabroot,'toolbox','slrealtime','examples',mdl)); end
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 mark signals programmatically for data logging. You can also mark signals for data logging in the Simulink Editor. You can view the logged data in in the Simulation Data Inspector.
set_param(mdl,'RTWVerbose','off'); set_param(mdl,'StopTime','10'); targetIP = '192.168.7.5'; set_param([mdl,'/UDP Receive'],'ipAddress',targetIP); hostIP = '192.168.7.2'; set_param([mdl,'/UDP Send'],'toAddress',hostIP) set_param([mdl,'/UDP Receive'],'fmAddress',hostIP) handle = get_param([mdl,'/Byte Unpacking '],'PortHandles'); Outport = handle.Outport(1); Simulink.sdi.markSignalForStreaming(Outport,'on'); slbuild(mdl); tg = slrealtime; load(tg,mdl);
### Successful completion of build procedure for: slrt_ex_target_to_host_UDP ### Created MLDATX ..\slrt_ex_target_to_host_UDP.mldatx Build Summary Top model targets built: Model Action Rebuild Reason =========================================================================================================== slrt_ex_target_to_host_UDP Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 53.286s
Close the model if it is opened.
if (mdlOpened) bdclose(mdl); end
Create UDP object in MATLAB on Development Computer
uByte = udpport("IPV4","LocalHost",hostIP,"LocalPort",8002);
Run Model on Target Tomputer
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
Disconnect UDP Object on Development Computer