Using OPC to realize data exchange between VC application program and PLC

Publisher:码梦小子Latest update time:2011-05-13 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction
VC (Visual C++) is a powerful Windows application visualization software development tool. VC supports object-oriented design methods and can use the powerful Microsoft Foundation Class Library MFC (Microsoft Foundation Class). And because of Microsoft's monopoly in the operating market, the software developed with VC has good stability and strong portability, and the software and hardware are independent of each other [1]. It can be used to develop the upper management system of the control system. RSView32 is a configuration software specially used for industrial control. It not only contains a large number of graphic development tools and ready-made graphic libraries, allowing users to easily develop systems, but also can configure alarms, activity records, events, historical trends, etc. It is a powerful industrial automation product [2], so it is very convenient to configure the lower-level equipment. In the actual system development, the OPC technology is used to effectively combine the two tools, so that the upper-level VC program can indirectly communicate with the lower-level PLC through RSView32 to obtain satisfactory results.

2 Introduction to OPC
OPC (OLE for Process Control) is an open and interoperable user interface standard based on the functions required by Microsoft's OLE (now Active), COM (Component Object Model) and DCOM (Distributed Component Object Model) technologies. It ensures the interoperability between automation/control applications and regional systems/equipment. It uses the OLE/COM mechanism as the application-level communication standard and adopts the CLIENT/SERVER model. The typical OPC architecture is shown in Figure 1:

Figure 1 Typical OPC architecture
The OPC specification provides two sets of interface solutions, namely, custom interface and automation interface. Custom interface is highly efficient and can bring out the best performance of OPC server. Clients using C++ language generally use custom interface solution; automation interface makes it possible for interpreted language and macro language to access OPC server. Clients using VB and other languages ​​generally use automation interface.
OPC data access server consists of three types of objects: server, group, and item. Server object is used to indicate the name of a specific OPC server application and serves as a container for group object; group object stores group information composed of several items and logically organizes data items; data item object () stores the definition, data value, status value and other information of specific item. One item represents a specific process variable. To obtain data from OPC server, OPC client application must specify the computer name where the server application is located (the server application and client application are not on the same PC), OPC data access server name and the definition of OPC item provided by the server.
After establishing an OPC connection, client applications can generally read data from the OPC server in three ways: using the synchronous interface IOPC-SyncIO, which is simple and effective and suitable for client programs that only read a small amount of data; using the "subscribe" function OnChange of the interface IOPCCallback, the server automatically notifies the client whenever the data changes; using the asynchronous interface IOPCASyncIO2, which can communicate directly with the physical device, which is slow but has high data accuracy.

3 RSView32 as OPC Server
RSView32, one of Siemens' general-purpose configuration software for industrial control, supports OPC technology. It can be used as an OPC client to communicate with external OPC server software, or as an OPC server to connect with other third-party software that supports OPC technology. In this article, RSView32 is used as a server and the VC application is used as a client. The C/S mode is used to implement data exchange between the two.
3.1 Enable RSView32 as an OPC server [4]
Use one of the following methods to enable RSView32 as an OPC server:
(1) Select the "OPC/DDE Server" checkbox on the "Startup" page of the "Startup" editor;
(2) Issue the RTDataServerOn command (from the command line or another RSView32 component, use the RTDataServerOff command to cancel this function), which will allow other applications to read the value but not change it;
(3) Issue the RTDataWriteEnable command (from the command line or another RSView32 component, use the RTDataWriteDisable command to cancel this function), which allows writing from an external OPC application to change the RSView32 tag value.
3.2 Create an OPC client project [4]
To obtain data from RSView32, the VC application must use the following information:
Server: RSI.RSView32OPCTagServer;
Type: Local/Remote;
Server computer name or address: If the client and server are on the same computer, this can be blank.
Access path: Project name;
Update rate: A rate in seconds;
Item: Tag name. This can be obtained by viewing the RSView32 tag database.

4 VC application as OPC client program implementation
In the VC environment, use the custom interface to develop OPC client applications. The following are the key steps for program implementation.
4.1 Include OPC header files
In addition to the OPC interface, you also need to include the OPC standard library files in the program when developing OPC client applications. You can download these files from the OPC Foundation website (URL: www.opcfoundation.org):
#include "opcda_i.c" OPC data access interface
#include "opcda.h" OPC data access 2.0 header file
#include "opccomn_i.c" OPC public interface definition
#include "opccomn.h" OPC public header file
4.2 Initialize COM support library
Because OPC is based on COM technology, you must first use the CoInitialize(NULL) function to initialize the COM library before using the interface class. If successful, the function return value is equal to S_ OK.
4.3 Connecting to the OPC Server
The OPC client can connect to the OPC server and establish OPC groups and OPC data items. This is the basis of OPC data access. Without this mechanism, other functions of data access cannot be realized [4]. To connect to the OPC server, the OPC client needs to specify the computer name (if the OPC server and the OPC client are not on the same computer) and the OPC data access server name (RSI.RSView32OPCTagServer) in advance. Implementation code is as follows:
ConnectToServer(/*in */LPOLESTR ProgID,/*in*/ BOOL IsRemote,/*out */ IUnknown **ppUnknown)
{
CLSID OPCCLSID;
HRESULT hRet=CLSIDFromProgID(ProgID,&OPCCLSID);
//Convert string ProgID to unique OPCCLSID
if(IsRemote)
//opc server and opc client are not on the same computer
{
COSERVERINFO ServerInfo;
memset(&ServerInfo,0,sizeof(ServerInfo));
ServerInfo.pwszName=T2OLE("ServerComouter");
MULTI_QI qi[1];
memset(qi, 0, sizeof(qi));
qi[0].pIID=&IID_IUnknown;
HRESULT hRet=CoCreateInstanceEx(OPCCLSID,NULL,CLSCTX_REMOTE_SERVER,
&ServerInfo,1,qi);
*ppUnknown=qi[0].pItf;
}
else
//opc server and opc client are on the same computer
{
hRet=CoCreateInstance(OPCCLSID,NULL,CLSCTX_LOCAL_SERVER,IID_IUnknown,
(void **)ppUnknown);
}
}

4.4 Create OPC Group
The AddGroup() method of the IOPCServer interface can create an OPC group with a specified name and attributes. Before calling this method, you can use the Iunknown interface pointer obtained in the previous step to request the IOPCServer interface pointer through the QueryInterface() method. The code is as follows:
ppUnknown->QueryInterface(IID_IOPCServer,(void **)&pServer);
//Get the IOPCServer interface pointer
pServer->AddGroup(L"",TRUE,500,1235,&lTimeBias,&fTemp,0,&hOPCServerGroup, &dwActualRate,IID_IOPCItemMgt,& pOPCItemMgt);
4.5 Add Data Items
The AddItem() method of the IOPCItemMgt interface can add a specified number of data items with special attributes.
pOPCItemMgt->AddItems(ItemNumber,ItemArray,
(OPCITEMRESULT**)&pItemResult,(HRESULT **)&pErrors);
ItemArray is an OPCTEMDEF type structure array, which contains detailed information about the data items. The client needs to know the name of the data to be exchanged in the RSView32 tag database, the data type, and the RSView32 project name as the OPC server. Before adding data items, the ItemArray structure array must be initialized with these data item information.
4.6 Data Exchange
After successfully adding the required data items, the OPC client (VC application) and the OPC server (RSView32) can exchange data. When the amount of data is not large, the Write() and Read() methods of the IOPCSyncIO synchronization interface can be used to read and write data, thereby realizing data exchange between the OPC client (VC application) and the OPC server (RSView32). The code is as follows:
ppUnknown->QueryInterface(IID_IOPCSyncIO,(void **)&pOPCSync);
//Get the IOPCSyncIO interface pointer
pOPCSync->Read(OPC_DS_CACHE,ReadNumber,hServerRead,&pItemValue,&pErrors);
//Read ReadNumber data
pOPCSync->Write(WriteNumber,hServerWrite,WriteValue,&pErrors);
//Write WriteNumber data
4.7 Release the interface pointer
Before the VC application stops running, the Release() method must be used to delete the created OPC object and release the memory.

5 Conclusion
The OPC technical specification separates hardware suppliers and application software developers, which greatly improves the work efficiency of both parties. Software developers can access the data in the OPC data server without understanding the essence and operation process of the hardware. In particular, when developers use high-level languages ​​such as C++ to develop systems based on process control systems that have used configuration software for real-time monitoring, the complex process of transmitting data from devices in the past has been greatly simplified. In the development of an automatic batching system in an aluminum plant, the application of OPC technology conveniently realized the data exchange between VC applications and RSView32, and indirectly realized the communication between VC applications and PLCs, achieving good results.

Reference address:Using OPC to realize data exchange between VC application program and PLC

Previous article:Application of PLC and frequency conversion speed regulation technology in automatic complete welding center
Next article:Application of Configuration Control in Concrete Mixing Plant System

Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号