How to develop OPC Server

zhaozj2021-02-11  213

How to develop OPC Server

First let's take a look at what is OPC.

OLE for process control is an industrial standard interface based on Microsoft's DNA (Distributed Internet Application) architecture and COM (Component Object Model) technology, which is designed according to easy scalability.

Let's take a look at the use of OPC.

OPC is mainly suitable for application fields such as process control and manufacturing automation. OPC is a communication standard for an OLE / COM mechanism as an application. OLE / COM is a customer / server mode with the advantages of language independence, code reuse, and easy integration. The OPC specifies the interface function, regardless of the form of on-site equipment, customers go to the unified approach, so that the software's transparency to customers, so that users are completely detached from low-level development.

Then let's take a look at the composition of OPC Server

The OPC Server of a device mainly has two components, one is the implementation of the OPC standard interface; the second is the communication module of the hardware device.

Implement OPC standard interface

[figure 1]

In these interfaces, IOPCServer is the primary interface of the OPC Server, which implements installation and registration in OPC Server in the operating system. This interface must be implemented, all of which must also be implemented. Other interfaces are optional, we don't make an introduction, the following mainly introduces how to implement the IOPCServer interface.

There are six laws in the IOPCServer interface:

1, IOPCSERVER :: AddGroup

HRESULT AddGroup ([In, String] lpcwstr szname,

[in] BOOL BACTIVE,

DWORD DWREQUESTEDUPDATERATE,

Opchandle HclientGroup,

[unique, in] long * ptimebias,

[in] float * ppercentdeadband,

DWORD DWLCID,

[OUT] opchandle * phservergroup,

[OUT] DWORD * PREVISEDUPDATERATE,

Refiid Riid,

[OUT, IID_IS (RIID)] LPUNKNOWN * PPUNK);

This method is to create a group on the OPC Server. Under our way to implement this method:

.

.

First check the group name (SZNAME) to see if it is valid or if this group is already.

IF (szname! = NULL)

{

RequestedName = SZNAME;

IF (RequestedName == "")

RequestedName = psvrobject-> defaultgroupname ();

}

Else

RequestedName = psvrobject-> defaultgroupname ();

For (i = 0; i numbrgroups (); i )

{

PGROUP = psvrobject-> getGroup (i);

IF (RequestedName == PGROUP-> NAME)

Return (OPC_E_Duplicatename);

}

This requires the list of OPC Groups (groups) to maintain the list of OPC Groups (list of OPC items).

If the SZNAME is correct and has not been established, the group is set according to the parameters passed, and the group will be added to the list of its own group to prepare.

Finally, return the new group interface pointer to the client. 2, IOPCSERVER :: getRrorstring

HRESULT GETERRORSTRING ([in] HRESULT DWERROR,

[in] LCID DWLOCALE,

[OUT, STRING] LPWSTR * PPSTRING);

Returns the corresponding error string for the error code for Server.

Code slightly

3, IOPCSERVER :: getGroupbyname

HRESULT GETGROUPBYNAME ([IN, String] lpcwstr szname,

Refiid Riid,

[OUT, IID_IS (RIID)] LPUNKNOWN * PPUNK);

The interface pointer of the group is found by the specified group name (established by the same client).

This method is relatively simple, as long as the interface pointer of the group is found in the group list according to the provided name loop, and return to the client

4,

IOPCSERVER :: getStatus

HRESULT GETSTATUS ([OUT] OPCSERVERSTATUS ** PPSERVERSTATUS;

Returns the status information of the current Server.

This method is relatively simple, but to note that memory allocation is performed before using OPCSerVersTaus.

5,

IOPCSERVER :: RemoveGroup

HRESULT REMOVEGROUP ([in] opchandle hservergroup,

"in] bool bforte);

Remove the specified group from the server

Find the specified group in the group list and delete it.

6, IOPCSERVER :: CreateGroupENUMERATOR

HRESULT CREATEGROUPENUMERATOR ([in] opcenumscope dwscope,

Refiid Riid,

[OUT, IID_IS (RIID)] LPUNKNOWN * PPUNK);

Set different enumerations for the group provided on Server.

The above is developed directly using COM technology, which requires you to be familiar with COM technology. If you don't matter to COM, you have no relationship. You can choose OPC Server development tools. You only need to simply call the function of the development tool to implement all interfaces in the OPC Server.

Although we implemented all the methods in the IOPCServer interface, it is just a bridge with our communications, the most important thing is to maintain the OPC Group and OPC Item list. This allows us to real data communication with the OPC Client.

Communication with hardware devices

The interface has been implemented, this time we will read data from the hardware device to the OPC Client.

There are many ways with hardware devices. If you are a device's manufacturer, you can do direct data directly; you can also operate through the API provided by the device driver and hardware vendors or by TCP, serial port. This is to see what the interface provided by hardware devices and software communications is.

No matter what method you use, just use the data from the hardware device and associate with the OPC item, you can implement OPC Server.

Here we we do an OPC Server as the RH2000 system of Tsinghua Tongfang (Figure 2).

[figure 2]

Because the RH2000 system is regulated by a software system called Easy, it controls multiple lower aircraft (hardware devices). We only need to communicate with easy-to-face, you can implement the operation of hardware devices (easy to provide TCP / IP communication mode). First, the SOCKET connection is established, then send the control command to read the information of the hardware device connected to the vision (also device point information). Based on the corresponding OPC items according to the different devices read, the OPC Client can read to the device information as long as the OPC item is read. If the OPC Client changes to the OPC item, we will operate the device to the vision of the OPC item, so that the OPC Client is controlled to the hardware device. Conclude

Although the use of development tools can also develop OPC Server, but I hope that you can learn more about COM technology, which is good for OPC Server extension and maintenance. And get to the official website of the OPC Foundation to learn about the latest knowledge of OPC and download relevant information, the URL is http://www.opcfoundation.org.

Due to the limitations of time and the author level, it is inevitable that there is a mistake and improper, please ask everyone to criticize and correct. My email is yanghongtao@thtf.com.cn. Finally, I thank Tsinghua Limited Co., Ltd.

Zhou Hongbo

Ph.D. and Lu Sheng's manager have developed and learned OPC opportunities to make me understand OPC and write this article.

2004-2-11

Hotyoung

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

New Post(0)