Use C # programming to communicate with Siemens PLC through OPC-synchronous communication

Source: Internet
Author: User

1. OPC service Introduction

The latest Software provided by Siemens: Simatic Net PC-Software CD 2005 provides a unified platform for the development of various configuration Software. Its PC station is not only used for some configuration Software, such: winCC, Protol, and so on provide a communication platform with PLC, also provides a set of programming interfaces, you can use advanced language programming through Simatic Net access PLC Data. This programming interface is discussed in this article. The latest version of Simatic NET supports five programming methods:

<1> ActiveX Controls

A series of data access controls are provided to facilitate communication with PLC using controls in VB6.

<2> OPC Automation

Use OLE Automation for programming for VB6, Dephi, and other languages.

<3> OPC user interface

This is an efficient programming method specifically provided for VC ++. Its flexibility and execution efficiency are much higher than the previous two methods.

<4> OPC user interface for Microsoft. NET platform

This is also a very flexible programming interface, but it is targeted.. NET platform, which provides a large number.. NET and other advanced programming languages. This document describes this interface in detail.

<5> opl xml interface

As the name suggests, it is mainly for XML programming.

For <2>, <3>, and <4> programming methods, they can be divided into synchronous access mode and asynchronous access mode. According to the documentation of Siemens, synchronous communication means that when a customer accesses the server, the access requests of other customers must wait until the server processes the requests of the customer, in order to continue the next service, asynchronous access is the opposite. This article focuses on Synchronous Programming and will be provided later.

2. Configure the OPC Server

To program, you must configure the server first. This article takes the Prfibus DP network as an example to introduce the configuration of the PC station. The content mainly comes from the Siemens documentation.

Required software:

Step 7 V5.3
Simatic Net PC-Software CD 2005

Required hardware:

At least CP5611 or above, the notebook can be CP5511, S7-300 PLC with DP port (if the use of Simatic NET simulation function can not need these hardware, will be introduced later)

<1> Configure an S7 station and configure the Profibus DP network. Set the DP address to 3, download it to the PLC, and switch the network cable from the MPI port to the DP port. The configuration of the S7 site is not described here.

<2> Create a new project in Step 7 V5.3, insert a PC station, and change the name of the PC station to the same name as your computer. Open the hardware configuration interface of the PC station. Insert the OPC server and connection card CP5611 (or CP5511), where they can be anywhere, such:

 




Note: when inserting CP5611, you should select the same Profibus network as the S7 station configuration and set the network address to 2. do not conflict with the PLC address.

Click the red icon in the toolbar below:

 




Select "OPC Server" and insert a new connection, for example:

 




In the displayed dialog box, set the Connection type to S7 Connection, for example:

 




After OK, enter 3 in the red position of the new dialog box to indicate the PLC address, for example:

 




And select Address Details ..., Set the CPU slot to 2, for example:

 




After OK, compile and save it.

<3> there are two ways to create an OPC server. This article introduces a simple one.

Open, the Station Configurator in Simatic Net. After installation, it will automatically start and click Import Station... Click to find

The XDB file in the XDBs folder is imported successfully.

<4> you can use the OPC Scout in Simatic Net, select the Simatic NET service, create a group under it, and then create a variable under the group to monitor PLC Data, VC # You do not need to use this program for programming, but you are familiar with using OPC Scout to understand the programming structure in Simatic Net.

Note: After you open the Configuration Console in Simatic Net and select S7 for the following Configuration, you do not need to use PLC, CP5611, or simulate it, for example:

 




All the above steps can be found in the Configuration Console, under the PC Station root tree, select the corresponding help documentation.

3. OPC Programming

<1> the variable structure of Siemens is as follows:

---------------------- Server ------------------------------
/OPC. SimaticNet OPCServer. Wincc... (a series of types of servers)
/Group1 Group2 Group3... (unified the variables with the same update time into a group)
/Item1 Item2... (variable: I, Q, M, DB, etc., pointing to a connection of the OPC Server service of a PC station in the Network)
Bytes -----------------------------------------------------------------------------------------------------------------
The first layer is different types of servers, such as OPC. SimaticNET type, OPC. SimaticNET. DP type, OPCServer. WinCC type, and so on. Here we select OPC. SimaticNET type.

The second layer is Group. A server can have multiple groups, which can be understood as a set of variables with the same scan cycle. When developing the configuration interface, you can unify all the variables in one interface into one group.

The third layer is Item, which is a series of variables that point to a connection of the OPC Server service of a PC station in the network, such as I, Q, M, DB, etc.

<2> name of an item

Item is an Item, which is directly a variable in the PLC in the S7 connection. Therefore, its name is very important:

Format: :[ ]
The protocolID indicates the connection type. You can select the connection type When configuring the PC site above. It should be the same here. There are nine types, the most commonly used is S7, that is, S7 connection, for other types, see the document.

Connectionname: the name of the connection generated when the PC station is configured above. If the simulation function is used, the connection name is DEMO Variablename: The variable name has a series of rules, readers can also use OPC Scout to create variables and learn how a program generates variable names.

S7: [DEMO] MB1: indicates that the connection type is S7, the connection name is DEMO (simulation here), and the variable is MB1.
S7: [DEMO] QB0, 3: represents three consecutive variables starting from QB0.
S7: [DEMO] DB10, X4.6: represents DBX4.6 of DB10.

<3> Add reference

Add OpcRcw to VC # development environment. reference of the Da library, which belongs. NET Library does not belong to the COM library. Although Siemens has compiled a class library to provide. but these class libraries are still difficult to program,

It contains a large amount of data transmitted in the hosting and hosting areas. Therefore, we need to develop a class library based on it to simplify future programming. First, we need to use the namespace at the beginning of the class:
Using System. Runtime. InteropServices;
Using OpcRcw. Da;
Using System. Collections;

<4> Programming

1. Generate name variables at the beginning of the class

Private string serverType = "";
Private IOPCServer pIOPCServer; // OPC server interface
Private Object pobjGroup1; // Pointer to group object
Private int nSvrGroupID; // server group handle for the added group
Private System. Collections. Hashtable groupsID = new Hashtable (11); // used to record the group name and group ID
Private System. Collections. Hashtable hitemsID = new Hashtable (17); // used to record the item name and item ID
Private Guid iidRequiredInterface;
Private int hClientGroup = 0; // customer group number
Private int hClientItem = 0; // Item No.

2. Create a server and compile the Open () method
///


/// Create an OPC Server interface
///
///
/// If it is true, the creation is successful; otherwise, the creation fails.
Public bool Open (out string error)
{
Error = ""; bool success = true;
Type svrComponenttyp;
// Obtain the OPC Server COM interface
IidRequiredInterface = typeof (IOPCItemMgt). GUID;
SvrComponenttyp = System. Type. GetTypeFromProgID (serverType );
T

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.