Products & Services Solutions Academia Support User Community Company

Learn more about Instrument Control Toolbox   

Building Simulink Models to Send and Receive Data

Example: 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 displayed in the browser.

To start the Simulink Library Browser, enter

simulink

at 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. See Using the Simulink Library Browser for more information.

Step 2: Create a New Model

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

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

You can also select the File menu in the Simulink Library Browser and select New > Model. Simulink opens an empty model window on the display. To name the new model, use 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 model window. For this example, you will need one instance of the To Instrument and the Query Instrument block in your model.

Drag Instrument Control Toolbox Blocks into Model Window

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. To access the Constant block, expand the Simulink node in the browser tree, and click the Source library entry. From the blocks displayed in the right pane, drag the Constant block into the model 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 model and place it to the right of the Query Instrument block.

Drag Display Block to the Model Window

Step 5: Connect the Blocks

Make a connection between the Constant block and the To Instrument block. When you move the pointer near the output port of the Constant block, the pointer become a cross hair. Click on the Constant output port and holding the mouse button, drag the pointer to the input port of the To Instrument block, and release the button.

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

Step 6: Specify the Block Parameter Values

You set parameters for the blocks in your model by double-clicking on the block.

Configure the To Instrument 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 Apply, then OK.

Configure the Query Instrument Block.  Double-click the Query Instrument block to open its parameters dialog box. Make sure that the values on the Hardware 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 Apply, then OK.

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

  1. Constant value to 25.

  2. Sample time to 1.

Click OK.

Configure the Display Block.  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. Right-click on the block and select Block Properties. Enter the priority number in the Priority field in 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.

Read Working with Block Parameters in the Simulink User Guide to understand more about block priorities.

Step 8: Run the Simulation

To run the simulation, click the Start button on the model window toolbar. Alternatively, you can use the Simulation menu in the model window and choose the Start option.

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 model window updates the progress of the simulation.

Example: Sending and Receiving Data Via a TCP/IP Network

This section provides an example that builds a simple model using the Instrument Control blocks in the block library in conjunction with other blocks in the Simulink library. This example 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 will simulate 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 that will work as an echo server that you can use to send and receive signals via TCP/IP. To create an echo server, type the following command in MATLAB:

echotcpip('on', 50000)

This will open port 50000 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 displayed in the browser.

To start the Simulink Library Browser, enter

simulink

at 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. See Using the Simulink Library Browser for more information.

Step 3: Create a New Model

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

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

You can also select the File menu in the Simulink Library Browser and select New > Model. Simulink opens an empty model window on the display. To name the new model, use 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 model window. For this model, you will need one instance of the TCP/IP Send and the TCP/IP Receive block in your model.

Drag Instrument Control Toolbox Blocks into Model Window

Step 5: Drag Other 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 on the Sources library entry. From the blocks displayed 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 Model Window

To display the data received by the TCP/IP Receive block, you can use the Scope block. To access the Scope 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 model and place it to the right of the Query Instrument block.

Drag Scope Block to Model Window

Step 6: Connect the Blocks

Make a connection between the Sine Wave block and the TCP/IP Send block. When you move the pointer near the output port of the Sine Wave block, the pointer becomes a cross hair. Click on the Sine Wave output port and holding the mouse button, drag the pointer to the input port of the TCP/IP Send block, and release the button. 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.

The two Instrument Control Toolbox blocks do not directly connect together within the model. You should therefore, set the block Priority to specify the order of execution of the blocks. Without the priorities specified, there is a chance that, during simulation the Receive block can run before the Send block. See Step 8: Specify the Block Priorities for more information.

Step 7: Specify the Block Parameter Values

You set parameters for the blocks in your model by double-clicking on 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.

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 and the Block sample time to 0.1.

Click OK.

Configure the Sine Wave Block.  Double-click on the Sine Wave to open its parameters dialog box. Set the Sample time to 0.1.

Click OK.

Step 8: Specify the Block Priorities

In order for the simulation to run correctly, you should specify the order in which Simulink should process the blocks. Right-click on the block and select Block Properties. Enter the priority number in the Priority field in dialog box. In this case, set the priority of the TCP/IP Send to 1 and the TCP/IP Receive to 2.

Read Working with Block Parameters in the Simulink User Guide to understand more about block priorities.

Step 9: Run the Simulation

To run the simulation, click Start on the model window toolbar. Alternatively, you can use the Simulation menu in the model window and choose the Start option.

While the simulation is running, the status bar at the bottom of the model window updates the progress of the simulation.

Step 10: View the Result

You can 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 the Instrument Control Toolbox blocks, see Blocks Reference.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS