Send and Receive Data over TCP/IP Network
This example shows how to build a simple model using the Instrument Control Toolbox™ blocks in the block library in conjunction with other blocks in the Simulink® library. This example also illustrates how to send data to an echo server using TCP/IP and to read that data back into your model.
In this example, you create an echo server on your machine that simulates sending a signal to the TCP/IP Send block and echo the result back to the Send block to send data. You then use the TCP/IP Receive block to read that same data back into your model.
Note
Block names are not shown by default in the model. To display the hidden block names while working in the model, select Display and clear the Hide Automatic Names check box.
Step 4: Drag the Instrument Control Toolbox Blocks into the Model
Step 5: Drag the Sine Wave and Scope Blocks to Complete the Model
Step 1: Create an Echo Server
Open a port on your computer to work as an echo server that you can use to send and receive signals using TCP/IP. To create an echo server, run the following command in MATLAB®.
echotcpip('on',50000)
Port 50000 opens on your machine to work as an echo server and turn it on.
Step 2: Create a New Model
To start Simulink and create a new model, enter the following at the MATLAB command prompt.
simulink
On the Simulink start page, click Blank Model and then Create Model. An empty Editor window opens.
In the toolstrip, click Save to assign a name to your new model.
Step 3: Open the Block Library
In the toolstrip, click Library Browser in the Simulation tab.
The Simulink Library Browser opens. The left pane contains a tree of available block libraries in alphabetical order. Click Instrument Control Toolbox.
To use a block, add it to an existing model or create a new model.
Step 4: Drag the Instrument Control Toolbox Blocks into the Model
To use a block in a model, drag the block into the Simulink Editor. For this model, you need one instance of the TCP/IP Send and the TCP/IP Receive blocks in your model.
Step 5: Drag the Sine Wave and Scope Blocks to Complete the Model
This example requires two more blocks. One block displays the data received from the TCP/IP Receive block and the other block is the data to be sent to the TCP/IP Send block.
The TCP/IP Send block needs a data source for data to be sent. Add the Sine Wave (Simulink) block to the model to send signals to the TCP/IP Send block. To access the Sine Wave block, expand the Simulink node in the browser tree, and click the Sources library entry. From the blocks in the right pane, drag the Sine Wave block into the model and place it to the left of the TCP/IP Send block.
To display the data received by the TCP/IP Receive block, use the Scope (Simulink) block. To access this block, click the Sinks library entry in the expanded Simulink node in the browser tree. From the blocks in the right pane, drag the Display block into the model and place it to the right of the TCP/IP Receive block.
Step 6: Connect the Blocks
Make a connection between the Sine Wave block and the TCP/IP Send block. A quick way to make the connection is to select the Sine Wave block, press and hold the Ctrl key, and then click the TCP/IP Send block. In the same way, make the connection between the output port of the TCP/IP Receive block and the input port of the Scope block.
Step 7: Specify the Block Parameter Values
Set parameters for the blocks in your model by double-clicking the block.
Configure the Send Block
Double-click the TCP/IP Send block to open its parameters
dialog box. Set the Remote address field to
localhost
and the Port field to
50000
, since that is the address you set the echo server
to when you started it.
Click Apply and then OK.
Configure the Receive Block
Double-click the Receive block to open its parameters dialog
box. Set the Remote address field to
localhost
and the Port field to
50000
. Change the Data type to
double
. The Block sample time field
is set to 0.01
by default. The block sample time here must
match the one in the Sine Wave block, so confirm that they are
both set to 0.01
.
Click OK.
Configure the Sine Wave Block
Double-click the Sine Wave block to open its parameters dialog
box. Set the Sample time field to
0.01
.
Click OK.
Step 8: Specify the Block Priorities
To run the simulation correctly, specify the order in which Simulink processes the blocks. Right-click the block and select
Properties. Enter the priority number in the
Priority field. For this example, set the priority of
TCP/IP Send to 1
and TCP/IP
Receive to 2
.
Caution
You must set the correct priority for the blocks in your model. Otherwise you might see unexpected results.
You also need to set two parameters on the model. In the Simulink toolstrip, click Model Settings from the
Prepare section on the Simulation tab.
In the Configuration Parameters dialog box, set the Type field
to Fixed-step
and set the Solver field to
discrete (no continuous states)
.
Click OK.
Step 9: Run the Simulation
To run the simulation, click the green Run button on the Simulink Editor toolstrip. You can use toolstrip options to specify how long to run the simulation and to stop a running simulation.
While the simulation runs, the status bar at the bottom of the Simulink Editor updates the progress of the simulation.
Step 10: View the Result
Double-click the Scope block to view the signal on a graph as it is received by the TCP/IP Receive block.
For more information about Instrument Control Toolbox blocks, see the blocks reference documentation.