OPC(OLE for Process Control) is actually a set of standards. I don't know much about this standard, so I understand it as a set of protocols or specifications in the process of use, which are mainly used in the field of industrial control. There are many OPC specifications, and I mainly use OPC DA specifications to read and write data. There are other specifications, such as OPC UA and OPC HDA. If you are an OPC server developer, check this information. This blog post mainly introduces the knowledge of OPC client development.
When using OPC DA to read and write clients, we use custom interfaces and automation interfaces. The following are three key objects involved in developing a custom interface: OpcServer, OpcGroup and OpcItem. The following figure shows the logical relationship between them:
When developing the client, it is necessary to use OpcServer object to realize the connection between the client and Opc server. There are multiple opcgroups under an OpcServer object and multiple OpcItem under an OpcGroup. Client development under the user-defined interface is based on group operation, and data reading and writing are carried out through opcgroup.
Build a program running environment
Software and hardware environment required for program operation:
. Net framework 4.0
Simatic Net 2008 (or other) HF 1
Siemens 300 (or other) PLC
We can realize the remote connection of OPC through the configuration of this machine. I didn't use this method. First, this configuration is troublesome, but this method is unstable. So I used this machine to install an OPCServer to realize the interaction with PLC.
For OPCServer software, I chose SimaticNet 2008 HF 1 (I will have the option to install SimaticNet when installing WinCC). There is no special reason, but I am familiar with it. PLC belongs to Siemens.
We can test how to interact with PLC through OPCServer without writing OPC client program. First, after we install SimaticNet, we need to configure the station configuration editor as follows:
First, we need to specify the station name. The picture above is called PCStation. Click the station name below to change it. Next, select an OPCServer on stack 1 and a communication network card on stack 3.
Next, we need to establish the connection between the station configuration editor and PLC in Step 7, which is temporarily called configuration. In the process of configuration, to establish Opc Server and IE General (with the same stack number) corresponding to the station configuration editor, the station configuration editor plays a bridge role, mainly to establish an S7 connection between PLC and Opc Server. I haven't got the configuration diagram for the time being, so I'll make it up later.
When we finish the configuration, how can we judge whether the configuration is correct? There is a software called Opc Scout(Opc Scout V 10) in the directory of SimaticNet. Open the following picture:
The above figure lists all the servers in this machine. We can use the server named OPC.SimaticNET. Double-click this server to add a group, and double-click this server several times to add multiple groups, which verifies the relationship between the server and the group in the above figure.
We double-click the newly created group to enter the following interface:
The above figure lists all the connections. The S7 connection established in the above configuration can be seen in the S7 node. Expand this node to see the S7 connection we have established, as shown in the following figure:
The above figure lists the S7 connection named S7 connection_ 1, expands the Object object, and lists the structure of PLC. We choose one to build our new item, because I don't have a PLC module here, so I can't take a screenshot for everyone to see.
At this point, the running environment of our OPC client has been built.
Write OPC client program.
We need to use the custom interface provided by OPC Foundation for development. In Visual Studio, we refer to two DLLs named OpcRcw.Comn.dll and OpcRcw.Da.dll.
We define a class named OpcDaCustomAsync, and let this class inherit from: IOPCDataCallback, IDisposable.
Reprinted, for reference only, have a nice day, please adopt it if you are satisfied.