How to write OPC client programs with VB

xiaoxiao2021-03-06  57

I. Introduction

In the field of industrial automation, computers have long become an essential tool, and the continuous development of computer technology has greatly accelerated advances in industrial automation technology, and various industrial control applications are the most important implementation of this process. tool.

In the past, application software developers have to develop drivers for each hardware. Due to the variety of hardware, different feature, software developers are extremely heavy, especially if the hardware features have changed, the entire application software It is also necessary to modify accordingly, this is very disadvantageous to the software developer. And because the drivers are not uniform, different applications often have conflicts when accessing the same hardware device. OPC (OLE for Process Control) Technical standards are produced in this case. OPC is based on Microsoft's OLE, COM and DCOM technology, and it is a special COM, which is also because of Microsoft's participation, and it is based on a mature technology, it is more efficient than the general industrial standards. It starts running from the beginning to the first operational specification, only less than a year.

Second, OPC principle and application

OPC technology provides a unified standard for object-oriented development of industrial automation software. It greatly reduces the burden on the software developer, software developers don't have to write drivers for each hardware, as long as the hardware features meet the unified OPC interface program standard, or hardware manufacturers provide OPC servers, as shown in the figure, Different application software developers can use OPC standard design industrial control software to access field data through the OPC server with a unified interface of standards.

Application 1

Application 2

OPC server a

OPC server B

OPC server C

Equipment A1

Equipment A2

Device B1

......

Thus, when the field device changes or add new devices in the system, the OPC server needs to re-implement the server interface to accommodate the change of the hardware, but due to the consistency of the interface provided by the server, the indentation software does not change Use, just in some cases, it may be necessary to re-configure (such as adding new PLC sites, etc.), so that software developers can save a lot of time to improve the performance of industrial control software, do not have to consider hardware changes The impact, greatly reduced the workload of software maintenance. This is just like the OPC specification, OPC clears the boundaries and hardware devices.

2.1 OPC basic structure

The OPC server has two types of interfaces: Custom Interface, Automation Interface, Custom Interface is relatively low, it provides more features, efficiency is higher than the latter, can be used to call such interfaces with C language, automation The interface is mainly used for development tools such as VB, Delphi. According to the OPC specification, the custom interface is the service provider must provide, and the automation interface is optional. However, the OPC Foundation (International Organization for Management OPC) provides a dynamic connection library called "Automated Packurber", with Conversion between the two. As shown in Figure 2:

OPC custom interface

OPC automation interface

OPC automation

Wrapper

C program

OPC server

VB program

Physical equipment

Figure 2 OPC typical structure

In the early specification of OPC, it mainly includes OPC data access specifications, OPC alarms, and events, OPC history data access specifications. OPC data access specification details the mechanism for data communication, other types of OPC servers, other types of OPC servers are often used by increasing the object, and the specification is also other OPC. The basis of the specification. The basic objects specified by the OPC Data Access specification have three categories: OPC Server, OPC Group, and OPC Item, OPC Server contains all information about the server, but also the container of OPC Group, which is responsible for managing OPC ITEM in addition to its own information. Their structure is shown in Figure 3. Each OPC Item represents a connection to the data source, but it does not provide an external interface, the client program cannot operate directly to the OPC Item, the application must rely on OPC Item's container OPC Group to operate it, this is below There will be specifically described in the program. IOPCServer

IConnectionPointContainer

IOPCCMMON

IOPCBROWSE

IOPCITEMIO

Iunknown

OPC Server

IopcgroupStatemgt

IOPCITEMMGT

IConnectionPointContainer

IopcitemdeadbandMGT

IOPCSYNCIO

Iopcasyncio2

IOPCSYNCIO2

IopcgroupStatemgt2

Iopcasyncio3

OPC Group

Iunknown

Figure 3 Standard OPC Server and OPC Group Object (DA 3.0)

2.2 OPC Data Access Method

The OPC Customer is divided into a synchronous read and write mode and asynchronous reading. The client program can simultaneize the server program in accordance with the IOPCSYNCIO interface of the OPC Group object in accordance with a certain period. At this time, the customer-party call function is running to all data read and write completion, and then can perform other operations, so this The method is suitable for reading a small amount of data. If the data is more, the system will be in a fake state and cannot be operated. IOPCSYNCIO2 is from 3.0, is an enhancement to IOPCSYNCIO. IOPCASYNCIO2 and IOPCASYNCIO3 are interfaces used in asynchronous mode, when the client is accessed asynchronously, immediately return to the OPC application to perform other operations, no need to wait, notify the OPC application when the OPC server completes the data read The application is to obtain data. The former is newly defined in version 2.0, with high communication performance; the latter is just in version 3.0, similar to IOPCSYNCIO2, IOPCASYNCIO3 is an enhancement to IOPCASYNCIO2. In an asynchronous mode, after the server program receives a read request, call the client's IOPCDATACALLBACK interface to send the data to the client. In asynchronous mode, the server allows the server to queue the read-write operation, allowing the client's call function to return immediately, and notify the client after the server reads and write operations. Obviously, the communication efficiency of asynchronous notification is higher, and this method is also the way to discuss this article, but there are multiple client programs to be connected to the server, and the synchronous read and write mode is more timeliness. For each group object, the client program can use one of the data access modes as needed without both.

Asynchronous reading has a special way, it is called subscription mode, and the application does not need to send a read request. When the data is periodically updated, if there is a certain change in the discovery data, it automatically supplies the application. Data issued and transmitted.

2.3 Write an OPC client application VB simple and practical, is a relatively ideal OPC application rapid development tool. To develop OPC applications with VB, you must use an OPC automation packager, which has been mentioned above, this packaging The supplier of the OPC server is provided in a DLL form. The following is a case as an example provided by SIEMENS, which describes how to develop OPC applications, which is the actual application of the author in the recent project, this is based on DA2 .0 version.

First, after the new VB project is created, the reference shown in Figure 4:

Figure 4 VB Reference Options Settings

2.3.1 Establish an OPC object

First, the OPC object is applied:

Option Base 1

DIM WITHEVENTS ServerObj AS OPCServer 'OPC Server object, Connection OPC Server

DIM GroupSobj As Opcgroups' OPC Groups object, add OPC group

DIM WITHEVENTS GroupObj As OpcGroup 'OPC Group Object

DIM Itemsobj As Opcitems' OPC ITEM Collection

DIM ServerHandles () AS Long 'Server OPC Item Handle

DIM ClientHandles () AS Long 'Handle of Client OPC Item

DIM ITEMID (2) AS STRING

Dim Errors () as long

Next, generate each object:

IF serverobj is nothing the set serverobj = new opcserver

'Connection OPC server

IF serverobj.serverstate = opcdisconnected then

ServerObj.connect ("opc.simaticnet") 'assumes that the OPC server is running in this machine

END IF

If groupsobj is nothing the set groupsobj = serverobj.opcgroups

IF groupobj is nothing the set groupobj = groupsobj.add

If itemsobj is nothing the set itemsobj = groupobj.opcitems

Groupobj.isactive = true 'Settings Group is active

'Suppose there are two data sources, one is an 8-bit switch input, one is an 8-bit switch output

ItemID (1) = "S7: [S7 connection_1] ib0"

ItemID (2) = "S7: [S7 connection_1] qb0"

ClientHandles (1) = 1

ClientHandles (2) = 2

'Add group project, the value of the ServerHandles array is a server handle of each OPC Item.

The value of the 'clientHandles array is the client handle of each OPC Item, and is set by the application.

Call itemsobj.Additems (2, itemid, clienthandles, serverhandles, errors)

2.3.2 Reading of asynchronous data

'OPC ITEM server handle, add OPC ITEM to allocate by server

DIM TEMPSERVERHANDLES (1) as long

'Transaction marker, generated by the client, the information it contains is provided to the OnReadcomplete event DIM TransactionID As Long

'Cancel the flag, the server is generated, when the operation needs to be canceled

DIM CANCELID AS Long

'Contains information returned when reading each OPC ITEM

Dim Errornr () as long

TempServerHandles (1) = ServerHandles (1) 'corresponds to the first OPC Item

Groupobj.asyncRead 1, TempServerHandles, Errornr, TransactionId, Cancelid

The first parameter is the number of OPC Items to read, which only contains an OPC Item. The result of the read is a transaction program for the IOPCDataCallback interface with the IConnectionPointContainer interface:

Private sub groupobj_asyncreadcomplete (Byval Numitems As Long, ClientHandles () As long, itemvalues ​​() As Variant, Qualities () As long, timestamps () AS DATE, ERRORS () AS Long

The clienthandles in the parameter clienthandles and additems methods are corresponding to determine which OPC Item is read. The description of the other parameters is as follows:

TransactionID: Client free use, application developer custom;

NumItems: Indicates the number of OPC Items read;

ItemVALUES (): Value of the data sources connected to each OPC ITEM, type is Variant;

Qualities (): OPC ITEM quality value;

Timestamps (): Timestamp;

Errors (): Record the information returned by the server.

2.3.3 asynchronous data writing

DIM TEMPSERVERHANDLES (1) as long

DIM VVALUE (1) as variant

Dim Errornr () as long

DIM TransactionID as long

DIM CANCELID AS Long

TempServerHandles (1) = ServerHandles (2)

Vvalue (1) = 1 'Suppose you want to write "1" to the clientHandle 2 OPC Item

Groupobj.asyncWrite 1, TempServerHandles, VValue, Errornr, TransactionID, Cancelid, asyncwrite also corresponds to a transaction program:

Private sub groupobj_asyncwritecomplete (Byval NumItems as long, ClientHandles () As long, errors () as long

The meaning of its parameters is similar to the meaning of AsuncReadComplete, which generally needs to process the return state after writing data, and details are not described here.

2.3.4 Disconnecting the connection with the server

Itemsobj.Remove Itemsobj.count, ServerHandles, Errors' Clear OPC Item

SET ITEMSOBJ = Nothing 'Release Resources, the following IF not groupobj is nothing the

Groupsobj.remove groupobj.serverhandle 'delete group

END IF

IF not groupsobj is nothing then

Set groupsobj = Nothing

END IF

IF not serverobj is nothing then

IF serverobj.serverstate <> opcdisconnected then

ServerObj.disconnect 'Disconnects to the server

END IF

SET ServerObj = Nothing

END IF three, end language

Due to the continuous development of OPC technology, the connection of field equipment and system software is more convenient and flexible, and the application and upgrade is also simpler. It is because of the significant superiority of OPC, more and more industrial hardware manufacturers have begun to support OPC. In recent years, in addition to some of the previous well-known big companies, such as ABB, Siemens, many companies have joined the OPC Foundation, with The widespread use of OPC specification, and the entire automation can be developed faster.

*********** Learn a little summary after OPC, if there is something wrong, more advice **********

****************** 2004-9-26, please clearly and copyright ****************

转载请注明原文地址:https://www.9cbs.com/read-112879.html

New Post(0)