I’m new to programing so I’m hoping you can help me. Not sure how to formulate the right question so I didn’t find any StackOverflow answers.
When I step through this code and come to OpcServObj.Connect OpcServerStrg it doesn't connect. I get no errors, nothing appears to happen. Cursor jumps to the left margin and does nothing. Only when I resume stepping through the routine it starts the function over again. It should continue and exit the for loop where I plan to test the state of the connection again.
Reference Library: OPC DA Automation Wrapper 2.02
Sub Main()
ConnectOPC
End Sub
Private Function ConnectOPC() As Boolean
Dim OpcServObj As OPCAutomation.OPCServer
Dim OpcGroupObj As OPCAutomation.OPCGroup
Dim OPCServerlist As Variant
Dim OpcServerStrg As String
Set OpcServObj = New OPCAutomation.OPCServer
OPCServerlist = OpcServObj.GetOPCServers
If OpcServObj.ServerState <> True Then
For i = 1 To UBound(OPCServerlist)
OpcServerStrg = OPCServerlist(i)
If OpcServerStrg = "ICONICS.IconicsOPCUAServer.V5" Then
OpcServObj.Connect OpcServerStrg
Exit For
End If
Next i
End If
End Function
Thanks
At the line:
If OpcServerStrg = "ICONICS.IconicsOPCUAServer.V5" Then
if the value in the variable OpcServerStrg
is not equal to "ICONICS.IconicsOPCUAServer.V5"
then the code will loop again to the top of the For
loop and execute OpcServerStrg = OPCServerlist(i)
again. The Exit For
will not be seen.
When you say you step through this code
... is it possible that you have a breakpoint set on the line: OpcServerStrg = OPCServerlist(i)
, and you are clicking run (untill breakpoint) instead of clicking to single-step through the code?
Try setting a breakpoint at the line:
If OpcServerStrg = "ICONICS.IconicsOPCUAServer.V5" Then
then examine the value of OpcServerStrg
when the program reaches the breakpoint and stops.