Skip to content
  • There are no suggestions because the search field is empty.

How can I enable an OPC Connection with scripting?

I'd like to enable and disable an OPC Connection through scripting when the value of a tag changes.

Overview

By using the .OnChange method in scripting, you can trigger a method to enable and disable an existing DataHub OPC Connection. The example below monitors the tag "GE.VaersaMaxSE.M0001". When the tag changes, the OPCReconnect.enable method is called. The OPC connection "opc001" is enabled if the tag is "1" and disabled if the tag is "0". This script is included as an attachment below.

Running the Script

Download the attached sample script. In the scripting section of DataHub, click "Add" then browse for the script you downloaded. Edit the script to use your OPC Connection name and Tag name. Save the script and close the script editor. Check the checkbox next to the script and click "apply".

Sample Code:

method OPCReconnect.enable()
{
    local opcclient = new OPCConnection();
    opcclient.setServer("opc001");
    
    if(value == 1)
    {
          opcclient.enable(t);
    }
    else
    {
          opcclient.enable(nil);
    }
}
method OPCReconnect.constructor ()
{
     .OnChange(#$default:GE.VaersaMaxSE.M0001, `(@self).enable ());
}