Problem with OPC OPCGroups VB.NET 2003

G

Thread Starter

Geir Bakke

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe

Additional information: QueryInterface for interface OPCAutomation.IOPCGroups failed.

I'm running this code stops at line marked with ***

Imports OPCAutomation
Imports System.Runtime.InteropServices

Dim WithEvents AnOPCServer As OPCServer
Dim WithEvents ConnectedOPCServer As OPCServer
Dim ConnectedServerGroup As OPCGroup
Dim WithEvents ConnectedGroup As OPCGroup

Dim OPCItemCollection As OPCItems
Dim ItemCount As Long
Dim OPCItemIDs(10) As String
Dim ItemServerHandles() As Long
Dim ItemServerErrors() As Long
Dim ClientHandles(10) As Long

'Create a new OPC Server object
ConnectedOPCServer = New OPCServer

'Attempt to connect with the server (Local only in this example)
ConnectedOPCServer.Connect(Servername.Text, NodeName.Text)

'Prepare to add a group to the current OPC Server
' Get the group interface from the server object

*** ConnectedServerGroup = CType Marshal.CreateWrapperOfType(ConnectedOPCServer.OPCGroups, GetType(OPCGroupsClass)), OPCGroupsClass)

'i have allso tryed this way to connect the group interface!

ConnectedServerGroup = ConnectedOPCServer.OPCGroups()
 
M
Hi Geir,

This means that the interface that you are requesting is not supported by the instance of the object that you have available. It is likely that one of the parameters in one of the interface methods is not, by default, being marshaled as expected. This is why, at this time, most .NET to OPC interfaces require that you write your own marshalling code to the OPC custom interface. By default, a lot of things work but some things don't. Contact me "off-list" and I may be able to help you.

[email protected]
 
Mark,

Is this a general problem with .NET to 'Automation' (ie IDispatch) interfaces or just an OPC thing?

For example if I had a VB6 program that manipulated an Excel spreadsheet, would the same techniques work in VB .NET?

Hugh
 
M
Hugh,

No, in theory it should work but in practice sometimes the default marshaling makes the wrong assumption about how to marshal a type. For example C-type arrays may be marshaled as safe-arrays, which wont work. I have seen this problem with this specific OPC interface but I haven't needed to figure it out because I do my own marshaling so as to avoid this. I suspect a small issue with the OPC automation interface being used and perhaps the latest version fixes it. As a general rule, if the COM interface was written for VB (automation) then referencing the automation interface should work with .NET with no problem.

Mark
 
M

Marcio Callejon


I'm having the same problem with the OPC Automation when I use vb.net 2003. I checked that any access to OPCGroups interface raise a error of Query_Interface. I checked too that when I'm in debbuging and execute a access to OPCGroups using immediate window, the interface OPCGroups works fine! I'm thinking that's a bug of OPC Automation.

Marcio

 
Hello Marcio,

You are thinking right. The OPC Automation interface is not resolving to what is expected. I know some have solved this by wrapping the
OPC interface as an ActiveX and using that without changing much of their existing code. (Use AxImp.exe)

If you are an OPC member, they have a new .NET "API" download available.

If you are working on new code you may want to try our free (LIO) evaluation at IndustrialDotNet.com. It is very easy to use and it works. It would take you abut 10 minutes to try it out.

Good luck,

[email protected]
 
M

Marcio Callejon

Mark,

I've already downloaded the new OPC Automation from OPC Foundation and It works fine. But that's a beta version yet. I checked that the new automation was rebuild using Visual C++ .Net version to be compatible with VB.net and C#.net, but probably the interface (tlb, idl) from the old version was wrong.

I found a error that when we execute the method "Disconnect" from OPC Server object, the OLE Server doesn't close correctly, but I don't know If it's a error from automation or OPC Server!

Thanks,
Marcio
 
Hi Marcio,

I have not heard of the Disconnect issue (which doesn't mean it doesn't exist). I wrote our interop in C# to the OPC custom interface, so we don't have automation issues. I would suggest posting the question to the OPC Foundation forum (www.opcfoundation.org/ under the support menu). You don't need to be a member to post questions and they are normally very responsive to technical posts. Also, I would suggest getting one or more of the free server simulators that are provided by various companies and see if they also have the Disconnect problem as some servers can act differently. There is a handy list of links to simulators at www.opcconnect.com under the "free stuff" listing.

Good luck!

[email protected]
 
This won't end your problems but try the KEPWARE site. Download the simple Kepware client 'Simple VB OPC Automation Example' & convert to vb.net. The conversion works(ish). But the code will be sufficient to demonstrate a way for you to surmount server connection difficulties. You may wish to download and facilitate the Kepware server (2 hour runtime). Good for Local connections and troubleshooting.

But like the handle says, 'new2this' so don't treat wat i say as Gospel.
 
P

Pascal Brunot

Just update your OPCDAAuto.dll in c:\windows\system32 to one more recent version. You can download one on the OPC Foundation website, I think.

Mine is 2.2.5 and it works perfectly. Before I upgraded the DLL, I had the same problems you're experiencing.

Hope it helps
Pascal.
 
Top