Share this

Using OPC to achieve data exchange between VC application and PLC

2026-04-06 07:15:24 · · #1

1 Introduction

VC (Visual C++) is a powerful visual software development tool for Windows applications. VC supports object-oriented design methods and can use the powerful Microsoft Foundation Class Library (MFC). Due to Microsoft's monopoly in the operating market, software developed with VC is stable, portable, and independent of hardware [1], and can be used to develop upper-level management systems for control systems. RSView32 is a configuration software specifically for industrial control. It not only includes a large number of graphical development tools and ready-made graphics libraries, enabling users to easily develop systems, but also allows configuration of alarms, activity records, events, historical trends, etc. It is a powerful industrial automation product [2], and therefore can easily configure lower-level devices. In actual system development, 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.

2OPC Introduction

OPC (OLE for Process Control) is an open and interoperable user interface standard developed based on the functional requirements of Microsoft's OLE (now Active), COM (Component Object Model), and DCOM (Distributed Component Object Model) technologies. It ensures interoperability between automation/control applications and regional systems/devices. It uses the OLE/COM mechanism as the application-level communication standard and adopts a CLIENT/SERVER model. A typical OPC architecture is shown in Figure 1 .

Figure 1 Typical OPC architecture

The OPC specification provides two interface schemes: the custom interface and the automation interface. The custom interface is highly efficient and can maximize the performance of the OPC server; clients using C++ typically use the custom interface. The automation interface enables interpreted languages ​​and macro languages ​​to access the OPC server; clients using languages ​​such as VB typically use the automation interface.

An OPC data access server consists of three types of objects : Server, Group, and Item. The Server object specifies the name of the OPC server application and acts as a container for Group objects. The Group object stores Group information composed of several Items and logically organizes the data items. The Item object stores the definition, data value, status value, and other information of the specific Item; each Item represents a specific process variable. For an OPC client application to access data from the OPC server, it must first specify the computer name where the server application resides (the server application and client application are not on the same PC), the OPC data access server name, and the definition of the OPC items provided by that 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 applications that only read a small amount of data; using the "subscription" function OnChange of the interface IOPCCallback, where the server automatically notifies the client whenever the data changes; and using the asynchronous interface IOPCASyncIO2, which can communicate directly with the physical device, is slower but has high data accuracy.

3RSView32 as an OPC server

Siemens' RSView32, a general-purpose configuration software specifically designed 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 OPC-enabled software. In this paper, RSView32 is used as the server, and the VC application as the client, employing a client/server (C/S) model to achieve data exchange between them.

3.1 Using RSView32 as an OPC server

Use one of the following methods to make RSView32 an OPC server :

(1) Select the "OPC/DDE Server" checkbox on the "Startup" page of the "Startup" editor;

(2) Issue the RTDataServerOn command (this function can be canceled using the RTDataServerOff command from the command line or another RSView32 component). This will allow other applications to read the value but not change it;

(3) Issue the RTDataWriteEnable command (this function can be disabled from the command line or from another RSView32 component using the RTDataWriteDisable command), which allows writing from an external OPC application to change the tag value of RSView32.

3.2 Establish OPC customer projects

To retrieve data from RSView32, a VC application must use the following information :

Server : RSI.RSView32OPCTagServer ;

Type : Local/Remote;

Server computer name or address : This can be left blank if the client and server are on the same computer.

Access path : project name;

Update rate : a rate measured in seconds;

Entry : Tag name. This can be obtained by viewing the RSView32 tag database.

4VC application as an OPC client program implementation

The following are the key steps in developing an OPC client application using a custom interface in a VC environment.

4.1 Include the OPC header file

Developing OPC client applications requires not only an OPC interface but also the inclusion of OPC standard library files in the program. These files can be downloaded from the OPC Foundation website ( 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 the COM support library

Since OPC is based on COM technology, the COM library must be initialized using the CoInitialize(NULL) function before using the interface class. If successful, the function returns S_OK.

.

4.3 Connecting to the OPC server

OPC clients can connect to the OPC server and establish OPC groups and OPC data items, which is the basis of OPC data access. Without this mechanism, other data access functions 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. The implementation code is as follows :

ConnectToServer(/*in*/LPOLESTRProgID , /*in*/BOOLIsRemote , /*out*/IUnknown**ppUnknown)

{

CLSIDOPCCLSID;

HRESULThRet=CLSIDFromProgID(ProgID , &OPCCLSID);

// Convert the string ProgID to a unique OPCCLSID

if(IsRemote)

//The OPC server and OPC client are not on the same computer.

{

COSERVERINFOServerInfo;

memset(&ServerInfo , 0 , sizeof(ServerInfo));

ServerInfo . pwszName=T2OLE("ServerComouter");

MULTI_QIqi[1];

memset(qi , 0 , sizeof(qi));

qi[0] . pIID=&IID_IUnknown;

HRESULThRet=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 Creating an 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 via 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 array of OPCITEMDEF type structures that contains detailed information about the data items. Clients need to know the name, data type, and RSView32 item name in the RSView32 tag database for the data to be exchanged, as well as the name of the RSView32 item acting as the OPC server. Before adding data items, the ItemArray structure array must be initialized with this data item information.

4.6 Data Exchange

After successfully adding the required data items, the OPC client (VC application) and OPC server (RSView32) can exchange data. For small amounts of data, the Write() and Read() methods of the IOPCSyncIO synchronization interface can be used for data reading and writing operations, thus enabling data exchange between the OPC client (VC application) and 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);

//ReadNumber data entries

pOPCSync->Write(WriteNumber , hServerWrite , WriteValue , &pErrors);

// WriteNumber data

4.7 Release the interface pointer

Before a VC application stops running, the Release() method must be used to delete the created OPC objects and release the memory.

5. Conclusion

The OPC technical specification separates hardware vendors and application software developers, significantly improving the efficiency of both parties. Software developers can access data in the OPC data server without needing to understand the hardware's details or operational processes. This is particularly beneficial when developers are using high-level languages ​​like C++ to develop systems based on process control systems that already utilize configuration software for real-time monitoring. It greatly simplifies the previously complex process of transferring data from equipment. In the development of an automated batching system at an aluminum plant, OPC technology facilitated data exchange between the VC application and RSView32, indirectly enabling communication between the VC application and the PLC, yielding excellent results.

Read next

CATDOLL Sabrina Hybrid Silicone Head

The hybrid silicone head is crafted using a soft silicone base combined with a reinforced scalp section, allowing durab...

Articles 2026-02-22
CATDOLL 136CM Mila

CATDOLL 136CM Mila

Articles
2026-02-22
CATDOLL Tami Hybrid Silicone Head

CATDOLL Tami Hybrid Silicone Head

Articles
2026-02-22