opc
OPC (Open Platform Communications) is an interoperability standard for secure data exchange in automation and other industries. The DolphinDB opc plugin has implemented the OPC DA 2.05a Specification, which can be used to access and collect data from OPC servers in the automation industry.
Installation (with installPlugin
)
Required server version: DolphinDB 2.00.10 or higher
Supported OS: Windows.
Installation Steps:
(1) Use listRemotePlugins to check plugin information in the plugin repository.
Note: For plugins not included in the provided list, you can install through precompiled binaries or compile from source. These files can be accessed from our GitHub repository by switching to the appropriate version branch.
login("admin", "123456")
listRemotePlugins()
(2) Invoke installPlugin for plugin installation.
installPlugin("opc")
(3) Use loadPlugin to load the plugin before using the plugin methods.
loadPlugin("opc")
Methods References
Currently, the OPC plugin only supports OPC 2.0 protocol and does not support asynchronous read and write operations.
getServerList
Syntax
getServerList(host)
Details
Get an OPC server.
Return a table with two columns. The first column is the progID indicating the identification of the server and the second is the CLSID of the server. Please configure DCOM settings for both OPC Client and Server.
Parameters
- host: A STRING scalar indicating the IP address, such as "127.0.0.1".
Examples
opc::getOpcServerList("desk9")
connect
Syntax
connect(host, serverName,[reqUpdateRate_ms=100])
Details
Connect to an OPC Server.
Return a connection object that can be explicitly closed using the close function, or automatically released when the reference count is 0. Please configure DCOM settings for both OPC Client and Server.
Parameters
- host: A STRING scalar indicating the IP address.
- serverName: A STRING scalar indicating the name of the OPC server.
- reqUpdateRate_ms: An integer indicating the request update rate in milliseconds. The default value is 100.
Examples
connection = opc::connect(`127.0.0.1,`Matrikon.OPC.Simulation.1,100)
readTag
Syntax
readTag(connection, tagName, [table])
Details
Read the values of the tags synchronously. An OPC connection needs to be established before reading.
Parameters
- connection: An object returned by connect.
- tagName: A STRING scalar/vector indicating the name of tags.
- table: A table or an array of tables used to store the result of reading. The number of tables must be the same as the number of tags when it is an array.
- If it is a table, all the tag values are inserted into this table.
- If it is an array of multiple tables, the value read by each tag is inserted into the corresponding table.
- If it is not specified,
readTag
returns a table containing all the tag values by default.
Examples
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])
writeTag
Syntax
writeTag(connection, tagName, value)
Details
Write values to the specified tags.
Parameters
- connection: An object returned by connect.
- tagName: A STRING scalar/vector indicating the name of tags.
- value: The values or array to be written to the tags.
Examples
opc::writeTag(conn,"testwrite.test1",[1.112,0.123,0.1234])
opc::writeTag(conn,["testwrite.test5","testwrite.test6"],[33,11])
subscribe
Syntax
subscribe(connection, tagName, handler)
Details
Subscribe to the values of the tags from the OPC server.
Parameters
- connection: An object returned by connect.
- tagName: A STRING scalar/vector indicating the name of tags.
- handler: A function to be called or a table where the subscribed values are written to.
Examples
t1 = table(200:0,`tag`time`value`quality, [SYMBOL,TIMESTAMP, STRING, INT])
conn1=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=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 = connect(`127.0.0.1,`Matrikon.OPC.Simulation.1,10)
opc::subscribe(conn10,".testBool", callback1{t3})
getSubscriberStat
Syntax
getSubscriberStat()
Details
Get all the subscription information.
Return a table with the following columns:
- subscriptionId: Subscription ID.
- user: The session user who creates the subscription.
- host: The site of the OPC server.
- serverName: The name of the OPC server.
- tag: The subscription tag.
- createTimestamp: The time when the subscription is created.
- receivedPackets: The number of packets received.
- errorMsg: The latest error message.
Parameters
None.
Examples
t = opc::getSubscriberStat()for(sub in t[`subscriptionId]){unsubscribe(sub)}
Unsubscribe
Syntax
unsubcribe(subscription)
Details
Unsubscribe from the OPC server.
Parameters
- subscription: The subscription ID returned by
connect
orgetSubscriberStat
.
Examples
opc::unsubcribe(subscription)
close
Syntax
close(connection)
Details
Close the connection to the OPC server.
Parameters
- connection: An object returned by
connect
.
Example
opc::close(connection)