概述
某些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就比较正常,但原因不明。
最后
以上就是想人陪鲜花为你收集整理的OPC学习(二)的全部内容,希望文章能够帮你解决OPC学习(二)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复