本文为OPC实现自定义异步读写代码,接网期OPC实现自动化定义功能
自定义接口异步读写
using
System;
using
System.Collections;
using
System.Runtime.InteropServices;
using
System.Drawing;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Reflection;
using
System.Runtime.CompilerServices;
using
System.Data;
using
OpcRcw.Comn;
//引用
using
OpcRcw.Da;
//引用
namespace
ASync_RW
{
public partial class
Form1
:
Form
,
IOPCDataCallback
{
public
Form1()
{
InitializeComponent();
Control
.CheckForIllegalCrossThreadCalls =
false
;
}
OpcRcw.Da.
IOPCServer
ServerObj;
//OPCServer
OpcRcw.Da.
IOPCAsyncIO2
IOPCAsyncIO2Obj =
null
;
//异步读写对象
OpcRcw.Da.
IOPCGroupStateMgt
IOPCGroupStateMgtObj =
null
;
//组管理对象
IConnectionPointContainer
pIConnectionPointContainer =
null
;
IConnectionPoint
pIConnectionPoint =
null
;
internal const int
LOCALE_ID = 0x407;
Object
MyobjGroup1 =
null
;
int
[] ItemServerHandle;
int
pSvrGroupHandle = 0;
Int32
dwCookie = 0;
private void
Btn_Conn_Click(
object
sender,
EventArgs
e)
{
Type
svrComponenttyp;
Int32
dwRequestedUpdateRate = 1000;
Int32
hClientGroup = 1;
Int32
pRevUpdateRate;
OpcRcw.Da.
OPCITEMDEF
[] ItemArray;
float
deadband = 0;
int
TimeBias = 0;
GCHandle
hTimeBias, hDeadband;
hTimeBias =
GCHandle
.Alloc(TimeBias,
GCHandleType
.Pinned);
hDeadband =
GCHandle
.Alloc(deadband,
GCHandleType
.Pinned);
Guid
iidRequiredInterface =
typeof
(
IOPCItemMgt
).GUID;
try
{
svrComponenttyp =
Type
.GetTypeFromProgID(
"OPC.SimaticNet"
,
"192.168.0.102"
);
//OPCServer
ServerObj = (OpcRcw.Da.
IOPCServer
)
Activator
.CreateInstance(svrComponenttyp);
//注册
try
{
ServerObj.AddGroup(
"MyOPCGroup1"
,
//组对象
0,
dwRequestedUpdateRate,
hClientGroup,
hTimeBias.AddrOfPinnedObject(),
hDeadband.AddrOfPinnedObject(),
LOCALE_ID,
out
pSvrGroupHandle,
out
pRevUpdateRate,
ref
iidRequiredInterface,
out
MyobjGroup1);
IOPCAsyncIO2Obj = (
IOPCAsyncIO2
)MyobjGroup1;
//Query interface for Async calls on group object
IOPCGroupStateMgtObj = (
IOPCGroupStateMgt
)MyobjGroup1;
pIConnectionPointContainer = (
IConnectionPointContainer
)MyobjGroup1;
//定义特定组的异步调用连接
Guid
iid =
typeof
(
IOPCDataCallback
).GUID;
// Establish Callback for all async operations
pIConnectionPointContainer.FindConnectionPoint(
ref
iid,
out
pIConnectionPoint);
// Creates a connection between the OPC servers's connection point and
this client's sink (the callback object)
pIConnectionPoint.Advise(
this
,
out
dwCookie);
ItemArray =
new
OPCITEMDEF
[4];
//定义读写的item,共个变量
ItemArray[0].szAccessPath =
""
;
ItemArray[0].szItemID =
"S7:[S7 connection_1]DB10,INT0"
;
//地址,不同数据类型表示方法不同
ItemArray[0].bActive = 1;
//是否激活
ItemArray[0].hClient = 1;
//表示ID
ItemArray[0].dwBlobSize = 0;
ItemArray[0].pBlob =
IntPtr
.Zero;
ItemArray[0].vtRequestedDataType = 2;
ItemArray[1].szAccessPath =
""
;
ItemArray[1].szItemID =
"S7:[S7 connection_1]DB10,Real4"
;
//地址,不同数据类型表示方法不同
ItemArray[1].bActive = 1;
//是否激活
ItemArray[1].hClient = 2;
//表示ID
ItemArray[1].dwBlobSize = 0;
ItemArray[1].pBlob =
IntPtr
.Zero;
ItemArray[1].vtRequestedDataType = 5;
ItemArray[2].szAccessPath =
""
;
ItemArray[2].szItemID =
"S7:[S7 connection_1]DB10,STRING26.10"
;
//地址,不同数据类型表示方法不同
ItemArray[2].bActive = 1;
//是否激活
ItemArray[2].hClient = 3;
//表示ID
ItemArray[2].dwBlobSize = 0;
ItemArray[2].pBlob =
IntPtr
.Zero;
ItemArray[2].vtRequestedDataType = 8;
IntPtr
pResults =
IntPtr
.Zero;
IntPtr
pErrors =
IntPtr
.Zero;
ItemArray[3].szAccessPath =
""
;
ItemArray[3].szItemID =
"S7:[S7 connection_1]DB10,X12.0"
;
//地址,不同数据类型表示方法不同
ItemArray[3].bActive = 1;
//是否激活
ItemArray[3].hClient = 4;
//表示ID
ItemArray[3].dwBlobSize = 0;
ItemArray[3].pBlob =
IntPtr
.Zero;
ItemArray[3].vtRequestedDataType = 11;
try
{
((OpcRcw.Da.
IOPCItemMgt
)MyobjGroup1).AddItems(4, ItemArray,
out
pResults,
out
pErrors);
int
[] errors =
new int
[4];
IntPtr
pos =