OPC自定义接口异步读写

本文介绍了如何使用C#编程语言实现OPC自定义的异步读写接口,通过创建OPCServer对象,设置异步读写对象和组管理对象,实现在OPC Server中进行异步读取和写入操作。详细代码展示了如何建立与OPC Server的连接,添加自定义项,执行异步读取和写入,并处理异步读写完成后的回调方法。
摘要由CSDN通过智能技术生成
本文为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 =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A_nanda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值