Main Content

Read and Write Data from a Model

Example Overview

This section provides a step-by-step example to illustrate how to use the OPC block library. The example builds a simple model using the blocks in the OPC block library with blocks from other Simulink® libraries.

This example writes a sine wave to the Matrikon™ OPC Simulation Server, and reads the data back from the same server. You use the OPC Write block to send data to the OPC server, and the OPC Read block to read that same data back into your model.


To run the code in the following examples, you must have the Matrikon OPC Simulation Server available on your local machine. For information on installing this, see Install an OPC DA or HDA Simulation Server for OPC Classic Examples. The code used in this example requires only minor changes to work with other servers.

Step 1: Create New Model in Simulink Editor

  1. To start Simulink and create a new model, enter the following at the MATLAB® command prompt:


    In the Simulink start page dialog, click Blank Model, and then Create Model. An empty, Editor window opens.

  2. In the Editor, click File > Save As to assign a name to your new model.

Step 2: Open the OPC Block Library

  1. In the model Editor window, click Library Browser.

    The Simulink Library Browser opens in the left pane of the Editor, with a tree of available block libraries in alphabetical order.

  2. Expand the Industrial Communication Toolbox node.

    OPC block library in the Simulink LIbrary Browser

Alternatively, you can open the OPC block library in a standalone window by typing the following command at the MATLAB command prompt:


Step 3: Drag OPC Blocks into the Editor

The OPC block library contains four blocks

You can use these blocks to configure and manage connections to servers, to send and receive live data between your OPC server and your simulation, and to analyze OPC quality.

To use the blocks in a model, select each block in the library and drag the block into the Simulink Editor. For this example, you need one instance each of the OPC Configuration, OPC Write, and OPC Read block in your model.

Drag OPC Configuration, OPC Write, and OPC Read blocks into your model


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 Other Blocks to Complete the Model

Your model requires three more blocks. One block provides the data sent to the server; the other two blocks display the data received from the server.

To send a sine wave to the server, you can use the Sine Wave (Simulink) block. To access the Sine Wave block, expand the Simulink node in the browser tree, and click the Sources library entry. From the blocks displayed in the right pane, drag the Sine Wave block into the Simulink Editor and place it to the left of the OPC Write block.

Drag a sine wave block into your model

You can use the Scope block to show the value received from the server, and a Display block to view the quality of the item. (You will remove the time stamp output port in the next step.) 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 Scope block into the Simulink Editor and place it above and to the right of the OPC Read block. Also drag a Display block into the Simulink Editor and place it below the Scope block.

Drag Scope and Display blocks into your model

Step 5: Configure OPC Servers for the Model

To communicate with OPC servers from Simulink, you first need to configure those servers in the model. The OPC Configuration block manages and configures OPC servers for a Simulink model. Each OPC Read or OPC Write block uses one server from the configured servers, and defines the items to read from or write to.

  1. Double-click the OPC Configuration block to open its parameters dialog.

    OPC Configuration block parameter dialog

  2. Click Configure OPC Clients to open the OPC Client Manager.

    OPC client manager dialog

  3. Click Add to open the OPC Server Properties dialog. Specify the ID of the server as 'Matrikon.OPC.Simulation.1' (or click Select and choose the server from the list of available OPC servers).

    OPC server properties dialog

  4. Click OK to add the OPC server to the OPC Client Manager.

    OPC client manager with client selected

    The Matrikon OPC Simulation Server is now available throughout the model for reading and writing.

  5. Your model will use default values for all other settings in the OPC Configuration block. Click OK in the OPC Configuration dialog to close that dialog.

Step 6: Specify the Block Parameter Values

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

  1. Double-click the OPC Write block to open its parameters dialog. The Matrikon server is automatically selected for you as the OPC client to use in this block. You need to specify the items for writing.

    OPC Write block parameter dialog

  2. Click Add Items to display a name space browser for the Matrikon OPC Simulation Server.

  3. Expand the Simulation Items node in the name space, then expand the Bucket Brigade node. Select the Real8 node and click >> to add that item to the selected items list.

    Select items dialog

  4. Click OK to add the item Bucket Brigade.Real8 to the OPC Write block’s ItemIDs list.

  5. In the OPC Write parameters dialog, click OK to accept the changes and close the dialog.

  6. Double-click the OPC Read block to open its dialog. Add the same item to the OPC Read block, repeating steps 2–5 that you followed for the OPC Write block in this section.

  7. Set the read mode to 'Synchronous (device)' and the sample time for the block to 0.2.

  8. Also uncheck the 'Show timestamp port' option. This step removes the time stamp output port from the OPC Read block.

    OPC Read block parameter dialog

Step 7: Connect the Blocks

Make a connection between the Sine Wave block and the OPC Write block. When you move the cursor near the output port of the Sine Wave block, the cursor becomes crosshairs. Click the Sine Wave output port and hold the mouse button; drag to the input port of the OPC Write block, and release the button.

In the same way, make a connection between the first output port of the OPC Read block (labeled V) and the input port of the Scope block. Then connect the other output port of the OPC Read block (labeled Q) to the input port of the Display block.

Note that the OPC Write and OPC Read blocks do not directly connect together within the model. The only communication between them is through an item on the server, which you defined in Step 5: Configure OPC Servers for the Model.

Completed model for OPC writing and reading

Step 8: Run the Simulation

Before you run the simulation, double-click the Scope block to open the scope view.

Scope block view before simulation

To run the simulation, click Run in the Simulink Editor toolstrip.

The model writes a sine wave to the OPC server, reads back from the server, and displays the wave in the scope trace. In addition, the quality value is set to 192, which indicates a good quality (see OPC Quality).

While the simulation is running, the status bar at the bottom of the model window updates the progress of the simulation, and the sine wave is displayed in the Scope window.

Scope block view after simulation