OPC学习(二)

某些OPC Server有一些自定接口,需要查看其文档才能正确使用其功能。如KEBA的控制器:“Kemro.opc.4.IF1.1.88“,在创建COM对象获得了IOPCServer接口后,需要再查询一个IKebaOpcConfig接口,然后调用其AddSystem函数添加一台设备:

vServerIntf := CreateComObject(ProgIDToClassID(TOPCServerInfo(ANode.Data).ProgID)) as IOPCServer; 
if vServerIntf = nil then 
begin 
  Application.MessageBox(PChar(S_FailedConnectOPCServer),
    PChar(Application.Title),mb_IconError or mb_OK); 
  Exit; 
end; 
vServerIntf._AddRef; 
ANode.Values[Column_Instance.ItemIndex]:=Integer(vServerIntf); ANode.Values[Column_Status.ItemIndex]:=nsActive; 
if GUIDToString(ProgIDToClassID(TOPCServerInfo(ANode.Data).ProgID))='{B7AF0914-D2C8-4A3B-9939-E607A5509C9F}' then
begin 
  vServerIntf.QueryInterface(IKebaOpcConfig,vConfigIntf); 
  if vConfigIntf<>nil then 
    vConfigIntf.AddSystem(PWideChar(WideString('192.168.181.176')), 
      PWideChar(WideString('192.168.181.176'))); 
end;

这个方法与通过opcsvc.ini来配置设备有差别,只有通过IKebaOpcConfig添加的设备,在展开Item时,才能看到SVs下的资料。

有个朋友使用VB.Net开发OPC Client,出现不稳定的状况,有时正确有时错误:

Private Sub test() 
Try 
  proxyIKebaOpcConfig.AddSystem("192.168.181.176", "192.168.181.176") 
Catch ex As Exception 
  proxyIKebaOpcConfig.RemoveSystem("192.168.181.176") MsgBox("error") 
End 
Try 
  Dim item As New OPCITEMDEFintern() 
  item.hClient = 2 
  item.szAccessPath = "" 
  item.szItemID = "192.168.181.176.SVs.system.sv_MoldFwdVis.Points[1].press" 
  'item.szItemID = "192.168.27.66.SVs.system.do_MotorOff" 
  item.vtRequestedDataType = VarEnum.VT_EMPTY 
  ', Int16) 
  'VarEnum 这个枚举要好好研究一下; 
  item.pBlob = Nothing 
  item.bActive = True 
  item.dwBlobSize = 0 
  item.wReserved = 0 
  Dim count As Int16 = 1 
  Dim nSize As Int32 = Marshal.SizeOf(item) 
  'GetType(OPCITEMDEFintern)) 
  Dim ptrDef As IntPtr = Marshal.AllocCoTaskMem(nSize) 
  Marshal.StructureToPtr(item, ptrDef, True) 
  'item = Nothing 
  'GC.Collect() 
  Dim ptrRes As IntPtr 
  Dim ptrErr As IntPtr 
  Dim hresult As Int32 
  Dim hServer(0) As Int32 
  Dim ptrItemValues As IntPtr 
  Dim ptrItemErrs As IntPtr = IntPtr.Zero 
  Dim timestamp As Int64 
  Dim quality As Int32 
  Dim datavalue As Int32 
  Dim err As Int32 
  hresult = proxyIOPCItemMgt.AddItems(count, ptrDef, ptrRes, ptrErr) 
  Dim erro As Int32 = Marshal.ReadInt32(ptrErr) 
  'Marshal.FreeCoTaskMem(ptrErr) 
  Dim ptrCancleID As Int32 = 0 
  Dim ptrTransactionID(0) As Int32 
  ptrTransactionID(0) = 1 
  If hresult = 0 Then 
    Thread.CurrentThread.Sleep(1000) 
    For i = 0 To count - 1 
      hServer(0) = Marshal.ReadInt32(ptrRes) 
      For j = 0 To 100 
        ptrItemValues = IntPtr.Zero 
        ptrItemErrs = IntPtr.Zero 
        hresult = proxyIOPCSyncIO.Read(OPCDATASOURCE.OPC_DS_DEVICE, 1, hServer, ptrItemValues, ptrItemErrs) 
        ptrTransactionID(0) += 1 
        Dim er As Int32 = Marshal.ReadInt32(ptrItemErrs)

查来查去,发现Marshal.StructureToPtr(item, ptrDef, True)的第3个参数传False就比较正常,但原因不明。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

火星牛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值