vbscriptjscriptwsh

Windows Script Host - JScript and VBScript with COM Object


Stuggling here...

VBScript

I have a DLL loaded, and can create an Object;

Set objServer = CreateObject("Matrikon.OPC.Automation.1")

I can then create a new Object, based on a Property of the objServer (I think I am saying that right?)

Set objGroups = objServer.OPCGroups
Set objGroup = objGroups.Add("Group001")

The manual shows OPCGroups is a Property of OPCServer. Once I have called this Property, I am left with a new Object of type OPCGroups, that I can then call its Properties and Methods.

Syntax  OPCGroups As OPCGroups

This works fine. I then continue, and get stuck when calling this function;

Syntax  AddItems (Count As Long, ItemIDs() As String, ClientHandles() As Long, ByRef  ServerHandles() As Long, ByRef Errors() As Long, Optional RequestedDataTypes As Variant, Optional AccessPaths As Variant)

It wants an Array of Strings. However, in VBScript, I always end up with an Array of Variants (VarType = 8204). When I try and pass my array, I get a Type Mismatch error. I can't find a way of strongly typing it to an Array of Strings; I'm not even sure it's possible.

Over to JScript

JScript

I perform the same first steps as above, but when I create the OPCGroups object;

var objGroups = objServer.OPCGroups;

Something hasn't worked. It hasn't actually created an OPCGroups object with all its associated Properties/Methods. When I try and call a Method

var objGroup = objGroups.Add();

It says the 'Object doesn't support this property or method'. It's as though it just created a generic/blank Object; not one typed as an OPCGroups object.

So. Can I workaround either of these issues?


Solution

  • Just cleaning this up.

    I used PowerShell - amazing how 3 years ago I knew nothing about PowerShell. Using newer .NET OPC APIs, it was much simpler;

    Load DLLs

    $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
    # Namespaces Opc, Opc.Ae, Opc.Da, Opc.Hda
    Add-Type -Path ($PSScriptRoot + "\OpcNetApi.dll")
    # Namespaces OpcCom, OpcCom.Ae, OpcCom.Da, OpcCom.Hda, OpcCom.Da20 and OpcCom.Da.Wrapper - the same namespace as the original Interop.OPCAutomation.dll
    Add-Type -Path ($PSScriptRoot + "\OpcNetApi.Com.dll")
    

    Make an array of tags as strings, and then convert to Opc.Da.Items[]

    # Convert array of strings into an Array of Opc.Da.Items[]
    $masterItemsList = $tagList | ForEach {New-Object "Opc.DA.Item" ([String]$_)}
    

    Connect to OPC Server

    $opcFactory = New-Object "OpcCom.Factory"
    # Constructor is Opc.Da.Server(OPC.Factory factory, Opc.URL url). Leave URL $null - apply it in the connect method
    $opcServer = New-Object "Opc.DA.Server" ($opcFactory, $null)
    
    $opcURL = New-Object "Opc.URL" ("opcda://" + $serverName + "/" + $progID)
    # Method is void Connect(Opc.URL url, new Opc.ConnectData(new System.Net.NetworkCredential())). connectData can supply Windows Credentials
    $opcServer.Connect($opcURL, $null)
    

    No need to create an OPC Group; it is done automatically.

    $opcResults = $opcServer.Read([Opc.Da.ItemValueResult[]]$masterItemsList)
    

    This doesn't really solve the VBS/JScript issue, but I hope someone finds it helpful.