| Contents | Index |
| On this page… |
|---|
Step 1: Open the OPC Toolbox Block Library Step 3: Drag the OPC Toolbox Blocks into the Model Step 4: Drag Other Blocks to Complete the Model Step 5: Configure OPC Servers for the Model |
This section provides a step-by-step example to illustrate how to use the OPC Toolbox block library. The example builds a simple model using the blocks in the OPC Toolbox 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.
Note To run the sample code in the following examples, you must have the Matrikon OPC Simulation Server available on your local machine. For information on installing this, see Installing the Matrikon OPC Simulation Server. The code requires only minor changes work with other servers. |
To open the OPC Toolbox block library, first start the Simulink Library Browser. To start the Simulink Library Browser, enter
simulink
at the MATLAB prompt. MATLAB opens the Simulink Library Browser window. The left pane contains a list of available block libraries in alphabetical order.
To open the OPC Toolbox block library, click its entry in the tree. When you open a library, Simulink loads the library and displays its blocks.
Selecting the OPC Toolbox Block Library in the Simulink Library Browser

Alternatively, you can open the OPC Toolbox block library by typing
opclib
at the MATLAB command prompt.
To use a block, you must add it to an existing model or create a new model.
To create a new model, click the File menu in the Simulink Library Browser and select New > Model. Simulink opens an empty model window on the display.

The OPC Toolbox block library contains four blocks: OPC Configuration, OPC Quality Parts, OPC Read, and OPC Write. 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, click each block in the library and, holding the mouse button down, drag the block into the model window. For this example, you need one instance each of the OPC Configuration, OPC Write, and OPC Read block in your model.
Dragging OPC Toolbox Blocks into Model Window

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 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 model and place it to the left of the OPC Write block.
Dragging Sine Wave Block into Model Window

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 model and place it above and to the right of the OPC Read block. Also drag a Display block into the model and place it below the Scope block.
Dragging Display and Scope Blocks into Model Window

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.
Double-click the OPC Configuration block to open its parameters dialog.

Click Configure OPC Clients to open the OPC Client Manager.

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).

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

The Matrikon OPC Simulation Server is now available throughout the model for reading and writing.
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.
You set parameters for the blocks in your model by double-clicking on the block.
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.

Click Add Items to display a name space browser for the Matrikon OPC Simulation Server.
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.

Click OK to add the item Bucket Brigade.Real8 to the OPC Write block's ItemIDs list.
In the OPC Write parameters dialog, click OK to accept the changes and close the dialog.
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.
Set the read mode to 'Synchronous (device)' and the sample time for the block to 0.2.
Also uncheck the 'Show timestamp port' option. This step removes the time stamp output port from the OPC Read block.

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.

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

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.

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 Strings).
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.

![]() | Overview | Using the OPC Client Manager | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |