Building Simulink Models to Transmit Data

Sending and Receiving Data Through a Serial Port Loopback

This section provides an example that builds a simple model using the Instrument Control Toolbox™ blocks in conjunction with other blocks in the Simulink® library. The example illustrates how to send data to a simple loopback device connected to the computer's COM1 serial port and to read that data back into your model.

You will use the To Instrument block to write a value to the serial port on your computer, and then use the Query Instrument block to read that same value back into your model.

Step 1: Open the Block Library

To open the Instrument Control Toolbox block library, start the Simulink Library Browser and choose Instrument Control Toolbox from the list of available libraries in the browser.

To start the Simulink Library Browser, enter

simulink

in the MATLAB® Command Window. The left pane in the Simulink Library Browser lists the available block libraries. To open the Instrument Control Toolbox block library, click its entry icon.

Step 2: Create a New Model

To use a block, add it to an existing model or create a new model.

For this example, create a new model by clicking the New model button on the toolbar.

You can also select File > New in the Simulink Library Browser. Simulink opens an empty model in the Simulink Editor. Name the new model using the Save option.

Step 3: Drag the Instrument Control Toolbox Blocks into the Model

To use the blocks in a model, click a block in the library and, holding the mouse button down, drag it into the Simulink Editor. For this example, you need one instance of the To Instrument and the Query Instrument blocks in your model.

    Note:   The To Instrument block can be used with these interfaces: VISA, GPIB, Serial, TCP/IP, and UDP. It is not supported on these interfaces: SPI, I2C, and Bluetooth.

Drag Instrument Control Toolbox Blocks into Model

Step 4: Drag Other Blocks to Complete the Model

This example requires two more blocks. One block provides the data that is sent to the instrument; the other block displays the data received from the instrument.

Because the data sent to the instrument will be a constant, you can use the Constant block for this purpose. Access the block by expanding the Simulink node in the browser tree, and clicking the Sources library entry. From the blocks in the right pane, drag the Constant block into the Simulink Editor and place it to the left of the To Instrument block.

Drag Constant Block to the Model Window

To display the data received from the instrument, you can use the Display block. To access the Display block, click the Sinks library entry in the expanded Simulink node in the browser tree. From the blocks displayed in the right pane, drag the Display block into the Simulink Editor and place it to the right of the Query Instrument block.

Step 5: Connect the Blocks

Make a connection between the Constant block and the To Instrument block. A quick way to make the connection is to select the Constant block, press and hold the Ctrl key, and then click the To Instrument block.

In the same way, make the connection between the output port of the Query Instrument block and the input port of the Display block.

    Note:   The two blocks do not directly connect together within the model. The only communication between them is through the instrument, which is the loopback connected to the COM1 serial port. Because there is no direct connection between these two blocks, you must consider their timing when running the model. The Query Instrument block does not get its input from the To Instrument block, so it has no way to know when the data from the instrument is available. Therefore, you must set the block parameters to write the data to the loopback before the model attempts to receive data from the loopback.

Step 6: Specify the Block Parameter Values

Set parameters for the blocks in your model by double-clicking the block.

Double-click the To Instrument block to open its parameters dialog box. Accept the default values configured for this block, so you do not need to modify any of the values.

Click OK to close the dialog box.

Double-click the Query Instrument block to open its parameters dialog box. Make sure that the values on the Hardware Configuration tab match the Hardware values on the To Instrument block.

The model uses the default values on the Instrument Initialization and Query tabs of this block, so you do not need to modify any of their values.

Click OK to apply any changes and close the dialog box.

Double-click the Constant block to open its parameters dialog box. Change the Constant value to the value you want to send to the instrument. For this example, change:

  1. Constant value to 25.

  2. Sample time to 1.

Click OK.

For the Display block, you can use its default parameters.

Step 7: Specify the Block Priority

The block with the lowest number gets the highest priority. In the Simulink Editor, right-click a block and select Properties. Enter the priority number in the Priority field in the Block Properties dialog box. To ensure that the To Instrument block first completes writing data to the loopback before the Query Instrument block reads it, set the priority of the To Instrument block to 1 and the Query Instrument block to 2.

    Caution   It is essential to set the correct priority for the blocks in your model. Otherwise you may see unexpected results.

To understand more about block priorities, see the Simulink documentation.

Step 8: Run the Simulation

To run the simulation, click the green Run button on the Simulink Editor toolbar. You can use toolbar options to specify how long to run the simulation and to stop a running simulation. You can also start the simulation by selecting Simulation > Run.

When you run the simulation, the constant value you specified (25) is written to the instrument (the serial loopback), received from the instrument, and shown in the Display block.

While the simulation is running, the status bar at the bottom of the Simulink Editor updates the progress of the simulation.

Sending and Receiving Data Over a TCP/IP Network

This example builds a simple model using the Instrument Control 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.

You will 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, and then use the TCP/IP Receive block to read that same data back into your 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 via TCP/IP. To create an echo server, type this command in MATLAB:

echotcpip('on', 50000)

Port 50000 opens on your machine to work as an echo server and turn it on.

Step 2: Open the Block Library

To open the Instrument Control Toolbox block library, start the Simulink Library Browser and choose Instrument Control Toolbox from the list of available libraries in the browser.

To start the Simulink Library Browser, enter

simulink

in the MATLAB Command Window. In the Simulink Library Browser, the left pane lists the available block libraries. To open the Instrument Control Toolbox block library, click its entry icon.

Step 3: Create a New Model

To use a block, add it to an existing model or create a new model.

For this example, create a new model by clicking the New model button on the toolbar.

You can also select File > New in the Simulink Library Browser. Simulink opens an empty model in the Simulink Editor. Name the new model using the Save option.

Step 4: Drag the Instrument Control Toolbox Blocks into the Model

To use the blocks in a model, click each block in the library and, holding the mouse button down, drag it 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.

Drag Instrument Control Toolbox Blocks into the 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 receive block and the other block is the data to be sent to the send block.

The TCP/IP Send block needs a data source for data to be sent. Add the Sine Wave 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.

Drag Sine Wave Block to the Model

To display the data received by the TCP/IP Receive block, use the Scope 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.

Drag Scope Block to the Model

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. 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 should process the blocks. Right-click the block and select Properties. Enter the priority number in the Priority field. In this case, set the priority of TCP/IP Send to 1 and TCP/IP Receive to 2.

    Caution   It is essential to set the correct priority for the blocks in your model. Otherwise, you may see unexpected results.

Read the Simulink documentation to understand more about block priorities.

You also need to set two parameters on the model. In the Simulink Editor, select Simulation > Model Configuration Parameters. In the Configuration Parameters dialog box, set the Type field to Fixed-step and set the Solver field to discrete (no continuous states).

Step 9: Run the Simulation

To run the simulation, click the green Run button on the Simulink Editor toolbar. You can use toolbar options to specify how long to run the simulation and to stop it. You can also start the simulation by selecting Simulation > Run.

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.

Was this topic helpful?