温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OPC自动化接口编程需要注意哪些问题

发布时间:2022-01-14 09:56:44 来源:亿速云 阅读:164 作者:iii 栏目:互联网科技

这篇文章主要介绍“OPC自动化接口编程需要注意哪些问题”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“OPC自动化接口编程需要注意哪些问题”文章能帮助大家解决问题。

注意事项如下:

(1)用SyncRead(组的同步读)和AsyncRead(组的异步读)读取服务数据

 c#代码原形:

SyncRead (short source,int NumItems,ref Arrray ServerHandles,out Values,outErrors,out Qualities,out

TimeStamps);

AsyncRead(int NumItems,ref Arrray ServerHandles,outErrors,int TransactionID,out int CancelID);

大家可以看出这两个函数里边都有一个ref Arrray ServerHandles,

如果我们在向函数传递参数的时候,

写成:

int[] handle=new int []{item.ServerHandle};

System.Array ServerHandles=(System.Array)handle;

这时候会出现“参数不正确”的错误异常。

应该改成:

int[] handle=new int []{/*函数内部数据下标是从1开始的,所以要把第0位空出来*/0,item.ServerHandle};

System.Array ServerHandles=(System.Array)handle;

OPCDAAuto.dll内部源码:

for (LONG ii = 1; ii <= NumItems; ii++)

{
  LONG hHandle = NULL;
  SafeArrayGetElement(*ppServerHandles, &ii, &hHandle);

  phHandles[ii-1] = hInvalidHandle;
  ppItems[ii-1]   = NULL;

  COPCItem* pItem = LookupItem(hHandle);
  
  if (pItem != NULL)
  {
   phHandles[ii-1] = pItem->GetServerHandle();
   ppItems[ii-1]   = pItem;
  }

}

(2)AsyncRead(组的异步读)读取服务数据

当用异步读取数据的时候,必须把相应的组的IsSubscribed参数设置为true,以订阅状态方式读取数据;当我们用异步方式

读取数据就会有一个异步读取完成的事件:

声明:

group.AsyncReadComplete +=new

OPCAutomation .DIOPCGroupEvent_AsyncReadCompleteEventHandler (AsyncReadComplete);

实现:

private void AsyncReadComplete(int TransactionID, int NumItems, ref System.Array ClientHandles, ref

System.Array ItemValues, ref System.Array Qualities, ref System.Array TimeStamps, ref System.Array

Errors)

{}

关于“OPC自动化接口编程需要注意哪些问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

opc
AI