Target to Target Transmission using UDP

This example shows how to use UDP blocks to send data between two target computers. The model TargetToTargetRealtimeUDP1 runs on TargetPC1. The model TargetToTargetRealtimeUDP2 runs on TargetPC2. Both models send and receive signal data packets.

Note: When considering UDP as a protocol for communicating data to/from the Simulink® Real-Time™ environment, it is important to be aware of the following:

  • UDP is a connectionless protocol that does not check to confirm that packets were transmitted or received. Data packets can be lost or dropped.

  • For more information on using UDP with Simulink Real-Time, see the documentation on UDP I/O support.

Click here to open this example: TargetToTargetUDP.

Open, Build, and Download the TargetPC1 Model

Click here to open model 1: TargetToTargetRealtimeUDP1.

Open the model.

mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp('TargetToTargetRealtimeUDP1', systems))
  mdlOpen = 1;
  open_system(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','TargetToTargetRealtimeUDP1'));
end

Build the model and download to the target computer.

  • Configure for a non-Verbose build.

  • Configure UDP parameters for TargetPC1.

  • Build and download application.

set_param('TargetToTargetRealtimeUDP1','RTWVerbose','off');
set_param('TargetToTargetRealtimeUDP1/UDP Configure','PCIBus','5')
set_param('TargetToTargetRealtimeUDP1/UDP Configure','PCISlot','0')
set_param('TargetToTargetRealtimeUDP1/UDP Configure','PCIFunction','0')
rtwbuild('TargetToTargetRealtimeUDP1');
tg = slrt('TargetPC1');
load(tg,'TargetToTargetRealtimeUDP1');
### Starting Simulink Real-Time build procedure for model: TargetToTargetRealtimeUDP1
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: TargetToTargetRealtimeUDP1
### Created MLDATX ..\TargetToTargetRealtimeUDP1.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Close the model if we opened it.

if (mdlOpen)
  bdclose('TargetToTargetRealtimeUDP1');
end

Open, Build, and Download the TargetPC2 Model

Click here to open model 2: TargetToTargetRealtimeUDP2.

Open the model.

mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp('TargetToTargetRealtimeUDP2', systems))
  mdlOpen = 1;
  open_system(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','TargetToTargetRealtimeUDP2'));
end

Build the model and download to the target computer.

  • Configure for a non-Verbose build.

  • Configure UDP parameters for TargetPC2.

  • Build and download application.

set_param('TargetToTargetRealtimeUDP2','RTWVerbose','off');
set_param('TargetToTargetRealtimeUDP2/UDP Configure','PCIBus','8')
set_param('TargetToTargetRealtimeUDP2/UDP Configure','PCISlot','10')
set_param('TargetToTargetRealtimeUDP2/UDP Configure','PCIFunction','0')
rtwbuild('TargetToTargetRealtimeUDP2');
tg2 = slrt('TargetPC2');
load(tg2,'TargetToTargetRealtimeUDP2');
### Starting Simulink Real-Time build procedure for model: TargetToTargetRealtimeUDP2
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: TargetToTargetRealtimeUDP2
### Created MLDATX ..\TargetToTargetRealtimeUDP2.mldatx
### Looking for target: TargetPC2
### Download model onto target: TargetPC2

Close the model if we opened it.

if (mdlOpen)
  bdclose('TargetToTargetRealtimeUDP2');
end

Run both Models

Using the Simulink Real-Time object variables tg1 and tg2, start the models.

  • Start the TargetPC1 model. Simulink object is tg.

  • Start the TargetPC2 model. Simulink object is tg2.

  • Run for 5 seconds.

start(tg);
start(tg2);
pause(5);

Stop both Models

Using the Simulink Real-Time object variables tg1 and tg2, stop the models.

  • Stop the TargetPC1 model. Simulink object is tg.

  • Stop the TargetPC2 model. Simulink object is tg2.

stop(tg);
stop(tg2);

Generate TargetPC1 Plot

tg.viewTargetScreen();

Generate TargetPC2 Plot

tg2.viewTargetScreen();