OPC客户端通过Interop COM服务器抛出InvalidCastException

时间:2016-07-26 09:07:56

标签: vb.net vb6 interop opc

我们的控制系统提供了一个非常古老的OPC服务器(OPCAutomation)。它是一个COM服务器。我曾编写过一个VB6 OPC cilent ActiveX,在客户端控制台操作系统是Windows XP Pro的过程中运行得非常好。最近,客户端控制台的操作系统更改为Windows 7 Pro(和更新的控制系统软件)。但OPC服务器是旧的(COM服务器DLL)。如果用户控件将嵌入控制系统图形,则控制系统编程环境要求更改为VS 2013。我必须使用.NET重新编写程序来互操作DLL。程序逻辑是一样的。但是行 Group.OPCItems.Add(...)抛出一个InvalidCastException"无法从OPCAutomation.OPCGroupClass转换为OPCAutomation.OPCItems。"。 (组已成功添加到OPCserver中)

VB6对象浏览器中的OPCAutomation如下:

OPCGroup in OPCAutomation.dll shows the default member is OPCItems)

OPCItems has a "add" method

VB.Net程序行如下(几乎与旧的VB6程序相同。删除try块和其他非OPC客户端语句)

Dim svr as OPCServer
Dim Group as OPCGroup
....... (and other declaration)

svr = new OPCServer
svr.Connect(strOPCServer)   ' strOPCServer is the server name
Group = svr.OPCGroup.Add(strGroupName)
Group.IsActive = False
Group.OPCItems.Add(TagNo, strTag, ClientHandles, ServerHandles, Errors1)    ' this line throw InvalidCastException
Group.SyncRead(OPCDataSource.OPCDevice, TagNo, ServerHandles, Values, Errors2)
svr.OPCGroups.RemoveAll()
svr.Disconnect()

........ (other post treatments)

在Interop.OPCAutomation.dll中,它显示OPCItems是IOPCGroup接口的默认属性。并且OPCItems应该返回一个OPCItems指针。但它似乎返回了错误的界面。

我应该如何修改程序让Group.OPCItems.Add不会抛出InvalidCastException?

一个非常有趣的事情是,如果我在VB6中编写.EXE客户端,它的工作原理。但VB.Net或c#失败。这意味着类型库中有问题吗?

(我没有.NET编程经验,很少有VB6经验。对不起我的英语不好。)

0 个答案:

没有答案