OPC Plugin

The DolphinDB OPC plugin has the branches release 200 and release130. Each plugin version corresponds to a DolphinDB server version. You're looking at the plugin documentation for release200. If you use a different DolphinDB server version, please refer to the corresponding branch of the plugin documentation.

The OPC Data Access(OPC DA) plugin for DolphinDB has implemented the OPC DA 2.05a Specification, which can be used to access and collect data from OPC servers.

Install the Plugin

You can install the plugin by downloading precompiled binaries for DolphinDB OPC Plugin or by manually compiling the project.

Install OPC Core Components

The plugin relies on the OPC Core Components Redistributable 3.0.106 and above. Please install the components before installing the plugin. You can download the zip package of OPC Core Components Redistributable from the bin directory. Unzip the package and double-click the msi file to install it.

Download Precompiled Binaries

You can load the precompiled binaries under the bin directory by executing the following command in DolphinDB (supposing it is installed on C drive:

loadPlugin("C:/path/to/opc/PluginOpc.txt")

Note that you must load the plugin with an absolute path and replace "\" with "\\" or "/".

Build a Plugin

Install CMake. It is a popular project build tool that can help you easily solve third-party dependencies.

Install MinGW. It is supposed that your version of MinGW contains the COM library. Currently compiled with MinGW-W64-builds-4.3.3 on 64-bit win10.

Add the bin directory of MinGW and cmake to the system environment variable path of the Windows system

    git clone https://github.com/dolphindb/DolphinDBPlugin.git
    cd DolphinDBPlugin
    mkdir build
    cd build
    cmake ../opc -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
    copy /YOURPATH/libDolphinDB.dll . 
    mingw32-make clean
    mingw32-make

Note: If you need to specify specific MingW path, please modify the following statement in CmakeList.txt:

    set(MINGW32_LOCATION C://MinGW/MinGW/)  

A file named libPluginOPC.dll will be generated under the directory.

Methods

OPC Plugin supports OPC 2.0 protocol.

Get OPC Server

Syntax

opc::getServerList(host)

Parameters

  • 'host' is a string indicating the IP address.

Details

Get the OPC server. Return a table with two columns. The first column is progID indicating the identification of server and the second column is CLSID. Please configure DCOM settings for both OPC Client and Server.

Example

opc::getOpcServerList("desk9")

Connect to OPC Server

Syntax

opc::connect(host, serverName,[reqUpdateRate_ms=100])

Parameters

  • 'host' is a string indicating the IP address.
  • 'serverName' is a string indicating the name of OPC server.
  • 'reqUpdateRate_ms' is an integer. It is used to requested update rate. It is optional, default is 100 ms.

Details

Connect to OPC Server. It returns an connection object which can be explicitly called to close the close function, or it can be automatically released when the reference count is 0. Please configure DCOM settings for both OPC Client and Server.

Example

connection=opc::connect(`127.0.0.1,`Matrikon.OPC.Simulation.1,100)

Read a Tag Synchronously

Syntax

opc::readTag(connection, tagName, [table])

Parameters

  • 'connection' is an object generated by function opc::connect.
  • 'tagName' is a string indicating the name of tag.
  • 'table' is a table or an array of tables. The number of tables must be the same as the number of tags when it is an array. It is used to store the result of the reading. If it is one table, all the values of the tags are inserted into this table. If there are multiple tables, the values read by each tag are inserted into these tables. If you do not enter a table, the return value is a table, and the record of the table is the tag value read.

Details

Read the values of the tags synchronously. An OPC connection needs to be established before.

Example

t = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, DOUBLE, INT])
opc::readTag(conn, "testwrite.test9",t)
opc::readTag(conn, ["testwrite.test5","testwrite.test8", "testwrite.test9"],t) 
tm = table(200:0,`time`tag1`quality1`tag2`quality2, [TIMESTAMP,STRING, INT,INT,INT])
opc::readTag(conn, ["testwrite.test1","testwrite.test4"],tm) 
t1 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, STRING, INT])
t2 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, INT, INT])
t3 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, DOUBLE, INT])
opc::readTag(conn, ["testwrite.test1","testwrite.test4", "testwrite.test9"],[t1,t2,t3]) 

Write a Tag Synchronously

Syntax

opc::writeTag(connection, tagName, value)

Parameters

  • 'connection' is an object generated by function opc::connect.
  • 'tagName' is a string or a string array indicating the name of tag.
  • 'value' is the values of the tags.

Details

Write the values to the tags. If the data type is incompatible, it will throw an exception.

Example

opc::writeTag(conn,"testwrite.test1",[1.112,0.123,0.1234])
opc::writeTag(conn,["testwrite.test5","testwrite.test6"],[33,11])

Subscribe

Syntax

opc::subscribe(connection, tagName, handler)

Parameters

  • 'connection' is an object generated by function opc::connect.
  • 'tagName' is a string or string array indicating the name of tag.
  • 'handler' is a function or a table or a table array. It is used to process the subscribed data.

Details

subscribe the values of the tags from the OPC server.

Example

t1 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, STRING, INT])
conn1=opc::connect(`127.0.0.1,`Matrikon.OPC.Simulation.1,100)
opc::subscribe(conn1,".testString",  t1)

t2 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, DOUBLE, INT])
conn2=opc::connect(`127.0.0.1,`Matrikon.OPC.Simulation.1,100)
opc::subscribe(conn2,[".testReal8",".testReal4"],  t2)

def callback1(mutable t, d) {
	t.append!(d)
}
t3 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, BOOL, INT])
conn10 = opc::connect(`127.0.0.1,`Matrikon.OPC.Simulation.1,10)
opc::subscribe(conn10,".testBool",   callback1{t3})

Get Subscription

Syntax

opc::getSubscriberStat()

Parameters

None

Details

Get all subscription information and return a table with the following columns:

  • "subscriptionId" indicates the subscription ID;
  • "user" is the user who creates the subscription;
  • "host" is the site of OPC server;
  • "serverName" is the OPC server name;
  • "tag" is the subscription tag;
  • "createTimestamp" is the time when the subscription is created;
  • "receivedPackets" is the number of packets received;
  • "errorMsg" is the latest error message.

Example

t=opc::getSubscriberStat()
for(sub in t[`subscriptionId]){
	opc::unsubscribe(sub)
}

Unsubscribe

Syntax

opc::unsubscribe(connection)

Parameters

  • 'connection' is an object generated by function OPC::connect.

Details

Unsubscribe from the OPC server.

Example

opc::unsubcribe(connection)

Close the connection

Syntax

opc::close(connection)

Parameters

  • 'connection' is an object generated by function opc::connect.

Details

Close the connection to the OPC server.

Example

opc::close(connection)