Documentation

Target to Target communication using TCP

This example shows how to use TCP blocks to send data between two target computers. This example also describes the effects of a server and a client running at different sample times.

The server model TargetToTargetTCPServer runs on TargetPC1 with sample time .02 second. This model contains a sine wave source. The client model TargetToTargetTCPClient runs on TargetPC2 with sample time .01 second. This model contains a sawtooth wave source. Both models send and receive signal data packets.

Click here to open this example: TargetToTargetTCP.

Open, build, and download the server model

Click here to open model 1: TargetToTargetTCPServer.

% Open the model.
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp('TargetToTargetTCPServer', systems))
  mdlOpen = 1;
  open_system('TargetToTargetTCPServer');
end

% Build the model and download to the target computer.
rtwbuild('TargetToTargetTCPServer');                     % Build and download application.
### Starting Simulink Real-Time build procedure for model: TargetToTargetTCPServer
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: TargetToTargetTCPServer
### Created MLDATX ..\TargetToTargetTCPServer.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1
% Close the model.
if (mdlOpen)
  bdclose('TargetToTargetTCPServer');
end

Open, build, and download the client model

Click here to open model 2: TargetToTargetTCPClient.

% Open the model.
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if ~any(strcmp('TargetToTargetTCPClient', systems))
  mdlOpen = 1;
  open_system('TargetToTargetTCPClient');
end

% Build the model and download to the target computer.
rtwbuild('TargetToTargetTCPClient');                     % Build and download application.
### Starting Simulink Real-Time build procedure for model: TargetToTargetTCPClient
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: TargetToTargetTCPClient
### Created MLDATX ..\TargetToTargetTCPClient.mldatx
### Looking for target: TargetPC2
### Download model onto target: TargetPC2
% Close the model.
if (mdlOpen)
  bdclose('TargetToTargetTCPClient');
end

Run both models

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

start(tg1); % Start the TargetPC1 model.
start(tg2); % Start the TargetPC2 model.
pause(4);

Stop both models

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

stop(tg1); % Stop the TargetPC1 model.
stop(tg2); % Stop the TargetPC2 model.

Generate Server (TargetPC1) Plot

tg1.viewTargetScreen();

  • Scope 1 is the 'Status' output of the 'TCP Server Configure' block. The value goes to 1 when a connection is established.

  • Scope 2 is the 'Status' output of the 'TCP Send' block. After the connection is established the value goes to 16 to indicate that 16 bytes are sent every sample time.

  • Scope 3 is the 'Length' output of the 'TCP Receive' block in the server model. The value in the scope goes from 0 to 16 and then stays at 32. This is due to the fact that the server is running half as fast as the client. As a result for every sample time on the server, the client sends it 2 packets of 16 bytes each. At every sample time, the server blocks have 32 bytes available to read. In this model on 16 bytes are read out so the other 16 bytes are missed out by the model. This will not be a TCP error as the packets are effectively transmitted and received - but the model is configured in a way that causes the packets to be missed.

  • Scope 4 plots the received data.

Generate Client (TargetPC2) Plot

tg2.viewTargetScreen();

  • Scope 1 is the 'Status' output of the 'TCP Client Configure' block. The value goes to 1 when a connection is established.

  • Scope 2 is the 'Status' output of the 'TCP Send' block. After the connection is established the value goes to 16 to indicate that 16 bytes are sent every sample time.

  • Scope 3 is the 'Length' output of the 'TCP Receive' block in the client model. This switches between 0 and 16 since the client runs twice as fast as the server, so for every alternate sample it receives no data.

  • Scope 4 plots the received data.

Effect of the difference in sample times

The following plot compares the sine wave sent by the server and received by the client.

% Server sine wave
tx = tg1.TimeLog;
x = tg1.OutputLog;
% Client sine wave (received)
ty = tg2.TimeLog;
y = tg2.OutputLog;

% Plot a short section
plot(tx(10:30), x(10:30), 'b--o', ty(10:60), y(10:60), 'g--o');
legend({'Server (SampleTime: .02)','Client (SampleTime: .01)'})
xlabel('Time (seconds)')
ylabel('Amplitude')
title('Compare Tx and Rx sine waveform')

  • The client runs twice as fast as the server, so there are double the number of green points for the same time duration.

  • Since the points in between have no new data sent, the previous value is held.

  • The delay between the sine waves depends on various factors like the lack of synchronization between the targets and network caused delays.

Was this topic helpful?