An Opc Library and a command line to perform OPC operations with ease and transparency among different protocols. Currently supports synchronous operation over UA and DA protocols. This fork is a netstandard2.0 port with minor improvements, will see if furthur maintenance will be carried out.
To install h-Opc-net
, run the following command in the Package Manager Console:
PM> Install-Package h-opc-net
To install the command line interface, head to the nuget gallery
.
to use the UA Client simply...
using (var client = new UaClient(new Uri("opc.tcp://host-url")))
{
client.Connect();
// Use `client` here
}
or with options...
var options = new UaClientOptions {
UserIdentity = new Opc.Ua.UserIdentity("<your-username>", "<your-password>")
};
using (var client = new UaClient(new Uri("opc.tcp://host-url")), options)
{
client.Connect();
// Use `client` here
}
and to use the DA Client instead:
using (var client = new DaClient(new Uri("opcda://host-url")))
{
client.Connect();
// Use `client` here
}
You can get a reference to a node with...
var node = client.FindNode("path.to.my.node");
This will get you a reference to the node node
in the folder path.to.my
.
You can use the node reference to explore the hieriarchy of nodes with the properties Parent
and SubNodes
. For example...
Node parentNode = node.Parent;
IEnumerable<Node> children = client.ExploreFolder(node.Tag);
IENumerable<Node> grandChildren = children.SelectMany(m => client.ExploreFolder(m.Tag));
Reading a variable? As simple as...
var myString = client.Read<string>("path.to.string").Value;
var myInt = client.Read<int>("path.to.num").Value;
The example above will read a string from the tags string
and num
in the folder path.to
To write a value just...
client.Write("path.to.string", "My new value");
client.Write("path.to.num", 42);
Dead-simple monitoring:
client.Monitor<string>("path.to.string", (readEvent, unsubscribe) =>
{
DoSomethingWithYourValue(readEvent.Value);
if(ThatsEnough == true)
unsubscribe();
});
The second parameter is an Action<T, Action>
that has two parameter:
readEvent
contains all the information relevant to the event such as timestamps, quality and the valueunsubscribe
is a function that unsubscribes the current monitored item. It's very handy when you want to terminate your callback
it's important that you either enclose the client into a using
statement or call Dispose()
when you are finished, to unsubscribe all the monitored items and terminate the connection!
Each method as an asynchornous counterpart that can be used with the async/await syntax. The asynchronous syntax is recommended over the synchronous one (maybe the synchronous one will be deprecated one day).
You can also use the command line interface project to quickly test your an OPC. Build the h-opc-cli
project or download it from the release
page of this repository, then run:
h-opc-cli.exe [OpcType] [server-url]
Where OpcType
is the type of opc to use (e.g: "UA", "DA"). Once the project is running, you can use the internal command to manipulate the variable. To have more information aboute the internal commands, type help
or ?
- ...